Sed is a very powerful file processing tool, mainly in the behavior of the processing units, you can replace the data rows, delete, add, select and other specific work
Format: sed [option] [command] [file]
Common commands:
A: New
C: Replace
D: Delete
I: Insert
P: Print
S: replace
Options:
-I: Directly modify the contents of the read file instead of the screen output.
-N: Use Quiet (silent) mode. In the usage of general sed, all data from stdin is generally listed on the screen. However, if you add the-n parameter, only the line (or action) that is specially processed by SED is listed.
1,sed ' 1d ' ghostwu.com D represents the deletion of the first row of the number represented by D, which does not modify the file itself
[Email protected]:~/linux/sed$Cat-N ghostwu.txt1This is GHOSTWU2 How is You3hod old is you4 and you5Fine Thank you6Come with me!!![email protected]:~/linux/sed$sed '1d'Ghostwu.txt How is youhod old is youand youfine thank youcome with me!!![email protected]:~/linux/sed$CatGhostwu.txt This is ghostwuhow be youhod old be youand youfine thank youcome with me!!!
2, delete last line, $ for last line
[Email protected]:~/linux/sedcat ghostwu.txt This is ghostwuhow be youhod old be youand youfine th Ank youcome with me!!! [email protected]:~/linux/sedsed'$d' ghostwu.txt This is Ghostwuhow is Youhod old is Youand youfine thank You
3, delete the first line to the second row
[Email protected]:~/linux/sedcat ghostwu.txt This is ghostwuhow be youhod old be youand youfine th Ank youcome with me!!! [email protected]:~/linux/sedsed'1,2d' ghostwu.txt hod Old is Youand youfine thank youcome with me!!!
4, delete the second line to the last row
[Email protected]:~/linux/sedsed'2, $d' ghostwu.txt this is GHOSTWU
5, find the row containing ' you ',/you/this is a regular expression, p is print, to be combined with N.
[Email protected]:~/linux/sedcat ghostwu.txt This is ghostwuhow be youhod old be youand youfine th Ank youcome with me!!! [email protected]:~/linux/sedsed'/you/p' ghostwu.txt How is Youhod old is Youand youfine thank You
6, matches the line at the end of the $ symbol
The $ symbol represents the end of the line in a regular expression, so it is escaped with \
[Email protected]:~/linux/sedsed'/\$/p'$
7, after the first line, add a line "hello."
[Email protected]:~/linux/sed$CatGhostwu.txt This is ghostwuhow be youhod old be youand youfine thank youcome with me!!!How much Do you have -$oh, is it?Yes[email protected]:~/linux/sed$sed '1a Hello.'Ghostwu.txt This is Ghostwu hello, how was Youhod old was Youand youfine thank youcome with me!!!How much Do you have -$oh, is it?Yes
8, add a row after the first and second lines
[Email protected]:~/linux/sedsed'1,2a learning how to use sed command' sedsed commandfine thank You
9, you can also add multiple lines
[Email protected]:~/linux/sedcat Ghostwu.txt This was Ghostwuhow is youfine thank You[email Protected]:~/linux/sedsed'1a Hello, nice to meet you. ' ghostwu.txt this is Ghostwu hello, nice to meet you. How was youfine thank you
C for the substitution operation, the meaning of the number, as in the above a, representing the line
[Email protected]:~/linux/sedcat Ghostwu.txt This was Ghostwuhow is youfine thank You[email Protected]:~/linux/sedsed'1,2c Heyguy' ghostwu.txt Hey Guyfine thank You[email Protected]:~/linux/sedsed'1c Heyguy' ghostwu.txt Hey guyhow is youfine thank you
One, S: replace the match to the content, S: Replace start/is/match the line containing is to G: replace All
[Email protected]:~/linux/sed$CatGhostwu.txt This was Ghostwuhow is youfine thank You[email protected]:~/linux/sed$sed 's/is/is/'Ghostwu.txt This was Ghostwuhow is youfine thank You[email protected]:~/linux/sed$sed 's/is/is/g'Ghostwu.txt This was Ghostwuhow is youfine thank You[email protected]:~/linux/sed$CatGhostwu.txt This is ghostwuhow be youfine thank you
12,-I: Modify, insert the file, will affect the contents of the file, in the last line, insert bye bye
[Email protected]:~/linux/sedcat Ghostwu.txt This was Ghostwuhow is youfine thank You[email Protected]:~/linux/sedsed'$a bye bye' ghostwu.txt [email Protected]:~/linux/sedcat ghostwu.txt This is ghostwuhow be youfine thank Youbye bye
13, in 1-3 rows, after each row is inserted a row of numbers
[Email protected]:~/linux/sedcat Ghostwu.txt This was Ghostwuhow is youfine thank Youbye Bye[email Protected]:~/linux/sedsed'1,3a 123457' ghostwu.txt [email Protected]:~/linux/sedcat ghostwu.txt This was GHOSTWU123457how is123457Fine thankyou123457Bye bye
Common basic commands for Linux:-sed of the Three Musketeers command