YouTip LogoYouTip

Linux Comm Sed

[![Image 1: Linux Command Manual](#) Linux Command Manual](#) The Linux sed command uses scripts to process text files. sed can process and edit text files according to the instructions in a script. Sed is mainly used to automatically edit one or more files, simplify repetitive operations on files, write conversion programs, etc. ### Syntax sed **Parameter Description**: * -e or --expression= Process the input text file using the script specified in the option. * -f or --file= Process the input text file using the script file specified in the option. * -h or --help Display help. * -n or --quiet or --silent Display only the results after script processing. * -V or --version Display version information. **Action Description**: * a : Add, a can be followed by a string, and this string will appear on a new line (the current next line)~ * c : Replace, c can be followed by a string, and this string can replace the lines between n1 and n2! * d : Delete, because it is deletion, so d is usually not followed by anything; * i : Insert, i can be followed by a string, and this string will appear on a new line (the current previous line); * p : Print, that is, print out certain selected data. Usually p is run with the parameter sed -n~ * s : Replace, can directly perform replacement work! Usually this s action can be used with regular expressions! For example, 1,20s/old/new/g is like this! ### Examples First, we create a **testfile** file with the following content: $ cat testfile #View the contents of testfile HELLO LINUX! Linux is a free unix-type opterating system. This is a linux testfile! Linux test GoogleTaobaoTutorialTesetfileWiki Add a line after the fourth line of the **testfile** file, and output the result to standard output. Enter the following command at the command line prompt: sed -e 4anewLine testfile After using the sed command, the output result is as follows: $ sed -e 4anewLine testfile HELLO LINUX! Linux is a free unix-type opterating system. This is a linux testfile! Linux test newLine GoogleTaobaoTutorialTesetfileWiki ### Adding/Deleting by Line List the contents of **testfile** and print line numbers, and at the same time, delete lines 2 to 5! $ nl testfile | sed '2,5d' 1 HELLO LINUX! 6 Taobao 7 Tutorial 8 Tesetfile 9 Wiki The sed action is 2,5d, where **d** means delete. Because lines 2-5 are deleted, the displayed data no longer has lines 2-5. Additionally, although it should have been sed -e, it works without -e. Also, note that the action following sed must be enclosed in '...' two single quotes! To delete only line 2: $ nl testfile | sed '2d' 1 HELLO LINUX! 3 This is a linux testfile! 4 Linux test 5 Google 6 Taobao 7 Tutorial 8 Tesetfile 9 Wiki To delete from line 3 to the last line: $ nl testfile | sed '3,$d' 1 HELLO LINUX! 2 Linux is a free unix-type opterating system. Add **drink tea?** after the second line (i.e., add it to the third line): $ nl testfile | sed '2a drink tea' 1 HELLO LINUX! 2 Linux is a free unix-type opterating system. drink tea 3 This is a linux testfile! 4 Linux test 5 Google 6 Taobao 7 Tutorial 8 Tesetfile 9 Wiki If you want to add it before the second line, the command is as follows: $ nl testfile | sed '2i drink tea' 1 HELLO LINUX! drink tea 2 Linux is a free unix-type opterating system. 3 This is a linux testfile! 4 Linux test 5 Google 6 Taobao 7 Tutorial 8 Tesetfile 9 Wiki If you want to add more than two lines, add two lines of text after the second line, for example **Drink tea or .....** and **drink beer?** $ nl testfile | sed '2a Drink tea or ...... drink beer ?'1 HELLO LINUX! 2 Linux is a free unix-type opterating system. Drink tea or ...... drink beer ? 3 This is a linux testfile! 4 Linux test 5 Google 6 Taobao 7 Tutorial 8 Tesetfile 9 Wiki Each line must be marked with a backslash for a new line. In the example above, we can see that there is a at the end of the first line. ### Replacing and Displaying by Line Replace the content of lines **2-5** with **No 2-5 number**? $ nl testfile | sed '2,5c No 2-5 number' 1 HELLO LINUX! No 2-5 number 6 Taobao 7 Tutorial 8 Tesetfile 9 Wiki Through this method, we can replace entire lines of data. List only lines 5-7 of the testfile: $ nl testfile | sed -n '5,7p' 5 Google 6 Taobao 7 Tutorial Using this line-based display function of sed, we can select and display certain line numbers from a file. ### Searching and Displaying Data Search for lines in testfile containing the keyword oo: $ nl testfile | sed -n '/oo/p' 5 Google 7 Tutorial If root is found, it will output all lines and also the matching line. ### Searching and Deleting Data Delete all lines in testfile containing oo, and output other lines: $ nl testfile | sed '/oo/d' 1 HELLO LINUX! 2 Linux is a free unix-type opterating system. 3 This is a linux testfile! 4 Linux test 6 Taobao 8 Tesetfile 9 Wiki ### Searching and Executing Commands on Data Search testfile, find the line corresponding to oo, execute a set of commands in the following curly braces, separate each command with a semicolon, here replace **oo** with **kk**, and then output this line: $ nl testfile | sed -n '/oo/{s/oo/kk/;p;q}' 5 Gkkgle The final q is to exit. ### Finding and Replacing Data In addition to the whole-line processing mode, sed can also perform partial data finding and replacing by line. The find and replace of sed is similar to the vi command, with the following syntax format: sed 's/string to be replaced/new string/g' Replace the first occurrence of oo in each line of the testfile file with the string kk, and then output the file content to standard output: sed -e 's/oo/kk/' testfile The g identifier indicates global find and replace, making sed replace all matching strings in the file. The modified content will go to standard output and will not modify the original file: sed -e 's/oo/kk/g' testfile The option i makes sed modify the file: sed -i 's/oo/kk/g' testfile Batch operate on files starting with **test** in the current directory: sed -i 's/oo/kk/g' ./test* Next, we use /sbin/ifconfig to query the IP: $ /sbin/ifconfig eth0 eth0 Link encap:Ethernet HWaddr 00:90:CC:A6:34:84 inet addr:192.168.1.100 Bcast:192.168.1.255 Mask:255.255.255.0 inet6 addr: fe80::290:ccff:fea6:3484/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1.....(omitted below)..... The local IP is 192.168.1.100. Delete the part before the IP: $ /sbin/ifconfig eth0 | grep 'inet addr' | sed 's/^.*addr://g'192.168.1.100 Bcast:192.168.1.255 Mask:255.255.255.0 Next, delete the subsequent part, i.e., **192.168.1.100 Bcast:192.168.1.255 Mask:255.255.255.0**. Delete the part after the IP: $ /sbin/ifconfig eth0 | grep 'inet addr' | sed 's/^.*addr://g' | sed 's/Bcast.*$//g'192.168.1.100 ### Multi-point Editing One sed command, delete the data from the third line to the end of testfile, and replace HELLO with TUTORIAL: $ nl testfile | sed -e '3,$d' -e 's/HELLO/TUTORIAL/' 1 TUTORIAL LINUX! 2 Linux is a free unix-type opterating system. -e indicates multi-point editing. The first editing command deletes the data from the third line to the end of testfile, and the second command searches for HELLO and replaces it with TUTORIAL. ### Directly Modifying File Content (Dangerous Action) sed can directly modify the content of a file without using pipe commands or data stream redirection! However, since this action will directly modify the original file, please do not test it randomly with system configuration! Let's use the file regular_express.txt for testing! The content of the regular_express.txt file is as follows: $ cat regular_express.txt tutorial. google. taobao. facebook. zhihu- weibo- Use sed to replace the end of each line in regular_express.txt if it is . with ! $ sed -i 's/.$/!/g' regular_express.txt $ cat regular_express.txt tutorial! google! taobao! facebook! zhihu- weibo- :q:q Use sed to directly add **# This is a test** to the last line of regular_express.txt: $ sed -i '$a # This is a test' regular_express.txt $ cat regular_express.txt tutorial! google! taobao! facebook! zhihu- weibo-# This is a test Because $ represents the last line, and the action of a is to add, therefore **# This is a test** is added to the end of the file! The -i option of sed can directly modify the content of the file, which is very helpful! For example, if you have a file with 1 million lines and you want to add some text at line 100, using vim might drive you crazy! Because the file is too big! What to do? Use sed! Through sed's direct modification/replacement function, you don't even need to use vim to revise! [![Image 2: Linux Command Manual](#) Linux Command Manual](#)
← Linux Comm SortLinux Comm Rgrep β†’