Linux shell online text editing sed

Source: Internet
Author: User

sed command file editing

sed is a text editing command that reads file data through a terminal into a buffer, then edits text through SED, and in the output to the specified file, sed is a stream editor, which is a very useful tool in text processing, perfect for use with regular expressions, and features are extraordinary. When processing, the currently processed rows are stored in a temporary buffer called pattern space, followed by the SED command to process the contents of the buffer, and after processing is done, the contents of the buffer are sent to the screen. Then the next line is processed, so it repeats until the end of the file. The file content does not change unless you use redirection to store the output. SED is mainly used to automatically edit one or more files, to simplify the repeated operation of the file, to write the conversion program and so on.

First of all, we see a bunch of strings, this is a command that has multiple sed command expressions combined, | Represents a combination of multiple expressions, \ denotes a newline, the s in the colon means substitution, and g means replace all; the first input parameter is a file name, and the first line means to read the data in the file. Replace all ivms_service=0 with ivms_service=1, this is just the character substitution, the second line is to replace the variable $svc_prog all Ivms_service_prog, the fourth line is replaced first, and then the data in the buffer is entered into the variable $svc _sys_file the marked file;

# Create service Bash
Sed-e "S%ivms_service=0%ivms_service=1%g" | \
Sed-e "s%ivms_service_prog% $SVC _prog%g" | \
Sed-e "s%ivms_service_name% $SVC _name%g" | \
Sed-e "s%ivms_service_dir% $SVC _dir%g" > $SVC _sys_file

Options, commands, replacement tags for sed

Command format

sed [options] 'file (s) sed [options]-F scriptfile file (s)
Options
-e<script> or--EXPRESSION=<SCRIPT>: processes the input text file with the specified script in the options;-f<script file > or--file=<script file : Process the input text file with the script file specified in the option,-H or-help: Display assistance,-n or--quiet or--silent: Displays only the results of script processing;-V or--version: Displays version information. 
Parameters

File: Specifies the list of text files to be processed.

SED command
a\Inserts text below the current line.i\Inserts text above the current line.c\Changes the selected line to a new text.DDelete, delete the selected row.DDelete the first line of the template block.sReplace the specified characterhCopies the contents of a template block into an in-memory buffer.HAppends the contents of the template block to the in-memory buffer.gGets the contents of the memory buffer and overrides the text in the current template block.GGets the contents of the memory buffer and appends it to the current template block text.LList cannot print a list of characters.NReads the next input line, processing the new row with the next command instead of the first command.NAppends the next input line to the template block and embeds a new line between them, changing the current line number.PPrints the line of the template block.P(uppercase) The first line of the print template block.QExit sed.b lableBranch to the place marked in the script and branch to the end of the script if the branch does not exist.r fileReads rows from file.T labelIf branch, starting with the last line, the condition satisfies or t,t the command, causing the branch to be at the command with the label, or at the end of the script.T LabelThe wrong branch, starting with the last line, will cause the branch to be at the command with a label, or at the end of the script, once an error or T,T command occurs.W fileWrite and append the template block to the end of file.W FileWrites and appends the first line of the template block to the end of file.! Indicates that the subsequent command has effect on all rows that are not selected.= Prints the current line number.#Extends annotations to the next line break.
SED replacement flag
Represents a full-line replacement within a row.  represents the print line.   W means writing the line to a file. Represents the interchange of text in  a template block and the text in a buffer.   indicates that a character is translated to another character (but not for a regular expression)  substring matching token  matched string token 
Sed meta Character set
The match line starts, such as:/^sed/matches all lines that begin with SED. The $ match line ends, such as:/sed$/matches all lines ending in sed.  any character that matches a non-line break, such as:/s.d/matches S followed by an arbitrary character, and finally D.  match 0 or more characters, such as:/*sed/match all the templates are one or more spaces followed by the SED line.  matches a specified range of characters, such as/[SS]ed/to match sed and sed.  matches a character that is not within the specified range, such as:/[^a-rt-z]ed/matches the beginning of a letter that does not contain a-r and t-z, followed by the line of Ed.   match substrings, save matching characters such as s/\ (love\) able/\1rs,loveable are replaced with lovers.  Saving search characters is used to replace other characters, such as S/love/**&**/,love, which becomes **love**.  matches the beginning of a word, such as:/\<love/matches a line containing a word that begins with love.  matches the end of a word, such as/love\>/, that matches a line containing a word ending in love.  repeat characters x,m times, such as:/0\{5\}/matches rows that contain 5 0.  Repeat the character X, at least m times, such as:/0\{5,\}/matches at least 5 rows of 0.  Repeat the character X, at least m times, not more than n times, such as:/0\{5,10\}/matches the line of 5~10 0. 
Sed usage Instance substitution operation: s command

Replace the string in the text:

Sed 's/book/books/' file

The- n option is used together with the p command to print only those rows where the substitution occurred:

SED- n ' s/test/test/p ' file

The direct Edit file option-I, which matches the first book of each row in the files file, is replaced with books:

-I ' s/book/books/g ' file
Full Replacement Mark G

Using the suffix/g tag replaces all matches in each row:

Sed ' s/book/books/g ' File

You can use/ng when you need to start replacing from Nth match:

echo Sksksksksksk | Sed ' s/sk/sk/2g ' Skskskskskskecho sksksksksksk | sed ' s/sk/sk/3g ' skskskskskskecho sksksksksksk | sed ' s/sk/sk/ 4g ' Sksksksksksk  
Delimiter

The characters in the above command are used as delimiters in sed or any delimiter can be used:

Sed ' s:test:text:g ' sed ' s|test| Text|g '

When delimiters appear inside a style, they need to be escaped:

Sed ' s/\/bin/\/usr\/local\/bin/g '
Delete operation: D command

To delete a blank line:

Sed '/^$/d ' file

Delete the 2nd line of the file:

Sed '2d ' file

Delete all lines from line 2nd to the end of the file:

Sed '2, $d ' file

Delete the last line of the file:

Sed '$d ' file

Delete all lines in the file that begin with test:

Sed '/^test/'d file 
Matched string Markers &

The regular expression \w\+ matches each word, using [&] to replace it,& corresponds to the previously matched word:

Echo this was a Test line | Sed ' s/\w\+/[&]/g ' [This] [was] [a] [test] [line]

All lines starting with 192.168.0.1 will be replaced with a self-added localhost:

Sed ' s/^192.168.0.1/&localhost/' File192.168.0.1localhost
SUBSTRING matching tag \1

Matches part of a given style:

echo this was digit 7 in a number | Sed ' s/digit \ ([0-9]\)/\1/' This was 7 in a number

The command digit 7, which was replaced by 7. The substring to which the style matches is 7,\ (.. \) is used to match substrings, the first substring that matches to is marked as \1, and so on the second result that matches to \2, for example:

echo AAA BBB | Sed ' s/\ ([a-z]\+\) \ ([a-z]\+\)/\1/' BBB AAA

Love is labeled as 1 and all loveable will be replaced with lovers and printed out:

Sed-n ' s/\ (love\) able/\1rs/p ' file
Combining multiple expressions
Sed ' expression ' | Sed ' expression ' is equivalent to: sed ' expression ; expression '
Reference

sed expressions can be referenced using single quotation marks, but double quotes are required if the expression contains variable strings inside.

"S/$test/hello" HELLO World
Range of selected rows:, (comma)

All lines that are within the range determined by the template test and check are printed:

Sed-n '/test/,/check/p ' file

Prints all lines starting from line 5th to the first one that contains the line starting with test:

Sed-n ' 5,/^test/p ' file

For the row between the template test and West, the end of each line is replaced with the string AAA BBB:

Sed '/test/,/west/s/$/aaa bbb/' file
Multi-point editing: E command

The-e option allows multiple commands to be executed on the same line:

-E ' s/test/check/' file

The first command of the SED expression above deletes rows 1 through 5, and the second command replaces test with a check. The order in which the commands are executed has an effect on the results. If all two commands are replacement commands, the first substitution command affects the result of the second replacement command.

And-e equivalent commands are--expression:

--expression= '/love/d ' file
Read from File: R command

The contents of file are read in and displayed after the line matching the test, and if multiple rows are matched, the contents of file are displayed below all matching rows:

Sed '/test/r file ' filename
Write file: w command

All rows containing test in example are written to file:

Sed-n '/test/w file ' example
Append (line): A\ command

Append this was a test line to the row that begins with test:

Sed '/^test/a\this is a test line ' file

After the 2nd line of the test.conf file, insert this is a test lines:

Sed-i ' 2a\this is a test line ' test.conf
Insert (on line): I\ command

Append this was a test line to the front of the row that begins with test:

Sed '/^test/i\this is a test line ' file

Before the 5th line of the test.conf file, insert this is a test lines:

Sed-i ' 5i\this is a test line ' test.conf
Next: N command

If test is matched, move to the next line of the matching line, replace the AA for this line, change to BB, and print the line, and then continue:

N s/aa/bb/; } ' file
Deform: Y command

Turn all ABCDE in 1~10 into uppercase, note that regular expression metacharacters cannot use this command:

Sed ' 1,10y/abcde/abcde/' file
Exit: Q command

After you finish printing line 10th, exit SED

Sed ' tenq ' file
Hold and get: H command and G command

When the SED processes the file, each row is stored in a temporary buffer called the pattern space, unless the row is deleted or the output is canceled, otherwise all processed rows will be printed on the screen. The pattern space is then emptied and a new line is stored for processing.

Sed-e '/test/h '-e '$G ' file 

In this example, when a row matching test is found, it is stored in the pattern space, and the H command copies it into a special buffer called the hold cache. The second statement means that when the last line is reached, the G command takes out the row holding the buffer, then puts it back into the pattern space and appends it to the end of the line that already exists in the pattern space. In this example, it is appended to the last line. Simply put, any line containing test is copied and appended to the end of the file.

Hold and Swap: H command and x command

Swaps the pattern space and preserves the contents of the buffer. That is, the line that contains test and check is swapped:

Sed-e '/test/h '-E '/check/x ' file 
Script ScriptFile

The SED script is a list of SED commands that boot the script file name with the-F option when you start sed. Sed is very picky about the commands entered in the script, and cannot have any whitespace or text at the end of the command, separated by semicolons if there are multiple commands in a row. The behavior that begins with # comments lines, and cannot span rows.

-F ScriptFile file (s)
Print odd or even rows

Method 1:

Sed-n 'p;n ' test.txt  #奇数行sed-n 'n;p '  test.txt #偶数行

Method 2:

Sed-n '1~2p ' test.txt  #奇数行sed-n '2~2p '  test.txt #偶数行
Print the next line matching a string
Grep-a 1 SCC urfilesed-n '/scc/{n;p} ' urfileawk '/scc/{getline; print} ' Urfile


Article Source: http://man.linuxde.net/sed

Linux shell online text editing sed

Related Article

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.