Recommended: One Linux command per day (2) : od
Function introduction
Sed is a stream editor and a great tool for text processing. With more powerful processing with the re, the current line is stored in a temporary buffer called “pattern space,” and the contents of the buffer are then processed with the sed command, printed to the screen, and the next line is processed. The file contents do not change unless the -i option is used. Sed is mainly used to edit one or more files, simplify repeated operations on files, and write conversion programs.
Sed has the same function as AWK, except that it is simple but has a weaker function in column processing. Awk has a more complex function in column processing.
The command format
sed [options] 'command' file(s)
sed [options] -f scriptfile file(s)
Copy the code
Commonly used parameters
-e # Process the input text file with the specified instruction -n # cancel the default output (only the changed lines will be printed if used with the p command) -h # help -v # Display version informationCopy the code
Common actions
A # insert text below current line I # insert text above current line C # change selected line to new text D # delete, Delete the selected line D # Delete the first line of the template block S # replace the specified character H # copy the contents of the template block to the memory buffer H # Append the contents of the template block to the memory buffer G # Obtain the contents of the memory buffer and replace the text in the current template block G # obtain the contents of the memory buffer N # Reads the next input line, processes the new line with the next command instead of using the first command n # appends the next input line to the template block and inserts a new line between the two, Change the current line number p # Print a matching line P #(uppercase) Print the first line of the template Q # exit Sed B #lable branch to the marked part of the script, branch to the end of the script if none exists r #file Read the line from file t #label if branch, Starting at the last line, if the condition is satisfied or the T, T command, it will branch to the labeled command, or at the end of the script, the T #label error branch, starting at the last line, if an error occurs or the T, T command, it will branch to the labeled command, ** w #file Write and append the first line of the template block to the end of the file **! # indicates that the following command takes effect on all unselected lines ** = # prints the current line number ** # # extends the comment to the next line break **Copy the code
Sed replacement command
G # said inline fully replace replaced with s command USES the (global) p # said printing line w # said to write a template file x # said swap block of text in a text and buffer y # said translate one character for another character (but not for the regular expression) 1 # substring matching tag & The string tag has been matchedCopy the code
Sed to regular
^ # Match the beginning of a line $# Match the end of a line. # Match any character that is not a newline * # Match zero or more characters [] # match a character in the specified range [^] # Match a character in the specified range (..) # match substring & # Save the search character to replace other characters < # match word start > # match word end x{m} # repeat character x, m times x{m,} # repeat character x, m times x{m,n} # repeat character x, m times at least, m times but not more than n timesCopy the code
Sed common examples
1. Replace operations
Echo "hello world" | sed 's / / - / 1 g' hello world # global replace - since the first space, just only a space in the textCopy the code
2. Delete operations
Sed '/^$/d' filename 'sed '2d' filename' Sed '$d' filename # sed '/^test/'d filename # delete the last line starting with testCopy the code
3. Match the replacement
Echo "hello world" | sed 's/w + / [&] / g' [hello] [world] echo "hello world" | sed 's/w + / "&"/g' "hello," "world" # w + match each word, & echo string means to match to the AAA BBB | sed 's/([a-z] +) ([a-z] +) / [2] [1] /' (BBB) (AAA) # substring matching replacementCopy the code
4. Scope of selection
Sed -n '/= 0/,/ Max /p' SVNserve.conf #min-encryption =0 #max-encryption = 256 # All lines in the range =0 to Max will be printedCopy the code
5. Sed multi-point editing (-e)
[root@centos001 ~]#cat -n test 1 This is a test file 2 Welcome 3 to 4 here 5 Hello WORLD 6 7 Linux Centos6.8 8 redhat Sed -e '2,6d' -e 's/ Linux centos6.8/Linux centos6.8/ 'test this is a test file Linux centos6.8 Sed --expression=' /Linux centos6.8/Linux centos6.8/ '--expression=' /to/ to/' test** This is a test file Welcome TO Here Hello WORLD Linux CenTOs6.8 RedhatCopy the code
6. Read and write
[root@centos001 ~]#cat test1 welcom to here [root@centos001 ~]#sed '/here/r test1' test this is a test file welcome to Sed -n '/centos6.8/w test2' test [root@centos001 ~]#cat test2 Linux centos6.8 # write all rows matching test file to Centos6.8 to test2 file, file may not existCopy the code
Append and insert
[root@centos001 ~]#sed '/^l/a2017-08-08' test2 Linux centos6.8 2017-08-08 [root@centos001 ~]#sed '/^l/i2017-08-08' #sed '/^l/i2017-08-08 Test2 2017-08-08 Linux centos6.8 # insert 2017-08-08 ####### to match a line starting with l ################ [root@centos001 ~]#sed I '/^l/i2017-08-08' test2 [root@centos001 ~]#cat test2 2017-08-08 Linux centos6.8Copy the code
8. Other command examples
[root@centos001 ~]#cat -n test2 1 2017-08-08 2 Linux centos6.8 3 08 4 5 test [root@centos001 ~]#**sed '/08/{n; s/l/L/; }' test2 2017-08-08 Linux centos6.8 08 test # [root@centos001 ~]#sed '1,4y/8/9/' test2 2017-09-09 Linux centos6.9 09 test # replace all numbers 8 with 9 [root@centos001 ~]#**sed '1q' test2** 2017-08-08 #Copy the code
9. Print odd or even lines
[root@centos001 ~]#sed -n 'p; n' test2 20170808 08 [root@centos001 ~]#sed -n 'n; P 'test2 Linux centos6.8 test [root@centos001 ~]#sed -n '1~2p' test2 20170808 08 [root@centos001 ~]#sed -n '2~2p' test2 Linux centos6.8 testCopy the code
Print the next line of the matching string
[root@centos001 ~]#sed -n '/linux/{n; p}' test2 08 [root@centos001 ~]#awk '/linux/{getline; print}' test2 08Copy the code
One Linux command per day (1) : xargs