Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Shell script sed command of the three Musketeers

2025-02-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Network Security >

Share

Shulou(Shulou.com)06/01 Report--

Sed:

Template:

The honeysuckle band played all night long for noly $90.

It was an evening of splendid music and company.

Too bad the disco floor fell through at 23:10.

The local nurse Miss P.Neaue was in attendance.

The sed options are as follows:

N does not print; sed does not write edit lines to standard output. The default is to print all lines (edited and unedited). The p command can be used to print edit lines. (key points)

C the next command is the edit command. Add this option when using multiple edits. This option is useless if only one sed command is used, but it doesn't matter if you specify it.

F use this option if you are calling the s e d script file. This option tells sed that a script file supports all sed commands, for example: sed-f myscript.sed input_file

Here myscript.sed is the file that supports the sed command.

How to use sed to locate text in a file:

XRX is an one-line number, such as 1.

XQuery y: indicates that the line number ranges from x to y, for example, 2Pol 5 means from line 2 to line 5.

/ pattern/: query contains rows of patterns. For example, / disk/ or / [amurz] /.

The / patern/:pattern/ query contains rows of two schemas. For example, / disk/disks/.

Pattern/,x: queries for rows that contain patterns on a given line number. Such as / ribbon/,3.

XQuery: query for matching rows by line number and pattern. 3./vdu/ .

XQuery: the query does not contain rows with the specified line numbers x and y. 1,2! .

Sed editing commands:

P print matching lines

= display file line number

A\ append new text information after locating the line number

I\ insert a new text message after locating the line number

D delete positioning line

C\ replace positioned text with new text

S replace the corresponding mode with the replacement mode

R read text from another file

W write text to a file

Q after the first pattern matching is completed, it will be launched or immediately launched.

L displays control characters equivalent to octal A S C I I codes

{} the group of commands executed on the location line

N read the next line of text from another file and append it to the next line

G paste mode 2 to / pattern n /

Y transfer character

N continues to the next input line; allows pattern matching statements across lines

Displays the specified line:

Example: [root@localhost sed] # sed '2p' data.f

The honeysuckle band played all night long for noly $90.

It was an evening of splendid music and company.

It was an evening of splendid music and company.

Too bad the disco floor fell through at 23:10.

The local nurse Miss P.Neaue was in attendance.

Example: [root@localhost sed] # sed-n '2p' data.f

It was an evening of splendid music and company.

Explanation: you cannot define the number of file lines if you don't add-n parsed. So all the information was printed.

Display range lines:

Example: [root@localhost sed] # sed-n '1Magi 3p' data.f

The honeysuckle band played all night long for noly $90.

It was an evening of splendid music and company.

Too bad the disco floor fell through at 23:10.

Explanation: displays 1 to 3 rows of data.

Display the line number:

[root@CMN-XN-4-3g1 yingyou] # sed'/ floor/=' test

The honeysuckle band played all night long for noly $90.

It was an evening of splendid music and company.

three

Too bad the disco floor fell through at 23:10.

The local nurse Miss P.Neaue was in attendance.

[root@CMN-XN-4-3g1 yingyou] # sed-n'/ floor/=' test

three

Explanation: without the-n option, all and corresponding line numbers are displayed.

Replace:

[root@CMN-XN-4-3g1 yingyou] # sed's /\ $/ / g 'test

The honeysuckle band played all night long for noly 90.

It was an evening of splendid music and company.

Too bad the disco floor fell through at 23:10.

The local nurse Miss P.Neaue was in attendance.

[root@CMN-XN-4-3g1 yingyou] # sed 's/in/ABCD/w 123.txt' test

The honeysuckle band played all night long for noly $90.

It was an evenABCDg of splendid music and company.

Too bad the disco floor fell through at 23:10.

The local nurse Miss P.Neaue was ABCD attendance.

[root@CMN-XN-4-3g1 yingyou] # cat 123.txt

It was an evenABCDg of splendid music and company.

The local nurse Miss P.Neaue was ABCD attendance.

Explanation: just save the results of the replacement to the file, not all of them.

[root@CMN-XN-4-3g1 yingyou] # sed-n 's/fell/ "abcd" & / p' test

Too bad the disco floor "abcd" fell through at 23:10.

[root@CMN-XN-4-3g1 yingyou] # sed-n 's/fell/abcd & / p' test

Too bad the disco floor abcd fell through at 23:10.

Explanation: insert the character you want to add before matching the pattern.

Write:

Root@localhost sed] # sed'/ bad/ w 456.txt' test

The honeysuckle band played all night long for noly $90.

It was an evening of splendid music and company.

Too bad the disco floor fell through at 23:10.

The local nurse Miss P.Neaue was in attendance.

[root@localhost sed] # cat 456.txt

Too bad the disco floor fell through at 23:10.

[root@localhost etc] # sed-n'14 named.conf' rndc.conf 24 w

[root@localhost etc] # cat named.conf

# Use with the following in named.conf, adjusting the allow list as needed:

# key "rndckey" {

# algorithm hmac-md5

# secret "Dqx3HzhRkD9X2cyL6pnLvQ=="

#}

#

# controls {

# inet 127.0.0.1 port 953

# allow {127.0.0.1;} keys {"rndckey";}

#}

# End of named.conf

Explanation: you can print the content of the line number, note that doing so will overwrite the original data.

[root@localhost sed] # sed'1 Magi 3 w 456.txt' test

The honeysuckle band played all night long for noly $90.

It was an evening of splendid music and company.

Too bad the disco floor fell through at 23:10.

The local nurse Miss P.Neaue was in attendance.

[root@localhost sed] # cat 456.txt

The honeysuckle band played all night long for noly $90.

It was an evening of splendid music and company.

Too bad the disco floor fell through at 23:10.

Explanation: can match regular content

[root@localhost sed] # sed'/ 2310. Test.

The honeysuckle band played all night long for noly $90.

It was an evening of splendid music and company.

Too bad the disco floor fell through at 23:10.

Abcdefg

The local nurse Miss P.Neaue was in attendance.

Explanation: use the r option to regularly match the contents of the 123.txt file under the line

Exit sed after pattern matching occurs for the first time

[root@localhost sed] # sed'/\. / Q' test

The honeysuckle band played all night long for noly $90.

[root@localhost sed] # sed'/ .a. * / Q 'test

The honeysuckle band played all night long for noly $90.

Explanation: suppose you want to look for "." in the test document. Or ".a. *" but use the Q parameter if you need to exit on the first match.

Comprehensive connection

[root@localhost sed] # cat test

The honeysuckle band played all night long for noly $90.

It was an evening of splendid music and company.

Too bad the disco floor fell through at 23:10.

The local nurse Miss P.Neaue was in attendance.

-

Fasdfds

Fsdfs

Safsdfdsfsdf

Sfsdfsd

[root@localhost sed] # sed's sed test test | sed'/ ^ $/ d' | sed'$d' | sed '1d'

It was an evening of splendid music and company.

Too bad the disco floor fell through at 23:10.

The local nurse Miss P.Neaue was in attendance.

Fasdfds

Fsdfs

Safsdfdsfsdf

The first command is to delete the dotted line, the second command is to delete the blank line, the third command is to delete the last line, and the fourth command is to delete the first line.

Remove the beginning of the line:

[root@localhost sed] # cat 123.txt

121 UDP_TCP

121 UDP_TCP

223 UDP_TCP

2 UDP_TCP

4324 UDP_TCP

32 UDP_TCP

543 UDP_TCP

534 UDP_TCP

654 UDP_TCP

654 UDP_TCP

[root@localhost sed] # sed's / ^ [0-9] * / / g '123.txt

UDP_TCP

UDP_TCP

UDP_TCP

UDP_TCP

UDP_TCP

UDP_TCP

UDP_TCP

UDP_TCP

UDP_TCP

UDP_TCP

Add the end of the line:

[root@localhost sed] # sed's / [0-9] * / & UDP/w 678.txt '456.txt

121 UDP

121 UDP

223 UDP

2 UDP

4324 UDP

32 UDP

543 UDP

534 UDP

654 UDP

654 UDP

[root@localhost sed] # cat 678.txt

121 UDP

121 UDP

223 UDP

2 UDP

4324 UDP

32 UDP

543 UDP

534 UDP

654 UDP

654 UDP

Pass a value from shell to sed:

[root@localhost sed] # echo $name | sed "s/the/$ABCD/g"

Too bad ABCD disco floor fell through at 23:10.

[root@localhost sed] # echo $name

Too bad the disco floor fell through at 23:10.

[root@localhost sed] # echo $ABCD

ABCD

Note: note that sed symbols should be in double quotation marks.

Set the shell variable from the sed output:

[root@localhost sed] # echo $name

Too bad the disco floor fell through at 23:10.

[root@localhost sed] # echo $ABCD

ABCD

[root@localhost sed] # NEW_NAME= `echo $name | sed "s/the/$ABCD/g" `

[root@localhost sed] # echo $NEW_NAME

Too bad ABCD disco floor fell through at 23:10.

's /\. $/ / g' delete the line ending with a period

'- e _ delete the row containing abcd

's / [] * / [] / g' delete more than one space and replace it with one space

's / ^ [] [] * / / g' delete the first space of the line

's /\. [] [] * / [] / g' delete the period followed by two or more spaces and replace it with a space

'/ ^ $/ d' Delete blank lines

's / ^. / / g' deletes the first character

's/COL\ (...\) / / g' deletes the last three letters immediately following COL

's/ ^\ / / / g' removes the first\ from the path

's / [] / [] / / g' remove all spaces and replace them with the t a b key

'S/ ^ [] / / g' delete all t a b keys at the beginning of the line

's / [] * / / g' remove all t a b keys

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Network Security

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report