KNOWN GitHub

Sed and Find

Sed and Find are both tiny but wonderful tools.

1. Sed

Give sample using this file sample.txt .

content:

Line1 hello
Line2
Line3
Line4

sed -i means taking modification back to the file.

1.1. Find and Append

sed 'N;/Line2/a\Line2.1' sample.txt

Line1 hello
Line2
Line2.1
Line3
Line4

1.2. Append at the end

sed '$a\LastLine' sample.txt

Line1 hello
Line2
Line3
Line4
LastLine

1.3. Search and Delete

sed '/Line2/'d sample.txt

Line1 hello
Line3
Line4

1.4. Delete line

sed '3d' sample.txt

delete line 3

Line1 hello
Line2
Line4

1.5. Replace

sed 's/Line/line/g' sample.txt

line1 hello
line2
line3
line4

1.6. Insert

sed '2iLine2.1' sample.txt

Inserts at line 2 Line2.1 .

Line1 hello
Line2.1
Line2
Line3
Line4

2. Find

ls -al
total 1860416
dr-xr-x---.  5 root    root         4096 Oct 10 20:33 .
dr-xr-xr-x. 18 root    root         4096 Aug  2 07:48 ..
-rw-------.  1 root    root         1549 Jul 21  2018 anaconda-ks.cfg
-rw-r--r--.  1 root    root        69856 Aug 19 15:35 kkk.txt
-rw-r--r--.  1 root    root           31 Oct 10 20:33 sample.txt
-rw-r--r--.  1 root    root         1120 Sep 19 12:05 tda.sh
-rw-r--r--.  1 tcpdump tcpdump     18997 Sep 19 12:31 test5.pcap
-rw-r--r--.  1 tcpdump tcpdump     18222 Sep 19 12:33 test6.pcap
-rw-r--r--.  1 tcpdump tcpdump    969225 Sep 19 12:50 test7.pcap
-rw-r--r--.  1 tcpdump tcpdump     29300 Sep 19 12:55 test8.pcap
-rw-r--r--.  1 tcpdump tcpdump     21058 Sep 19 13:31 test9.pcap
-rw-r--r--.  1 root    root           48 Aug 20 19:51 tst.sh

2.1. File

.
|-- nest
|   |-- ss.txt
|-- sample.txt

find ./ -name ss.txt

./nest/ss.txt

find ./ -type f -size +1M

2.2. Content

grep -R "Line" ./

./nest/ss.txt:Line1 hello
./nest/ss.txt:Line2
./nest/ss.txt:Line3
./nest/ss.txt:Line4
./sample.txt:Line1 hello
./sample.txt:Line2
./sample.txt:Line3
./sample.txt:Line4

OR:

find ./ -type f -exec cat {} + | grep Line2

Line2
Line2



Comments !

About the blog

Some notes at work and life to share

Brian Shen