sed of the shell text processing three swordsmen (sed awk grep)

sed of the shell text processing three swordsmen (sed awk grep)

Respective advantages

Types of and awk grep
Do you require formatting Claim Claim no request
Support regular Standard regular Standard regular Extended regular

sed detailed

It is a pipeline command, which is mainly processed in units of behaviors. It can perform specific tasks such as replacing, deleting, adding, and selecting data rows.

  • sed common parameters

Common options:
Usage: sed [OPTION]… {script-only-if-no-other-script} [input-file]…
-n: Use silent mode. In general sed usage, all data from STDIN will generally be listed on the screen. But if the -n parameter is added, only the line (or action) that has been specially processed by sed will be listed.
-e: Edit sed actions directly in command line mode;
-f: write sed actions directly in a file, -f filename can execute sed actions in filename;
-r: sed actions support The grammar of extended regular notation. (The default is basic regular notation syntax)
-i: directly modify the content of the read file instead of outputting it on the screen.
Commonly used commands:
a: add new, after a can be followed by strings, and these strings will appear on a new line (the current next line) ~
c: replace, after c can be followed by strings, these strings can be replaced The line between n1 and n2!
d: delete, because it is a delete, so there is usually no dongdong after d;
i: insert, you can connect strings after i, and these strings will appear on a new line (the current previous line);
p: Print, that is, print out a selected data. Usually p will work with the parameter sed -n~
s: Replace, you can directly perform the replacement work! Usually the action of s can be matched with regular notation! For example, 1,20s/old/new/g is it!

sed add, delete, modify and check

** Add operation **

  1. Add a line at the end of the line
# 无数据
cat sed_test  
# vi 添加hello world1
vi sed_test 
# sed 行末添加数据
sed -i '$a hello world' sed_test 
# 查看vi的数据与sed添加的数据
cat sed_test 

1. Add a line

Delete operation

  1. Delete the first line

But '1d' from

  1. Delete the last line

But "$ d" of

  1. Delete the first row to the second row

But '1,2d' from

  1. Delete the second to last line

But the 2, $ d "of

Modify operation

  1. Modify a line

But 1c Hi sed_test

Insert picture description here

  1. Modify multiple lines

But -n '1,2c These' sed_test

Insert picture description here

  1. Modify part of a line

sed -n ‘/hello/p’ sed_test | sed ‘s/hello/hi/g’

Insert picture description here

  1. Modify part of multiple lines

Query operation

  1. Show the first line

But -n '1p' from

  1. Show last line

But -n '$ p' from

  1. Display the first to second line

sed -n ‘1,2p’ ab

  1. Display the second to last line

sed -n '2,$p' ab
regular pattern matching

  1. The query includes all rows where the keyword ruby ​​is located

sed -n ‘/ruby/p’ ab

  1. The query includes all lines where the keyword $ is located, and uses backslash\ to shield special meaning

But -n '/ $ / p' from

Guess you like

Origin blog.csdn.net/dbc_zt/article/details/111045362