How to use the SED command under UNIX

Source: Internet
Author: User
Tags first row

SED usage: sed ' Command ' filename (s) displays only results without modifying the file.

1, sed ' 2,5d ' file displays files, excluding 2-5 rows, but the number of rows exceeds the actual number of files in the file does not error.

The sed '/10[1-4]/d ' file displays files, excluding rows containing 101-104.

Sed ' 2, $d ' file displays files, showing only the first row. The sed ' 2,$!d ' file displays only the other rows except the first line.

Sed '/^ *$/d file deletes a blank line in the files.

2, sed-n '/10[1-4]/p ' file displays only the rows that contain 101-104 in file files. (-N and P must be used at the same time, otherwise only p displays all files and displays multiple rows found)

The sed-n ' 5p ' file displays only the 5th line of files

3, sed ' s/moding/moden/g ' file replaces moding with Moden

4, sed-n ' s/^west/north/p ' file replaces West's line with North and displays it.

5. The sed ' s/[0-9][0-9][0-9]$/&.5/' file replaces the 3-digit line in the file with the string that the original number plus the ". 5",& represents the search for.

6, sed ' s/\ (mod\) ing/\1en/g file will be mod as mode 1 encapsulated in parentheses, and then replaced.

The sed ' s/...$//' file deletes the last three characters of each line.

Sed ' s/^...//' file deletes the first three characters of each line.

7, sed ' s#moding#moden#g ' file replaces moding with the # behind Moden,s to represent the delimiters between search strings and replacement strings.

8, sed-n '/101/,/105/p ' file shows matching rows from 101 to 105. If only 101 matching rows are found, the matching rows from 101 to the end of the file.

Sed-n ' 2,/999/p ' file displays from line 2nd to matching rows.

9. Sed '/101/,/105/s/$/20050119/' file will increase the "20050119" content from the end of the line to 105 matching rows from 101.

10. Sed-e ' 1,3d '-e ' s/moding/moden/g ' file deletes 1-3 rows of the files before replacing them.

Sed-e '/^#/!d ' file shows the line at which the file begins with #.

11. Sed '/101/r newfile ' file adds NewFile content to each matching row

Sed '/101/w newfile ' file writes matching rows to NewFile.

12. Sed '/101/a\

> ### ' file adds a new row after the matching row.

Sed '/101/i\

> ### ' file adds a new row before matching rows.

Sed '/101/c\

> ### ' file replaces matching rows with new rows.

13. The sed ' y/abcd/abcd/' file replaces A, B, C, and D with ABCD respectively.

14. The sed ' 5q ' file is displayed to exit at the 5th row.

15. Sed '/101/{n; s/moding/moden/g} ' file finds the following line (n) to be substituted in the files.

The sed '/101/{s/moding/moden/g q;} ' file is replaced after the first matching row is found in the files and then exited.

16. Sed-e '/101/{h; D;} '-E '/104/{G;} ' file finds a matching row in a cache before matching the 104 row.

Sed-e '/101/{h; D;} '-E '/104/{g;} ' file finds a matching row with 101 in a cache before replacing the 104 matching row.

Sed-e '/101/h '-e ' $G ' file places the last matching row at the end of the file.

Sed-e '/101/h '-e ' $g ' file replaces the last row of the file with a matching row.

Sed-e '/101/h '-e '/104/x ' file has a cache in it that matches 101 rows, and then swaps with 104 of matching rows.

17. sed-f Sfile File operates according to a list of SFILE commands.

Cat Sfile

/101/a\

### #101 ####\

101****

/104/c\

### #104 deleted####\

deleted****

1i\

### #test ####\

test****

See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/OS/unix/

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.