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 **
- 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
Delete operation
- Delete the first line
But '1d' from
- Delete the last line
But "$ d" of
- Delete the first row to the second row
But '1,2d' from
- Delete the second to last line
But the 2, $ d "of
Modify operation
- Modify a line
But 1c Hi sed_test
- Modify multiple lines
But -n '1,2c These' sed_test
- Modify part of a line
sed -n ‘/hello/p’ sed_test | sed ‘s/hello/hi/g’
- Modify part of multiple lines
Query operation
- Show the first line
But -n '1p' from
- Show last line
But -n '$ p' from
- Display the first to second line
sed -n ‘1,2p’ ab
- Display the second to last line
sed -n '2,$p' ab
regular pattern matching
- The query includes all rows where the keyword ruby is located
sed -n ‘/ruby/p’ ab
- The query includes all lines where the keyword $ is located, and uses backslash\ to shield special meaning
But -n '/ $ / p' from