In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
Today, I will talk to you about the basic usage of sed, which may not be well understood by many people. in order to make you understand better, the editor has summarized the following for you. I hope you can get something from this article.
Sed contains several functions, the most common of which is the replace command
1 replacement
S/pattern/replacement/flags
Flags includes the following
A number between 1-512 to replace the nth match.
G: globally modify all matches
P: all content in print mode
W:file to write the schema contents to the file file
For replacement, the following characters have a special meaning
&: replace it with pattern
\ n: match the nth string, specified with "\ (" and "\)" in pattern
\: escape character
& replaced by pattern, but\ & escaped
[oracle@ ~] $cat test
ORA
[oracle@ ~] $cat test | sed's Associates ORA Reilly\ & Associates, Inc./g'
O'Reilly & Associates, Inc.
[oracle@ ~] $cat test | sed's Associates ORA Reilly, Inc./g'
O'Reilly ORA Associates, Inc.
[oracle@ ~] $cat test1
See Section 19.40 See Section 18.89
See Section 19.09
See Section 07.10
[oracle@ ~] $sed 's/See Section [1-9] [0-9] *\. [1-9] [0-9] * / (&) / 2' test1-enclose "()" for the second matching character of each line
See Section 19.40 (See Section 18.89)
See Section 19.09
See Section 07.10
Replace the second See of each line with a newline character
[oracle@] $sed 's/See/\\ nUniverse 2' test1-- it doesn't work
See Section 19.40\ nSection 18.89
See Section 19.09
See Section 07.10
[oracle@ ~] $sed 's/See/\
> / 2' test1-the line breaks successfully, but it is not easy to maintain
See Section 19.40
Section 18.89
See Section 19.09
See Section 07.10
[oracle@ ~] $sed 's/See/\ nAccord 2' test1-use\ nsubstitute
See Section 19.40
Section 18.89
See Section 19.09
See Section 07.10
Search for the second matching pattern in each line and swap its first / second element
[oracle@] $sed's /\ (See Section\)\ ([1-9] [0-9]\. [1-9] [0-9]\) /\ 2\ 1Universe 2' test1
See Section 19.40 18.89 See Section
See Section 19.09
See Section 07.10
[oracle@ ~] $sed'/ 0 [1-9]\. / s /\ (See Section\)\ ([0-9] [0-9] *. [1-9] [0-9]\) / 2\ 1 A test1-first search contains "0 [1-9]." If it satisfies the matching pattern, its first / second element is swapped
See Section 19.40 See Section 18.89
See Section 19.09
07.10 See Section
[oracle@ ~] $cat test1 | grep UNIX
UNIX
[oracle@ ~] $sed 's/UNIX/\\ s Murray 1 &\\ s 0 UNIX' test1-use & replace the previous UNIX
\ s-1UNIX\ s0
[oracle@ ~] $sed 's/UNIX/\ s test1-use\ & make it a normal character, change\ s to\ s, and s is not a special character, so\ s equals s
Smuri 1 &\ s 0
[oracle@ ~] $sed 's/UNIX/s-1\ &\\ s0Universe' test1
Smuri 1 &\ s 0
Ignore case
[oracle@ ~] $cat test5
Who finger w last
[oracle@ ~] $sed's test5
Who www w last
2 Delete (d)
Delete the row if the row matches
/ ^ $/ d-Delete blank lines
[oracle@ ~] $cat test3
Adf
Sdf
[oracle@ ~] $sed'/ ^ $/ d'test3-- Delete blank lines
Adf
Sdf
[oracle@ ~] $sed'/. /! d 'test3-Delete blank lines,! Denotes notrecover.Compact! That is, lines without characters
Adf
Sdf
[oracle@ ~] $sed'/. / d' test3-Delete non-blank lines
[oracle@ ~] $sed'$d' test3-- Delete the last line
Adf
[oracle@ ~] $sed '2d' test3-Delete line 2
Adf
Sdf
[oracle@ ~] $sed'$! d 'test3-keep only the last row of data and delete the rest
Sdf
3 append (a) / insert (I) / change (c)
The format is as follows
[line-address] [a | I | c] text
Note: (O'Reilly) indicates that "these commands must be specified on multiple lines, (\ for escaping the end of the line)"
[line-address] [a | I | c]\
Text
But personal experiments have found that there is no need for so much trouble.
[oracle@ ~] $cat test3
Adf
Sdf
Add information before and after the first and last lines, respectively
[oracle@] $sed-e'1i insert before the first line'-e' $an append after the last line' test3
Insert before the first line
Adf
Sdf
Append after the last line
Using the change (c) command is sometimes more efficient than replacing, as follows:
Replace the input parameters for all lines starting with ".sp" with 5
[oracle@ ~] $cat test4
.sp 1.5
.sp 4
.sp
5.sp 67
[oracle@ ~] $sed'/ ^\ .sp/c\ .sp 5' test4-search for lines that start with .sp and replace the entire line with .sp 5
.sp 5
.sp 5
.sp 5
5.sp 67
4 list (l)
Displays the contents of the mode space, displaying non-print characters as two-digit ASCII codes, similar to vi's (: l) or cat-v
Can be used to detect invisible characters
Oracle@ ~] $cat-v test5
Who finger w last ^ M
^ M
^ M
[oracle@ ~] $sed-e "l" test5
Who finger w last\ r $
Who finger w last
\ r$
\ r$
[oracle@] $sed-n-e "l" test5
Who finger w last\ r $
\ r$
\ r$
The function of-n option is as follows:-n,-- quiet,-- silent suppress automatic printing of pattern space
5 next line (n)
Output the contents of the pattern space, and then read the next line of input without returning to the top of the script
[oracle@usuwsoadb06] $cat-n test7
1 apple
2 banana
3 mango
4 orange
5 strawberry
[oracle@usuwsoadb06 ~] $sed'/ ban/ {nscape d;} 'test7-- Delete the next line that matches the pattern
Apple
Banana
Orange
Strawberry
The difference between N and n, the former outputs the current matching line
[oracle@usuwsoadb06] $sed-n'/ ban/ {n; p;} 'test7
Mango
[oracle@usuwsoadb06 ~] $sed-n'/ ban/ {N; p;} 'test7
Banana
Mango
Look for the next line that contains ban characters and replace it with justin
[oracle@usuwsoadb06 ~] $sed'/ ban/ {n; s test7.
Apple
Banana
Justin-the original mango is now justin
Orange
Strawberry
6 read and write files (rcode)
Write a file
Read eligible lines from one file and write them to another file, with the following syntax:
Sed'/ pattern/w outputfile' inputfile
[oracle@] $cat-n test7
1 apple
2 banana
3 mango
4 orange
5 strawberry
[oracle@ ~] $sed-n'2 test8' test7 4w test8' test7-enter 2-4 lines of test7 into test8
[oracle@ ~] $cat test8
Banana
Mango
Orange
[oracle@ ~] $sed-n'/ mango/,$w test8' test7-writes test7 to test8 from the data containing the mango row to the last row
[oracle@ ~] $cat test8
Mango
Orange
Strawberry
[oracle@ ~] $sed-n'$! W test8' test7-write all the contents of test7 to test8 except the last line
[oracle@ ~] $cat test8
Apple
Banana
Mango
Orange
Read the file-it doesn't actually change the contents of the file.
Sed'/ pattern/r outputfile' inputfile
-- write the contents of the file to sed output
[oracle@ ~] $cat test9
1apple
1banana
1mango
[oracle@ ~] $cat test10
2orange
2strawberry
[oracle@ ~] $sed'2 test10' test9-insert the contents of the test10 file into test9 from line 2 to the last line
1apple
1banana
2orange
2strawberry
1mango
2orange
2strawberry
[oracle@ ~] $sed'$! r test10' test9- inserts test10 into test9 after the rest of the line except the last one
1apple
2orange
2strawberry
1banana
2orange
2strawberry
1mango
After reading the above, do you have any further understanding of the basic usage of sed? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.