Sed command details and examples

Source: Internet
Author: User

Sed command details and examples

The query function of the grep command, the stream editing function of the sed command, and the awk (which has now become a programming language) are more powerful text editing commands. in Linux system management, it plays an important and comprehensive role. The following perl programming Language (PracticalExtraction and Report Language) can implement almost all the functions of sed and awk. However, the use of perl is relatively complex and bloated, so sed and awk are still active in various aspects of shell programming.

Introduce shell variable in sed command

Shell programming in Linux -- basic usage of sed command

Sed for Unix text processing tools

Sed advanced usage

(1) sed is a non-interactive editor that processes object files by row without modifying the file itself, instead, read the file content row by row, save the copy in the temporary cache, and process it. After each row is processed, the target content of the row is printed to the screen, the content in the cache area is deleted, and then read into the next row for processing. In addition, unlike grep, whether sed knows the specified mode or not, its exit status is 0 (true in Linux ). The sed exit status is not 0 only when a syntax error exists.

The sed command is generally in the format

Sed [option] '{command}' datafile

Or

Sed [option]-f sed_script.sh data_file

If no input file exists, sed receives the input stream from the standard input.

(2) commonly used sed option

-N: cancel the default output. By default, the sed command prints matching row content again when all rows in the input file are printed. After the-n command is added, only the line matching the pattern specified in the command is printed.

-E specifies multiple editing commands. Sed accepts a group of commands by default. To specify multiple commands, use the-e Option to specify each command. For example

Sed-n-e '2017 {/pattern/p} '-e' 1,800 {/parttern/=} 'datafile

Use-e to specify two commands. The first row that matches the pattern, and the last row that prints the pattern. 1,800 is the addressing condition, indicating that only data from 1 to 800 rows can be queried.

(3) sed command

The command p is used to display the content processed by sed. It is often used with the-n option to cancel the default print operation and only print the selected content.

Command d is used to delete matching rows. Note that the buffer copy is deleted, not the file itself.

Sed '$ d' datafile
Sed '/pattern/d' datafile

The first command deletes the last line of the file ($ matches the last line) and prints all other content of the file. The second command deletes the line that matches pattern and prints all other lines.
Command s is used to replace the specified content.

Sed's/pattern/newconent/G' datafile
Sed-n'1, 20 s/str $/newconent/gp 'datafile

The first command replaces newconent with the pattern-compliant string in the (g) file globally. The second command processes line 1 to 20 Matching lines ending with str ($ matching line tail), replaces str with newcontent, and then prints matching lines.

In the preceding example,/is used as the separator for matching strings and new strings. However, you can specify other separators (except line breaks and backslash ). When other delimiters are specified, you can use the following method.

Sed's # pattern # newcontent # G' datafile

The above command specifies that # Is the separator.

Command r is a READ command that adds the content of another text file to a specific location of the current file for reading.

Sed '/pattern/r input.txt' datafile

If you match patternin a row of a file datafile, read the content of the file input.txt. This process is a global action.

Command w is a write command that writes the content of the current file to another file.

For more details, please continue to read the highlights on the next page:

  • 1
  • 2
  • Next Page

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.