Select line 5

> sed -n '5p' rumenz.txt
Copy the code

Select 4 rows from 2 to 5

> sed -n '2,+3 p' rumenz.txtCopy the code

Select odd rows

> sed -n '1~2 p' rumenz.txt 
Copy the code

Select even rows

> sed -n '2~2 p' rumenz.txt
Copy the code

Print from line 2 to the end of the text

> sed -n '2,$ p' rumenz.txt 
Copy the code

Select the line with the rumenz word, and the next three lines.

> sed -n '/rumenz/,+3 p' rumenz.txt
Copy the code

Select the data between the line that starts with rumenz and the line that appears with the word zhan

> sed -n '/^rumenz/,/zhan/ p' rumenz.txt
Copy the code

P means print

Delete the data in rows 2 through 5

> sed '2,5 d' rumenz.txt

Copy the code

Note: the -i parameter is not added to the file, but deleted on the terminal. The original file is not deleted on lines 2 to 5.

Save the data in lines 2 through 5 to1.txt

> sed -n '2,5 w 1.txt' rumenz.txt
Copy the code

Select the data between the line starting with rumenz and the line that appears zhan, and replace a with B

> sed -n '/^rumenz/,/zhan/s/a/b/g' rumenz.txt
Copy the code

Select the line with the rumenz word, and the next three lines, and replace all a with B

> sed '/rumenz/,+3 s/a/b/g' rumenz.txt
Copy the code

willrumenz.txtIn the textareplacebAnd written to the2.txtfile

> sed -n 's/a/b/gipw' rumenz.txt
Copy the code

Regular escape

Many characters need to be escaped because of the re. You do a lot of \, * stuff in the script. You can use | ^ @! Four characters to replace

> sed '/aaa/s/\/etc/\/usr/g' rumenz.txt > sed '/aaa/s@/etc@/usr@g' rumenz.txt > sed '/aaa/s^/etc^/usr^g' rumenz.txt > sed '/aaa/s|/etc|/usr|g' rumenz.txt > sed '/aaa/s! /etc! /usr! g' rumenz.txtCopy the code

The above five commands are equivalent

Replace the backup source file

> sed -i.bak 's/a/b/g' rumenz.txt
> ls
rumenz.txt.bak rumenz.txt
Copy the code

Output lines of at least 50 characters

> sed -n '/^.{50}/p'
Copy the code

Look for the PHP files in your directory and delete any line-level comments

> find . -name "*.php" | xargs sed -i.bak '/^[ ]*#/d'
Copy the code

Print all lines starting with first and ending with end

> sed -n '/^first.*end$/p' rumenz.txt
Copy the code

Append from line 2 to line lastrumenz

> sed '2,$ s/$/rumenz/' rumenz.txt
Copy the code

From line 2 to the beginning of the last linerumenz

> sed '2,$ s/^/rumenz/' rumenz.txt
Copy the code

View discontinuous lines and ranges

> sed-n-e '2,5p' -e '4,9p' rumenz.txtCopy the code

Remove the comments

> sed '/^#\|^$\| *#/d' rumenz.txt
Copy the code

The files in thezip.ZipUnified substitution byrar

> sed 's/[zZ]ip/rar/g' rumenz.txt
Copy the code

Make two or more substitutions at a time

> sed -i 's/that/this/gi; s/line/rumenz/gi' rumenz.txtCopy the code

Deletes the last line of the file

> sed "$d" rumenz.txt
Copy the code

willrumenz.txtIn the text torumenzThe opening line is saved to1.txtfile

> sed -n '/^rumenz/w 1.txt' rumenz.txt
Copy the code

Original link :rumenz.com/rumenbiji/l… Wechat official account: entry station