Sed, awk easy to use

Source: Internet
Author: User

Sed is a non-interactive stream editor that allows you to dynamically edit files.

Non-interactive mode: unlike traditional text editors, sed does not directly interact with users. The object processed by SED is the stream of files ).

The SED working mode is to compare each data row. If the style is correct, the specified operation is executed.

Syntax format:

Sed 'style command 'file

If a line in the file meets the "style command", run the specified sed command, such as Delete (d), replace (s), and output (P ).

Here, the "style command" is enclosed by a pair of // for searching.

For example:

1,/, indicating 1 to 6 rows

2./AAA/,/DDD/, indicates the line containing the AAA character to the line containing the DDD character

Note: sed does not change the file content. Sed reads the file content and outputs the result to the standard output after being edited by the stream. Therefore, if you want to save the SED processing result, you need to use the redirection.

Example:

1. delete data rows in a certain range

sed '1,4d' filepath

2. delete a row containing the specified "style"

sed '/styleText/d' filepath

3. Delete blank rows

sed '/^$/d' filepath

4. Delete rows with unspecified "styles"

sed '/styleText/!d' filepath

5. Output rows matching the "style"

sed -n '/styleText/p' filepath

The command P will display the current data, but because SED will also display non-conforming data rows by default, you need to use the option "-n ".

-N: suppress the display of non-conforming data rows.

6. Delete the three characters starting with each line

sed 's/^...//' filepath

7. Get the matching string

sed -n 's/\(styleText\)/\1string/p' filepath

() Used to store matching characters

\ 1 get the matched characters

8. Find the matched data row and then replace the command.

sed -n '/AAA/s/123/456/p' filepath

Find the row containing AAA, replace 123 with 456

9. Replace the command on the specified line

sed -n '2,4s/123/456/p' filepath

In lines 2nd to 4, replace 123 with 456

Awk to be continued

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.