The SED of shell programming

Source: Internet
Author: User
Tags uuid egrep

SED command

Text Processing Three musketeers:
Grep,egrep,fgrep: Text Filter
Sed: Stream editor, in behavior units
awk: Text formatting tool, Report Builder

sed [OPTION] ... {Script-only-if-no-other-script} [File] ...

Common options:
-N: Do not print the line is not OK, output mode space content to the screen;
-e: Multi-point editing; Connect multiple commands at the same time; You can also use semicolons ";" Separated
-F: Specifies the script file and applies the script to the file;
The contents of the script file, one command per line;
Cat Sed.rules
S/this/that/g
/^$/d
Sed-f Sed.rules Sed.text
-R: Supports the use of extended regular expressions;
-I: Directly modify the original file, you can use the-i.bak parameters to back up the original file;
Sed–i.bak ' s/dog/cat/g ' pets

Address delimitation:
(1) Not to address: The full text of the processing;
(2) Single address:
#: Specify the first few lines;
/pattern/: Each row that is matched by this pattern;
(3) Address range:
#,#: The first few lines to the first few lines;
#,+#: The first few lines to + #行;
#,/pat1/: Lines to first match to PAT1;
/PAT1/,/PAT2/: The first match to the row of the PAT1 to the first match to the PAT2 line;
$: last line;
(4) Step forward: ~
N/A: all odd lines;
2~1: all even lines;

Edit command:
D: Delete
Sed ' 1,5d '/etc/fstab
P: Printing, must use the-n parameter;
Ifconfig | Sed-n ' 2p ' shows the second line
Sed ' 1~2p '/etc/fstab odd rows displayed 2 times
Sed-n ' 1~2p '/etc/fstab show only odd lines
Sed ' 1~2d '/etc/fstab show even lines
I\text: Insert Text "text" in front of line, support using \ n to implement multiline insertion;
Sed ' 3 i \new line '/etc/fstab inserting new lines in front of the third row
Sed ' 3 i \new line\nanother New line '/etc/fstab
A\text: Append text "text" after line, support using \ n to implement multiline append;
Sed ' 3 a \new line '/etc/fstab insert new lines after the third row
Sed '/uuid/a \new line '/etc/fstab inserting new lines after the row containing the UUID
C\text: Replace the matched line with the text "text" specified here, note that the whole line is replaced;
W: Saves the matched result to the specified position;
Sed-n '/^[^#]/p '/etc/fstab
Sed '/^[^#]/w/tmp/fstab.new '/etc/fstab
R: Read text from other files and insert matching line; file merge;
Sed ' 3 r/etc/issue '/etc/fstab
=: The line number is printed for the row to match;
Sed '/^uuid/= '/etc/fstab
!: conditional inversion;
Sed '/^[^#]/d '/etc/fstab delete lines not beginning with #
Sed '/^#/!d '/etc/fstab delete lines not beginning with #
Sed ' 5!d '/etc/fstab retains only line 5th (processing unmatched rows)
s///: Find replacement, its delimiter can be self-specified, commonly used are [email protected]@@,s## #等;
By default, only the content that matches the first time is replaced;
Sed ' s/line/line '
To match up to 2 per line, use/2
Sed ' S/LINE/LINE/2 '
Replace tag:
G: global substitution;
P: Shows the successful replacement line;
W: Save the result of the replacement successfully to the specified file;
&amp: Refers to the entire contents of the previous search;
The Find Replacement command adds # to the beginning of each line of the/tmp/functions file that begins with a white-space character;
:%[email protected]^[[:space:]]@#&@g
\ (\): Back reference, reference grouping, such as s/\ (love\) able/\1r,loveable replaced with lovers;

Practice:
1. Delete all whitespace characters at the beginning of the line beginning with whitespace characters in the/etc/prelink.conf.d/grub2.conf file;
Sed ' [email protected]^[[:space:]]\[email protected]@ '/etc/prelink.conf.d/grub2.conf

2. Delete all the white characters from the beginning of the line beginning with # in the/etc/fstab file and the # after # and #.
Sed ' [email protected]^#[[:space:]]*@@ '/etc/fstab

3, output an absolute path to SED, take out its directory;
echo "/var/log/messages" | Sed ' [Email protected][^/]\[email protected]@ '
echo "/var/log/messages" | Sed-r ' [Email protected][^/][email protected]@ '

4. Add # At the beginning of each line of/root/install.log;
Sed ' [email protected]^.*@#&@ '/root/install.log
Sed ' [email protected]^@#@ '/root/install.log

5. Add # to the beginning of the line in the/etc/fstab file that does not begin with #;
Sed '/^#/[email protected]*@#&@ '/etc/fstab (processing unmatched rows)
Sed '/^#/[email protected]^@#@ '/etc/fstab

6, the processing/etc/fstab path, uses the SED command to take out its directory name and the base name;
echo "/etc/fstab" | Sed ' [Email protected][^/]\[email protected]@ '
echo "/etc/fstab" | Sed ' [Email protected]^.*[/]@@ '

7. Using SED to remove the IPv4 address of the ifconfig command;
Ifconfig | Sed-n ' 2p ' | Tr-s "" | Cut-d ""-f3

8. Filter out the schema field of RPM package in/media/cdrom/packages directory, and count the number of each architecture;
LS *.rpm | Sed ' [email protected]*\.\ (. *\) \[email protected]\[email protected] ' | Sort | Uniq-c
LS *.rpm | Sed-r ' [email protected]*\. (. *) \[email protected]\[email protected] ' | Sort | Uniq-c
LS *.rpm | Sed-r ' [email protected]*\. (.*)\.. *@\[email protected] ' | Sort | Uniq-c
LS *.rpm | Rev | cut-d.-f2 | Rev | Sort | Uniq-c
Rev:reverse lines of a file or files. Turn each line in reverse order;

9, statistics a file which word repeats the most times;
Egrep-o "[[: alpha:]]+]/etc/init.d/functions | Sort | uniq-c | Sort-n
Sed ' [email protected][^[:alpha:]]@\[email protected] '/etc/init.d/functions | Sort | uniq-c | Sort-n


Advanced Editing Commands:
H: The content of the pattern space is covered in the holding space;
H: Append the contents of the pattern space to the holding space;
G: To cover the contents of the holding space in the pattern space;
G: Append the contents of the holding space to the pattern space;
x: Swap the content in the pattern space with the content in the hold space;
N: Overwrites the next line of the row to the pattern space, reads the next input line, and processes the new line with the next command;
N: Append the next line of the row to the pattern space to read;
D: Delete rows in the pattern space;
D: Delete all rows in multi-line mode space;

Example:
Sed-n ' n;p ' file: Shows even rows;
Sed ' 1! G;h;$!d ' file: Display the contents of files in reverse order;
Sed ' $!d ' file: Take out the last line;
Sed ' $! n;$! D ' file: two rows after removal;
Sed '/^$/d; G '/etc/fstab: Delete all existing blank lines, and then add a blank line after all the non-blank lines;
Sed ' n;d ' file: Displays odd lines;
Sed ' G ' file: Adds a blank line behind the original line;
Sed-n ' 1! g;h; $p ' file: Displays the contents of files in reverse order;

The SED of shell programming

Related Article

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.