Text Three musketeers sed

Source: Internet
Author: User
Tags line editor

My previous article introduced grep, and today I'll say a second sed, which is the abbreviation for Stream editor. In a Linux text file, the text is stored in a row, the display of many lines is actually because of the existence of a newline character, for example: The text is: Abc$def$ghi display is three line. Sed is a line editor that reads one line of text at a time and then, depending on our choices, synchronizes to the text. There's also an awk, called gawk (GNU awk) on Linux, which is a text formatting tool that we'll talk about in our next article.

Working mechanism: Each time a line of text is read into "pattern space", the processing is done in the mode space, and the processing result is output to the standard output device;

syntax: sed [OPTION] ... {Script} [Input-file] ...

-R: Supports extended regular expressions;

-N: Silent mode;

-E script1-e script2-e SCRIPT3: Specifies multiple scripts to run;

-f/path/to/script_file: Reads the script from the specified file and runs it;

-I: Directly modify the source file;

Address delimitation:

#: Specify line;

$: last line;

/regexp/: Any line that can be matched to by regexp; Sed '/^#/d '/etc/fstab #删除以井号开头的行

\%regexp%: Ibid., except for the regexp boundary symbol;

/regexp/| : Ignoring character case when matching;

\%regexp%| : Ignoring character case when matching;

Startline,endline:

#,/regexp/: Starting from # line, to the end of the line to which the first/regexp/is matched, all rows in the middle;

#,#

/regexp1/,/regexp2/: Starting from the first line that is matched to the/regexp1/, to the end of the line that is first matched to the/regexp2/, all rows in the middle;

#,+|-n: Starting with # lines, up to n rows down;

First~step: Specify the starting line, and the step size;

edit commands for sed

D: Delete rows in the pattern space; sed ' 1,2d '/etc/fstab

=: Display line number; the line number of the line beginning with sed '/^#/= '/etc/fstab #显示以 #

A \text: Additional text;sed '/^#/a \new line '/etc/fstab# to add the word "new" after the start of the well number

Sed ' 1a \new line\nsecond line\ntherid line ' #\n denotes newline characters

I \text: Insert text, support \ n implement multi-line insertion;

C \text: Replace the matching line with text; sed '/5,7/c \new txt ' #五到七行都替换成一个new txt

Sed '/^#/c \new txt '/etc/fstab# only the lines at the beginning of the well are replaced with new txt, not just one

P: Lines in print mode space; sed ' 5,7p '/etc/fstab#5-7 line content is displayed two times, using-N to meet our expectations

s/regexp/replacement/: Replace the content that is matched by regexp to replacement;

G: global substitution;

I: Case insensitive #sed ' s/^#//g '/etc/fstab

W/path/to/somefile: Save the specified content to the file specified by the/path/to/somefile path;

# sed ' 5,9w/tmp/test1.txt '/etc/fstab# writes 5-9 lines in Fstab to Test1.txt

R/path/from/somefile: Insert all the contents of another file at the specified location of the file, complete the file merge;

#sed ' 8r/etc/issue '/etc/fstab# to read the contents of the 8th line after issue and put it in front of the fstab.

Practice:

(1) Remove whitespace characters from the beginning of all lines in the/boot/grub/grub.conf file;

Sed ' s/^[[:space:]]\+//'/boot/grub/grub.conf

(2) Remove all # and white space characters from the beginning of the line at the beginning of #, followed by at least one white-space character, in the/etc/fstab file;

Sed ' s/^#[[:space:]]\+//'/etc/fstab

(3) Save the odd line of the/etc/fstab file as a/tmp/fstab.3;

Sed ' 1~2w/tmp/fstab.3 '/etc/fstab

(4) Echo a file path to the SED command, take out its base name, and further, take out its path name;

Base name: Echo "/etc/sysconfig/network-scripts/" | Sed ' [Email protected]^.*/\ ([^/]\+\]/\[email protected]\[email protected] '

Fetch path name: echo "/etc/sysconfig/network-scripts/" | Sed ' [Email protected][^/]\+/\[email protected]@ '

SED has not only a pattern space, but also a hold space, which helps SED to temporarily store something for later use.

Advanced Command:

H: Use the content of the pattern space to cover the content of keeping the space;

H: Append the contents of the pattern space to the content in the holding space;

G: Take its content from the hold space and overwrite it with the contents of the pattern space;

G: Take the content from the hold space and append it to the content in the pattern space;

X: Exchanging in the space of keeping and pattern;

N: Reads the next line from the matching row to the pattern space (overwrites the original content in the pattern space);

N: Reads the next line to the pattern space of the matched row, appended to the original content in the pattern space;

D: Delete the contents of the pattern space;

D: Delete the first row in multi-line mode space;

Note: Command function can be used! Reverse; semicolons can be used to separate scripts;

Example:

Sed ' G '/etc/issue: Adds a blank line behind each line in the file;

Sed ' $!d '/etc/fstab: Keep the last line;

Sed '/^$/d; G '/etc/issue: Ensure that the specified file has only one blank line behind each line;

Sed ' n;d '/etc/issue: reserved odd lines;

Sed-n ' 1! g;h; $p '/etc/issue: Full-text flashback display

Sed ' $! n;$! D '/etc/issue: Show last two lines

Text Three musketeers sed

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.