The use of SED

Source: Internet
Author: User

1, what is the sedsed command is a streamlined, non-interactive editor, you can achieve in the VI and other editors in the same editing effect.
2. SED working principle mode space (pattern spaces) SED processes one line of text (or input) at a time, sending the output to the screen or redirecting it to a file. SED stores the currently processed rows in a temporary buffer, known as pattern space. Once the row in the pattern space is processed, that is, the SED command in this line is executed, the lines in the pattern space are sent to the screen (unless you delete the line command or print to the printer). After the line is processed, it is moved out of the mode space, and the program reads into the next line, processing, displaying, moving out ... The SED ends until the last line of the file finishes processing.
The keep buffers (holding buffer) H command copies the rows in the pattern space into a special buffer called the hold buffer. The G command puts the rows in the buffer back into the pattern space and appends to the end of the line already in the pattern space. The G command places the rows in the buffer back into the pattern space and replaces the existing rows in the pattern space. The x command swaps the pattern space and preserves the contents of the buffer.
Note: The hold buffer can only hold one row at a time. 3, addressing through addressing to determine which line to be edited. Can be addressed by means of numbers, regular expressions, or a combination of the two. In the case of no addressing, sed processes all the lines of the input file. $sed ' 1,3d ' myfile$sed-n '/john/p ' 4, Commands and Options command: a\Adds a line or text after the current line i\Insert a row before the current line DDelete a matching row from the pattern space pPrint the row R file that matches the templateRead line h from fileCopy the contents of the template block to the in-memory buffer gThe command puts the rows in the buffer back into the pattern space and appends to the end of the line already in the pattern space gThe command places the rows in the buffer back into the pattern space and replaces the existing rows in the pattern space QExit sed!Indicates that the subsequent command has no effect on all rows that are not selected s/re/stringReplace string with the regular expression re matched by string
Replacement tag: gAll-in-line replacement WWrite file
Option:-e commandAllow multi-point editing--expression commandAllow multi-point editing-NCancel default Output
5. SED-supported special meta-characters &Save search characters to replace other characterss/love/&/Replacing love with Love6, sed script is a file containing the SED command manifest, and the advantage is that you don't have to worry about interacting with the shell command line, and you don't need to reference the SED command to prevent it from being interpreted by the shell. Sed is very picky about the commands entered in the script and cannot have any whitespace or text at the end of the command. $sed-F script.sh Datafile7, instance 1) P command $sed-n '/north/p ' datafileThe-N and P commands are used in a common piece to print only the rows that match the template, canceling the behavior of all rows in the default print mode space
2) Delete: D command $sed ' 3d ' datafileDelete line 3rd $sed ' 3,$ ' datafileDelete 3rd to last row
3) Replace: s command $sed ' s/west/north/g ' datafileIf there is no G flag, only the 1th matching $sed-n ' S/WEST/NORTH/GP ' datafile is replaced.The-N and P commands only print the line where the substitution occurred $sed ' s/[0-9][0-9]$/&.5/' datafile& represents the part found in the replacement string $sed-n ' S/\ (mar\) got/\1ianne/p ' datafileTagged $sed ' s#west#east#g 'The character following S is the character separating the search string from the replacement string, and the default is/, but in the case of the S command it can be changed, no matter what character follows the S command is considered a new delimiter.
4) Range of selected lines:, $sed-n '/west/,/east/p ' datafilePrints a row between West and east. If West is not found, no rows are printed, and if East is not found, all rows from west to the end are printed. $sed '/west/,/east/s/$/var*/' datafile
5) Multipoint edit: E command $sed-e ' 1,3d '-e ' s/hemenway/jones/' datafileMultiple commands are executed on the same line, and the order in which the commands are executed affects the result of the command. $sed--expression= ' s/tb/tobias/'--expression= '/north/d ' datafile
6) read from file: R command $sed '/suan/r newfile ' datafileContent that is read from a file is displayed after all matching rows
7) Write file: w command $sed-n '/north/w newfile ' datafileWrites a row of the template to a file
8) Append command: A command $sed-n '/^north/a\----->the North Sales Distribute have moved<------------' datafile
$sed-n '/^north/a\----->the North Sales Distribute have moved<------------\----->the North Sales Distribute have M oved<------------' datafileAppend multiple lines, except for the last line, each line needs to add \
9) Insert: I command $sed-n '/^north/i\----->the North Sales Distribute have moved<------------' datafile
$sed-n '/^north/i\----->the North Sales Distribute have moved<------------\----->the North Sales Distribute have M oved<------------' datafileInsert multiple rows before matching rows, except for the last row, each line needs to be added \
10) Next: n command $sed '/eastern/{n; s/am/archie/;} ' datafile
11) Variant: Y command $sed ' 1,3y/abcdefg/abcdefg/' datafileConvert A-g to uppercase, the addressable part can use regular expressions
12) Exit: Q command $sed ' 5q ' datafileExit $sed '/lewis/{s/lewis/jooseph/;q} after printing line 5th{} Executes multiple commands, semicolon delimited, can be nested
13) Hold and get: H command, g command, G command $sed-e '/northeast/h '-e ' $G ' datafileAppend rows containing northeast to the last line of the file $sed-e '/we/{h, D;} '-E '/ct/{g;} ' datafileMove the line containing we to the back of the line containing the CT $sed-e '/northeast/h '-e ' $g ' datafileReplace the last line of the file with a row with northeast
14) Maintain and interchange $sed-e '/particia/h '-e '/margot/x ' datafileRows that contain Margot are replaced with rows containing Particia.
15) Other instances find lines that contain strings #sed-n '/^vmm_read_interval/p ' log2.txt >avg2.txt//-n and/p commands are used in general, only the rows found are printed
Delete blank line #sed '/^$/d ' file
Replace the found content with #sed ' s/^[1-9]/\n&/g ' 2.srt>3.srt//Add a blank line before the line that starts with a number #sed ' s/[0-9][0-9]$/&.5/' datafile/double-digit End line Plus. 5#sed ' s/address/&#/g '/etc/network/interfaces//After all address add # #sed ' s/:.*[0-9] $g ' Replace the colon and the number after the colon with an empty note Meaning: 1) & symbol replaces the part found in the string, not the entire row of the found Part 2)/g indicates that the command is scoped to the entire line, that is, the action is performed on all the found content in a row, or only for the first found content
Multipoint edit #sed-e ' s/address/#&/'-e ' s/netmask/#&/'-e ' s/gateway/#&/g '-e ' s/^##//g ' $NET _config_file.bak >$ Net_config_file//First add a # to all Address,netmask,gateway words, then remove the two # from the line starting with two #, and then execute in turn.
Merge two lines 1) first with a special string tag at the beginning of each line, for example, BBBB2) to remove all carriage returns with a special string token (for example, AAAA3) at the end of the preceding line of rows that need to be merged, and then all rows are composited in a row. There is a aaaabbbb tag between the rows that really need to be merged, there is a BBBB Mark 4 in place of the line break, and all aaaabbbb are replaced with empty 5) all BBBB are replaced with ' \ n ' specific programs as follows: SED ' 1, $s/^/bbbb/g ' $1.1 >$ 1.2sed '/[^. )? ”! 〗]$/s/$/aaaa/' $1.2 >$1.3sed-n ' 1, $p ' $1.3|tr-d ' \ n ' >$1.4sed ' s/aaaabbbb//g ' $1.4>$1.5sed ' s/bbbb/\n/g ' $. 5>$1.6sed ' s/aaaa//g ' $1.6&GT;$1.7CP $1.7 $1.txtecho "finished." RM $. [0-9]*
Divide Chinese and English subtitle files into two subtitle files $sed '/^[0-9][0-9]:/{n;d} ' 1.srt >2.srt$sed '/^[0-9][0-9]:/{n;n;d} ' 1.srt >3.srt

=-=-=-=-=
Powered by Blogilo

The use of SED

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.