Linux-sed-non-interactive text stream Editor-general Linux technology-Linux programming and kernel information. For details, see the following. Introduction
Sed is a non-interactive text stream editor. It edits the copy of a file or standard input and export text.
Instance
1. Row matching
[Root @ mypc/] # sed-n '2p'/etc/passwd print 2nd rows
[Root @ mypc/] # sed-n '1, 3 P'/etc/passwd print 1st to 3rd rows
[Root @ mypc/] # sed-n' $ P'/etc/passwd print the last line
[Root @ mypc/] # sed-n'/user/'P/etc/passwd print the row containing the user
Rpc: x: 32: 32: Portmapper RPC user: // sbin/nologin
Rpcuser: x: 29: 29: RPC Service User:/var/lib/nfs:/sbin/nologin
[Root @ mypc/] # sed-n'/\ $/'p/etc/passwd print the row containing $ metacharacters, $ indicates the last row
2. Insert text and additional text (insert a new line)
[Root @ mypc/] # sed-n'/FTP/P'/etc/passwd print the FTP row
Ftp: x: 14: 50: FTP User:/var/ftp:/sbin/nologin
[Root @ mypc/] # sed '/FTP/a \ 100'/etc/passwd Insert a new row after the row containing FTP, with the content 456
[Root @ mypc/] # sed '/FTP/I \ 123'/etc/passwd Insert a new row before the row containing FTP, with the content 123
[Root @ mypc/] # sed '/FTP/I \ "123"'/etc/passwd Insert a new row before the row containing FTP with the content of "123 ″
[Root @ mypc/] # sed '5 a \ 100'/etc/passwd Insert a new row after row 123 with the content of 5th
[Root @ mypc/] # sed '5 I \ "12345" '/etc/passwd Insert a new row before row 5th with the content "12345 ″
3. delete text
[Root @ mypc/] # sed '1d '/etc/passwd Delete row 1st
[Root @ mypc/] # sed '1, 3d '/etc/passwd Delete lines 1st to 3
[Root @ mypc/] # sed '/user/D'/etc/passwd Delete row with user
4. Replace the text. Replace the command with the replacement mode to replace the specified mode. The format is:
[A d r e s [, address] s/pattern-to-find/replacement-pattern/[g p w n]
[Root @ mypc/] # sed's/user/USER/'/etc/passwd: replace 1st users with user, and g indicates global replacement.
[Root @ mypc/] # sed's/user/USER/G'/etc/passwd replace all users with user
[Root @ mypc/] # sed's/user/# user/'/etc/passwd: replace 1st users with # user, for example, used for shielding
[Root @ mypc/] # sed's/user // '/etc/passwd: replace 1st users with null
[Root @ mypc/] # sed's/user/& 11111111111111/'/etc/passwd if you want to append or modify a long string, you can use the (&) command,
& Save the discovery mode of the command to call it again, and put it in the replacement string. Here we put & above
[Root @ mypc/] # sed's/user/11111111111111 &/'/etc/passwd here will be placed behind
5. A quick command line
Below are some command lines. ([] Indicates space, [] indicates t a B key)
'S/\. $ // G' Delete the row ending with a period
'-E/abcd/d': Delete the row that contains a B c d.
'S/[] [] [] */[]/G' Delete more than one space and replace it with one space
'S/^ [] [] * // G' Delete the first space in the row
'S/\. [] [] */[]/G' deletes a period followed by two or more spaces, replacing it with a space.
'/^ $/D' Delete empty rows
'S/^. // G' Delete the first character
'S/COL \ (... \) // G' Delete the last three letters followed by C O L
'S/^ \ // G' Delete the first \ from the path \
'S/[]/[] // G' delete all spaces and replace them with the t a B key.
'S/^ [] // G' delete all t a B keys at the beginning of the row
'S/[] * // G' delete all t a B keys
If you use s e d to filter files, it is best to divide the problem into several steps, perform the step by step, and execute the test results.
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service