Text Tool Three musketeers: SED

Source: Internet
Author: User

Text Tool Three musketeers: SED
SDE is a stream editor that processes a single line of content at a time. When processing, stores the currently processed rows in a temporary buffer, called the pattern space
Usage:
sed [options] [Add your own language] [file to process]
Options:
-N does not output the contents of the mode to the screen
# sed-n "10p" F2 only prints out line 10th matches to the row other does not show
Ten Uuid=07a475f4-3d7a-4b27-9ec9-500bc8a19c4d/boot
-e Multi-point editing
-F Read Edit script in specified file
-R supports the use of extended regular expressions
-I in situ edit

Script:
Address command

Address delimitation:
(1) Not to address: The full text of the processing
(2) Single address:
# The specified row
/mode/each row to which the pattern can be matched
(3) Address range
#,#
#,+#
/pat1/,/pat2/
#,/pat1/
(4) ~: Step forward
Odd lines
2~2 even rows

SED edit command
D Delete a row that matches the pattern space
Sed "10d" F2 delete the 10th line of F2 not directly delete the 10th line of the file
P Display the contents of the mode space
Sed "10p" F2 display line 10th 10th line of F2
Sed-n "10p" F2 only show line 10th print again

A \text append text at the back of the line; Support for multiline append using \ n
Sed "10a\===" F2 to the end of line 10th print = = =
Sed "a\===" F2 at the end of all lines print = = =

I \text inserting text in front of the line; Support for multi-row insertion using \ n
Sed "/uuid/i\===" F2 shown in front of the UUID =====

C \text-line or multiline text
Sed "/uuid/c\++++++++" F2 replace the rows that match the UUID to +++++++

W/path/to/somefile save pattern matching rows to the specified file
Sed "/uuid/w f3" F2 the rows to which the UUID matches are saved to F3


R/path/from/somefile reads the text from the specified file to the line in the pattern space
Sed "/uuid/r/etc/issue" F2 print the contents of the/etc/issue behind the UUID

= Print line numbers for lines in pattern space
Sed "/uuid/=" F2 the line in front of the UUID

! Matching row-fetching anti-processing in pattern space
Sed "/uuid/!=" F2 except that the UUID line does not print the others are printed again

s///find replacement, support the use of other separators, [email protected]@@,s###
Sed "/uuid/s/xfs/chen/" F2 the line searching for the UUID in the F2 file to replace XFS with Chen

Replace tag
G in-line global substitution
P shows the row that replaced the successful
W/path/to/somefile save a successful row to a file

Display columns:
Sed ' 2p '/etc/passwd show/etc/passwd on line 2nd other also show

Sed–n ' 2p '/etc/passwd only shows/etc/passwd on line 2nd other not shown

Sed–n ' 1,4p '/etc/passwd show/etc/passwd 1~4 Line

Sed–n '/root/p '/etc/passwd search for root line other not shown

Sed–n ' 2,/root/p '/etc/passwd start searching for root line from line 2nd

Sed-n '/^$/= ' file displays empty line line numbers

Sed–n–e '/^$/p ' –e '/^$/= ' file

Sed '/root/a\superman '/etc/passwd after line

Sed '/root/i\superman '/etc/passwd before line

Sed '/root/c\superman '/etc/passwd instead of line

Sed '/^$/d ' file

Sed ' 1,10d ' file

nl/etc/passwd| Sed ' 2,5d '

nl/etc/passwd| Sed ' 2a tea '

Sed ' s/test/mytest/g ' example

Sed–n ' s/root/&superman/p '/etc/passwd after the word

Sed–n ' s/root/superman&/p '/etc/passwd words before

Sed-e ' s/dog/cat/'-e ' s/hi/lo/' pets

Sed–i.bak ' s/dog/cat/g ' pets

Practice
1. Delete all whitespace characters from the beginning of lines in the/etc/grub2.conf file that begin with whitespace
Sed ' s/^[[:space:]]\+//'/etc/grub2.cfg


2. Delete all # and white space characters at the beginning of the line beginning with #, followed by at least one white-space character, in the/etc/fstab file
1. Sed "s/^#[[:space:]]\+//"/etc/fstab

3. Add # At the beginning of each line of/root/install.log
Sed ' s/.*/#&/'/etc/passwd
Sed ' s/^/#/'/etc/passwd

4. Add # to the beginning of the line in the/etc/fstab file that does not begin with #
Sed '/^#/!s/.*/#&/' f2

5, processing/etc/fstab path, use sed command to take out its directory name and base name
echo "/etc/fstab" | Sed-r ' [Email protected]^ (/?. *)/[^/]+/[email Protected]\[email protected] '
echo "/etc/fstab" | Sed-r ' [email protected]^/?. */([^/]+/?) @\[email protected] '
6. Use SED to remove the IPv4 address of the ifconfig command
Ifconfig | Sed-n "2p" | Tr-s ' # ' | cut-d#-F3


SED: Advanced editing commands
H: Overwrite the contents of the pattern space in the hold space
H: Append the contents of the pattern space to the hold space
G: Remove data from hold space to pattern space
G: Append content from hold space to mode space
x: Swap the content in the pattern space with the content in the hold space
N: Reads the next line of the matched row to overwrite the pattern space
N: Append the next line of matching rows to the mode space
D: Delete rows in the pattern space
D: Delete all rows in multi-line mode space

Display columns:
Sed-n ' N;p ' FILE
Sed ' 1! G;h;$!d ' FILE
Sed ' $! n;$! D ' FILE
Sed ' $!d ' FILE
Sed ' G ' FILE
Sed ' g ' FILE
Sed '/^$/d; G ' FILE
Sed ' n;d ' FILE
Sed-n ' 1! g;h; $p ' FILE

This article is from the "Linux" blog, so be sure to keep this source http://001230.blog.51cto.com/11559496/1886601

Text Tool Three musketeers: SED

Contact Us

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

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.