Stream editor sed uses the summary and uses sed to extract strings from text. The editor sed

Source: Internet
Author: User

Stream editor sed uses the summary and uses sed to extract strings from text. The editor sed

Sed is an editor, but it is different from most other editors. In addition to not facing the screen, it is still non-interactive, and our commonly used vim editor is interactive.

This means that the command to be executed on the data must be inserted into the command line or the script to be processed. Sed is non-interactive in a file (or file set) and receives and executes a series of commands without asking. Therefore, it flows through the text just like a stream passing through a stream. Therefore, sed properly represents a stream editor. You can delete, replace, insert, and append files or file sets in batches.

The stream editor is very suitable for executing repeated edits, which takes a lot of time if manually completed.

Its parameters may be as limited as the parameters required to use a simple operation at a time, or as complex as a script file with thousands of lines to edit and modify.

This is the shortcoming of our commonly used interactive editor VIM. Therefore, it is very practical to combine the two!

Sed is one of the most useful tools in Linux and UNIX toolbox, and has very few parameters.

Sed reads a row from the input (this input can be a pipe or file), matches the data with the provided editing command, and modifies the data in the stream in the way specified in the command, then the generated data is output to STDOUT. After the stream editor matches all commands with one row of data, it will read the next row of data and repeat this process. After the stream editor processes all data rows in the stream, it stops.

Sed command line format:
Sed [-nefri] 'command' input text

Common options:
-N: Use silent mode. The default processing result of sed is output to STDOUT. However, if the-n parameter is added, only the row (or action) that has been specially processed by sed will be listed.
-E: directly edit the sed action in the Command column mode (multiple operation commands can be executed simultaneously );
-F: Write the sed action directly in a file.-f filename can execute the sed action in filename;
-R: The sed Action Command is an extended regular expression. (The default is the basic regular expression)
-I: directly modify the Read File Content, instead of STDOUT output.

Common commands:
A: new. a can be followed by strings. These strings will appear in the new row (the next row currently)
C: replace. c can be followed by strings. These strings can replace rows between n1 and n2!
D: Delete the row;
I: insert, I can be followed by strings, and these strings will appear in the new line (the previous line currently );
P: print the selected data. It can be used for search. sed-n'/search string/P' can be used to search for strings, similar to grep. Usually p will work with the sed-n parameter.
S: replace, directly replace the work, usually this s action can be combined with a regular expression

The following describes how to use sed.

1) Define and edit commands in the command line
$ Echo "This is a test" | sed's/test/big test /'
This is a big test
 
$ Cat data1
The quick brown fox jumps over the lazy dog.
The quick brown fox jumps over the lazy dog.
The quick brown fox jumps over the lazy dog.
The quick brown fox jumps over the lazy dog.
 
$ Sed's/dog/cat/'data1
The quick brown fox jumps over the lazy cat.
The quick brown fox jumps over the lazy cat.
The quick brown fox jumps over the lazy cat.
The quick brown fox jumps over the lazy cat.
2) use multiple editor commands on the command line (use the-e Option)
$ Sed-e's/brown/green/; s/dog/cat/'data1
The quick green fox jumps over the lazy cat.
The quick green fox jumps over the lazy cat.
The quick green fox jumps over the lazy cat.
The quick green fox jumps over the lazy cat.
3) read the editor command from the file (use the-f option)
$ Cat script1.sed
S/brown/green/
S/fox/elephant/
S/dog/cat/
 
$ Sed-f script1.sed data1
The quick green elephant jumps over the lazy cat.
The quick green elephant jumps over the lazy cat.
The quick green elephant jumps over the lazy cat.
The quick green elephant jumps over the lazy cat.

4) Replacement mark
Note that the substitute command replaces the matching mode in the string, that is, it only replaces the first place in each line by default. To enable the replacement command to take effect for the text that appears in different places in a line, you must use the replacement flag)

S/pattern/repacement/flags

1> Number, indicating the place where the new text matches the pattern;
2> g, indicating that the new text will replace all existing text;
3> p, indicating that the content of the original row should be printed;
4> w file: Write the result of replacing the original line to the file.
 
$ Echo "This is a test" | sed-n's/test/big test/P'
This is a big test
Note: The-n option will disable sed editor output, but the p replacement mark will output modified lines. If the two are combined, only modified lines will be output.
 
5) replace characters
$ Sed's/\ bin \/bash/\/bin \/csh/'/etc/passwd
Sed editor can use exclamation marks as separators to make the path name easier to read and understand.
$ Sed's! /Bin/bash! /Bin/cash! '/Etc/passwd
 
6) Address used
By default, the commands used in the sed editor act on all lines of text data. If you only want to act on a specific row or some rows, you must use line addressing ).
There are two forms of row addressing in the sed Editor:
1. the number range of the row;
2. A line is filtered out in text mode.
Both formats use the same format to specify the address:
[Address] command
You can also put multiple commands together for a specific address:
Address {
Command1
Command2
Command3
}
1> digital row addressing
$ Sed '2s/dog/cat/'data1
The quick brown fox jumps over the lazy dog.
The quick brown fox jumps over the lazy cat.
The quick brown fox jumps over the lazy dog.
The quick brown fox jumps over the lazy dog.
 
$ Sed '2, 3 s/dog/cat/'data1
The quick brown fox jumps over the lazy dog.
The quick brown fox jumps over the lazy cat.
The quick brown fox jumps over the lazy cat.
The quick brown fox jumps over the lazy dog.
 
$ Sed '2, $ s/dog/cat/'data1
The quick brown fox jumps over the lazy dog.
The quick brown fox jumps over the lazy cat.
The quick brown fox jumps over the lazy cat.
The quick brown fox jumps over the lazy cat.
2> use text filter
The format is as follows:
/Pattern/command
$ Echo "This is a test" | sed '/test/s/test/TEST /'
This is a TEST
3> combined commands
To execute multiple commands on a single line, you can use curly brackets to combine multiple commands.
$ Sed '2 {
> S/fox/elelhant/
> S/dog/cat/
>} 'Data1
The quick brown fox jumps over the lazy dog.
The quick brown elelhant jumps over the lazy cat.
The quick brown fox jumps over the lazy dog.
The quick brown fox jumps over the lazy dog.
7) delete a row
$ Cat data2
Line1
Line2
Line3
Line4
 
$ Sed '3d 'data2
Line1
Line2
Line4
You can delete rows in a certain range in two text modes, but be careful when doing so. The row deletion function is enabled in the first mode you specified, and the row deletion function is disabled in the second mode. Sed editor deletes all rows (including specified rows) between two specified rows)
$ Sed '/1/,/3/d' data2
Line4
If the delete function is enabled but not disabled, all data is deleted.
$ Sed '/1/,/5/d' data2
8) insert and add text
1 insert command I will add a new line before the specified row;
2. append Command a adds a new line after the specified line.
These two commands cannot be used on a single command line. You must specify whether to insert a row or append it to another row. The format is as follows:
Sed '[address] command \
New Line'
The text in the new line will appear in the position you specified in the sed editor output. Remember, when you use an insert command, the text will appear before the data stream text, and when you use an additional command, the text will appear after the data stream text:
$ Echo "Test line 2" | sed 'I \ Test line 1'
Test line 1
Test line 2
 
$ Echo "Test line 2" | sed 'a \ Test line 1'
Test line 2
Test line 1
9) Modify rows
The change command allows you to modify the content of the entire line of text in a data stream. It works like inserting and appending commands. You must specify a new line in the sed command.
$ Sed '3c \ This is a changed line'./data2
Line1
Line2
This is a changed line
Line4
You can also use the address range in the modify command, but the result may not be what you want (because it includes the row where the range is located ):
$ Sed '2, 3c \ This is a changed line'./data2
Line1
This is a changed line
Line4
10) Conversion command
The transform (y) command is the only sed edit command that can process a single character. The conversion command format is as follows:
[Address] y/inchars/outchars/
The conversion command maps inchars and outchars values one to one.
$ Sed 'y/1234/6789/'data2
Line6
Line7
Line8
Line9
The conversion command is global, that is, it automatically replaces all instances of the specified characters found in the text line,
$ Echo "this 1 is a test of 1 try." | sed 'y/123/456 /'
This 4 is a test of 4 try.
11) Review Printing
Here are three commands that can be used to print information in the data stream:
1> the lower-case p command is used to print text lines;
2> the equal sign (=) command is used to print the equal sign;
3> the lower-case l command is used to list rows.
$ Echo "This is a test" | sed 'P'
This is a test
This is a test
 
$ Sed '= 'data1
1
The quick brown fox jumps over the lazy dog.
2
The quick brown fox jumps over the lazy dog.
3
The quick brown fox jumps over the lazy dog.
4
The quick brown fox jumps over the lazy dog.


 
The above describes some basic usage methods of sed. The following is a practical example of a problem I encountered!

Now, you need to parse the commandline of the kernel after the kernel starts to obtain network parameters such as the ip netmask.

First view/proc/cmdline, as shown below:

Console = ttyS0, 115200 rdinit =/sbin/init ipaddr = 10.0.14.143 netmask = 255.255.255.252 gw = 10.0.12.1

Of course, you can write a program for string analysis. This is the most conservative and stupid method... You need to write string Analysis on your own.

It is better to solve the problem in the simplest way.

Use sed in the script to implement the following:

#!/bin/baship=`sed -n 's/ /\n/gp' /proc/cmdline | sed -n '/ipaddr/p' | sed -n 's/ipaddr=//gp'`netmask=`sed -n 's/ /\n/gp' /proc/cmdline | sed -n '/netmask/p' | sed -n 's/netmask=//gp'`gw=`sed -n 's/ /\n/gp' /proc/cmdline | sed -n '/gw/p' | sed -n 's/gw=//gp'`if [ "$ip" != "" -a "$netmask" != "" ]; then    ifconfig eth0 $ip netmask $netmaskfiif [ "$gw" != "" ]; then    route add default gw $gw fi
This script can be used to extract and use the ipaddr netmask gw string.



When a newbie uses shell, sed is used to extract the string (all numbers) from the text. I want to assign it to Variable n,

N = 'sed *** | awk '{printf "% d", $0} ''---- sed is yours...

Replacing strings in text with shell sed in linux

Sed 'n'; s/Hour \ N/Hour/'tt.txt

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.