SED command entry and advanced

Source: Internet
Author: User
Tags uuid line editor
SED command entry and advanced

Sed:stream EDitor, line editor:


Operating mechanism:
Do not edit the file in situ, but read one row from the specified file, read to the pattern space, edit the content in the pattern space based on the predefined editing script, and output the result to the screen after the edit is finished;

Pattern spaces: Mode space
Hold space: Keep room

Usage:

sed [option] ... ' Script ' Inputfile ...

Script
Format:
' Address command '

No spaces between "note" Address and command

Common options:
-N: Do not output the contents of the mode space to the screen;
-E: Realize multi-point editing function;
-f/path/to/script_file: Reads the edit script from the specified file with one execution command per line in the script;
-R: Using extended regular expressions, default is basic grep
-I: Edit in situ, modify the original document;
Such as:
~]# sed-i.bak ' 2,4d ' file1
~]# ls file1 file1.bak

Address delimitation:
Three kinds of cases:
1, not to address: The full text of the processing;
2. Single Address:
#: The specified line
/pattren/: Each row that can be matched by the pattern here;
3, address range:
#,# :
#,+# :
/pattern1/,/pattern2/:
#,/pattern1/:
4. ~: Step in
1~2: Read all odd lines
2~2: Read all even rows

such as: ~]# seq 10 | Sed-n ' 1~2p '

Edit command:
D: Delete
For example, delete the output after the row that matches the specified pattern:
~]# sed '/^uuid/d '/etc/fstab
~]# sed '/nosuid/d '/proc/mounts
Delete 1-4 lines:
~]# sed ' 1,4d '/etc/fstab

P: Shows what can be matched to patterns in the pattern space
For example, when the content in the mode space is exported to the screen, the line matching the pattern is also output to the screen:
~]# sed '/^uuid/p '/etc/fstab
Does not output the contents of the mode space to the screen, only shows the line to screen in the mode space that can be matched by pattern:
~]# sed-n '/^uuid/p '/etc/fstab

The following two commands in "notes" have the exact opposite effect:
~]# sed '/^uuid/p '/etc/fstab
~]# sed-n '/^uuid/p '/etc/fstab

A [\]text: Append text content after lines that conform to pattern mode, support using \ n to implement multiple-line append

Such as:
~]# SEQ 10 | Sed ' 1,4a\========= '
~]# SEQ 10 | Sed ' 1,4a\ ========= '
~]# SEQ 10 | Sed ' 1,4a\ =====\n +++++ '

I [\]text: Inserts text content before lines that conform to pattern mode, supports using \ n to implement multiple-row inserts

c [\]text: Replace the whole behavior text text with pattern mode

W/path/to/somefile: Deposit eligible content into a file
Such as:
~]# sed '/^uuid/w/test_files/fstab.txt '/etc/fstab

R/path/from/somefile: Reads the file contents of the specified path behind each line in the current file that meets the criteria;
For example: Read the contents of the/etc/issue file after "one line":
~]# sed-n ' 6r/etc/issue '/etc/fstab
Read the contents of the/etc/issue file after each line that meets the criteria:
~]# sed-n '/^uuid/r/etc/issue '/etc/fstab

=: Print line numbers for lines in the pattern space
such as: ~]# sed '/^\/dev/= '/etc/fstab

。 : Take the reverse condition
Such as:
~]# sed '/^uuid/d '/etc/fstab

Take counter:
~]# sed '/^uuid/!d '/etc/fstab
Equals:
~]# sed-n '/^uuid/p '/etc/fstab

S///: Find replacement, support other separators, s@@@,s###;
Replace tag:
G: In-line global substitution
P: Show only rows that have been replaced successfully
W/path/to/somefile: Saves the result of the replacement success to the specified file

such as: ~]# sed ' s@dev@dev@g '/etc/fstab

Back reference:
For example: Add ABC after content in f...t format:
~]# sed ' s@f...t@&abc@g '/etc/fstab
can also be:
~]# sed ' s@\ (f...t\) @\1abc@p '/etc/fstab


Practice:
1. Delete all whitespace characters in the/var/log/boot.log file that start at the beginning of a blank line;
Solution:
~]# sed ' s/^[[:space:]]\+//g '/var/log/boot.log
2. Delete the # and whitespace characters of all lines beginning with #, followed by at least one blank character in the/etc/fstab file;
Answer:
~]# sed ' s@^#[[:space:]]\+@@g '/etc/fstab

3, echo an absolute path to the SED command, remove its base name and directory name;
Answer:
Take base name:
~]#

Fetch Directory Name:
]# echo-e "/etc/fstab\n/etc/init.d/inittab/" | Sed ' s@\<[^/]\+/\?$@@ '



Sed Advanced Edit command:
H: Cover the contents of the pattern space to maintain space
H: Append the contents of the pattern space to the holding space
G: Remove data from the retention space to cover the pattern space
G: Remove data from retention space append to mode space
x: Swap the content in the pattern space with the content in the retention space
N: Reads the next line of matching rows to the pattern space
N: Reads the next line of matching rows append to the pattern space
D: Delete rows in the mode space
D: Delete rows in multiple-line mode space


Seq a number starting at 1, taking out even rows from it:
~]# SEQ 10 | Sed-n ' N;p '

Display file contents in reverse order:
~]# SEQ 10 | Sed ' 1! G;h;$!d '

Two lines after displaying the file:
~]# SEQ 10 | Sed ' $! n;$! D

1 lines after displaying file:
~]# SEQ 10 | Sed ' $! D

Add a blank line between each of the two lines:
~]# SEQ 10 | Sed ' G '

Delete all blank lines, and then add a blank line between each of the two rows:
~]# sed '/^$/d; G ' FILE

Show Odd lines:
~]# SEQ 10 | Sed ' n;d '

Display file contents in reverse order:
~]# SEQ 10 | Sed-n ' 1! G;h, $p '

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.