shell text processing --sed Editor Basics

Replacement options

1. Alternatively markers
when Alternatively, a case will be:

[root@ommleft zd]# more data4.txt
This is a test of the test script.
This is the second test of the test script.
[root@ommleft zd]# sed 's/test/trial/' data4.txt
This is a trial of the test script.
This is the second trial of the test script.

Only replace the case at the first match, to replace the text that appears in different places, you must use replacement tags.
Alternatively markers used as follows:
S / pattern / Replacement / the flags
there are four kinds of replacement tags are available:
- figures show that the new text replaces the local pattern matching of several

  • g, shows that the new text will replace all matching text
  • p, show that the original contents of the line to be printed out
  • w file, will replace the written document results

Specifying matching using a digital line position, as the second match line

[root@ommleft zd]# sed 's/test/trial/2' data4.txt
This is a test of the trial script.
This is the second test of the trial script.

Use g Match all cases, as follows:

[root@ommleft zd]# sed 's/test/trial/g' data4.txt
This is a trial of the trial script.
This is the second trial of the trial script.

Use p to print out the original line will feel less useful, for example as follows:

[root@ommleft zd]# sed 's/test/trial/p' data4.txt
This is a trial of the test script.
This is a trial of the test script.
This is the second trial of the test script.
This is the second trial of the test script.

Alternatively a plurality of markers may be used at the same time, the following simultaneously with g and p:

[root@ommleft zd]# sed 's/test/trial/gp' data4.txt
This is a trial of the trial script.
This is a trial of the trial script.
This is the second trial of the trial script.
This is the second trial of the trial script.

Use w file, the substitution result written to the file, as will be written to the substitution result in test.txt:

[root@ommleft zd]# sed 's/test/trial/w test.txt' data4.txt
This is a trial of the test script.
This is the second trial of the test script.

2. Replace the characters
often encounter some of the characters is not easy to use in the replacement mode, as usual slash when writing the script (/)
such as: sed 's // bin / bash // bin / csh / '/ etc / passwd
example above using escape character escaped be confusing, may be replaced in the command character delimiter other characters:

$ sed  's!/bin/bash!/bin/csh!'  /etc/passwd

In the above example, an exclamation point used as a separator, the path is more obvious.

Use Address

If the command acts on only some specific line or row, row addressing must be used (line addressing)
There are two types of row addressing in sed editing:

  • Express line intervals in digital form
  • Text mode filter travel
    Both forms use the same format to specify the address:
[address] command

Also a plurality of commands specific address can be grouped:

address {
    command1
    command2
    command3
}

1. The number of row addressing mode

[root@ommleft zd]# sed '2s/test/trail/' data4.txt
This is a test of the test script.
This is the second trail of the test script.

sed editor to modify only the second line of text specified address. Can also set the row address range, for example, modify the line 2,3

[root@ommleft zd]# more data1.txt
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.
The quick brown fox jumps over the lazy dog.
[root@ommleft zd]# sed '2,3s/dog/cat/' data1.txt 
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.
The quick brown fox jumps over the lazy dog.

You can modify all the rows from one line to the end of the text, and when I do not know how many lines of text when this method is very convenient to use:

[root@ommleft zd]# sed '3,$s/dog/cat/' data1.txt
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 cat.
The quick brown fox jumps over the lazy cat.
The quick brown fox jumps over the lazy cat.

2. text mode filters
may be used by a text character string pattern matching line direct replacement

[root@ommleft zd]# grep zternc /etc/passwd
zternc:x:1005:1006::/home/zte/PM:/bin/bash
[root@ommleft zd]# sed '/zternc/s/bash/csh/' /etc/passwd
zternc:x:1005:1006::/home/zte/PM:/bin/csh

3. The combination of commands
you need to perform multiple commands on a single line, can be combined with braces in terms of multiple commands.

[root@ommleft zd]# sed '2{
> s/test/trail/
> s/This/That/
> s/script/text/
> }' data4.txt
This is a test of the test script.
That is the second trail of the test text.

Or separated by a semicolon

[root@ommleft zd]# sed '2{s/test/trail/;s/This/That/;s/script/text/}' data4.txt
This is a test of the test script.
That is the second trail of the test text.

Delete Row

If you need to delete a particular line of text stream, you can use the Delete command d.

[root@ommleft zd]# more data2.txt
One line of test text.
Two line of test text.
Three line of test text.
[root@ommleft zd]# sed '2d' data2.txt
One line of test text.
Three line of test text.

In addition text mode matching method can also be used to delete

[root@ommleft zd]# sed '/Two/d' data2.txt 
One line of test text.
Three line of test text.

Delete rows may also skip, delete only the following first line and the third line

[root@ommleft zd]# sed '1d;3d' data2.txt
Two line of test text.

When using the note, sometimes not match does not complain, but has been deleted down as follows:

[root@ommleft zd]# sed '/One/,/Five/d' data2.txt
[root@ommleft zd]# 

In the above example, when matched to the "One", it will open the line delete function, after unable to find "Five", will always delete it, causing the output is empty

And insert additional text

sed editor allows want the data stream is inserted and additional text lines:
1) insert (insert) commands (i) adds a new row before the specified line;
2) additional (the append) command (a) to increase after a specified row a new line;
can be specified in the text before a row is inserted, as shown below

[root@ommleft zd]# sed '3i\This is an inserted line.' data2.txt
One line of test text.
Two line of test text.
This is an inserted line.
Three line of test text.

After a row or additional text, as follows:

[root@ommleft zd]# sed '3a\This is an appended line.' data2.txt
One line of test text.
Two line of test text.
Three line of test text.
This is an appended line.

If you want to use the following additional operations at the end of the text, without having to know the last line of the line number:

[root@ommleft zd]# sed '$a\This is an appended line.' data2.txt
One line of test text.
Two line of test text.
Three line of test text.
This is an appended line.

To insert or attach multiple lines of text must be used for each row for a backslash or insert additional new text, as follows:

[root@ommleft zd]# sed '1i\
> This is one line of new line\
> This is another line of new line.' data2.txt
This is one line of new line
This is another line of new line.
One line of test text.
Two line of test text.
Three line of test text.

Specifies two rows will be added to the data stream.

Modify the line

Modify (change) command allows the content data stream entire line of text. It is the same with inserts and additional working mechanisms need to specify the new line.

[root@ommleft zd]# sed '2c\Second line.' data2.txt
One line of test text.
Second line.
Three line of test text.

It also supports a text mode addressing, using the following text pattern to match the second row modifications:

[root@ommleft zd]# sed '/Two/c\This is Second line.' data2.txt
One line of test text.
This is Second line.
Three line of test text.

When using the modified address range, pay attention:

[root@ommleft zd]# more data1.txt
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.
The quick brown fox jumps over the lazy dog.
[root@ommleft zd]# sed '2,3c\This is changed.' data1.txt
The quick brown fox jumps over the lazy dog.
This is changed.
The quick brown fox jumps over the lazy dog.
The quick brown fox jumps over the lazy dog.

The modification of the second embodiment is not one by one and third rows, but the two lines used to modify the new line, when required attention.

Conversion command

Conversion (Transform) command (y) is the only process single character sed editor commands. Conversion command format is as follows:
[address] Y / inchars / outchars /
conversion command will inchars outchars value and one mapping. This mapping process will continue until the specified character processed.

[root@ommleft zd]# more data5.txt
This is line number 1.
This is line number 2.
This is line number 3.
This is line number 4.
This is line number 1 again.
This is yet another line.
This is the last line in the file.
[root@ommleft zd]# sed 'y/123/789/' data5.txt
This is line number 7.
This is line number 8.
This is line number 9.
This is line number 4.
This is line number 7 again.
This is yet another line.
This is the last line in the file.

When inchars outchars and length while being given:

[root@ommleft zd]# sed 'y/12/789/' data5.txt
sed:-e 表达式 #1,字符9:‘y'命令的字符串长度不同

print

sed command to print information output data stream:
. 1) command is used to print lines of text P
2), the equal sign (=) command to print line No.
3) l (lowercase L) command to list the line
1. Print line
used to print existing lines

[root@ommleft zd]# sed '/Two/p' data2.txt
One line of test text.
Two line of test text.
Two line of test text.
Three line of test text.

Normally match in text mode, used in conjunction with -n, hidden lines that are not relevant to show only the matching rows:

[root@ommleft zd]# sed -n '/Two/p' data2.txt
Two line of test text.

If you want to see the original lines before the replacement, you can use the following:

[root@ommleft zd]# sed -n '/text/{
> p
> s/line/String/p
> }' data2.txt
One line of test text.
One String of test text.
Two line of test text.
Two String of test text.
Three line of test text.
Three String of test text.

In the embodiment, to match the "text" in the row to be replaced, this way, can easily compare the text changes.
2. Print line number
may be (=) to print the line numbers, you can be viewed by matching text line number equal sign

[root@ommleft zd]# sed -n '/Two/{
> =
> p
> }' data2.txt
2
Two line of test text.

3. List row
lists (list) command (l) can be printed and text data stream of ASCII characters can not be printed.

[root@ommleft zd]# more data6.txt
THis	line	contains	tabs.
[root@ommleft zd]# sed -n 'l' data6.txt
THis\tline\tcontains\ttabs.$

Examples position using tab \ t displayed. Trailing dollar signs represent a newline character.

Processing file using sed

1. Write File
w command to write to the file line. The command format is as follows:
[address] W filename
filename can be relative or absolute path

[root@ommleft zd]# sed -n '1,3w /home/omm/zd/test3.txt' data3.txt
[root@ommleft zd]# more test3.txt
Line 1
Line 2
Line 3

If you want to create a data file through a common text, you can use this method to generate.
For example, you want to use the public file / etc / passwd root to generate all configured users

[root@ommleft zd]# sed -n '/^root/w root.txt' /etc/passwd
[root@ommleft zd]# more root.txt
root:x:0:0:root:/root:/bin/bash

2. The data read from the file
is read (Read) command (r) allows to insert a separate data file into the data stream
read command format is as follows:
[address] R & lt filename
filename parameter specifies the absolute file path of data or a relative path. sed editor will be inserted into the text in the file after the specified address.

[root@ommleft zd]# more data3.txt
Line 1
Line 2
Line 3
[root@ommleft zd]# more data2.txt
One line of test text.
Two line of test text.
Three line of test text.
[root@ommleft zd]# sed '/Line 2/r data2.txt' data3.txt
Line 1
Line 2
One line of test text.
Two line of test text.
Three line of test text.
Line 3

After data2.txt the embodiment will be inserted into the text matches the text line (Line 2), it may also be used of course sed '2r data2.txt' data3.txtbe inserted.
Read and delete data using the data to replace the text:

[root@ommleft zd]# more notice.std 
Would the following people:
List
please report to the ship's captain.
[root@ommleft zd]# more root.txt
root:x:0:0:root:/root:/bin/bash
[root@ommleft zd]# sed '/List/{
> r root.txt
> d
> }' notice.std
Would the following people:
root:x:0:0:root:/root:/bin/bash
please report to the ship's captain.

It can be seen using the command to read the file (r root.txt) and delete text command (d) the text "List" at the order to replace the contents of root.txt in here List played a placeholder role.

Published 75 original articles · won praise 7 · views 10000 +

Guess you like

Origin blog.csdn.net/zhengdong12345/article/details/101285146