Linux sed for personal use

Source: Internet
Author: User

First, SED

1. Replace the nth (if any) of each line with a match


sed "s/regexpr/anyword/${n}" filename

Cat filename
111111111111111111
222222222222222222
333333333333333333
444444444444444444
Example
Sed "s/4/four/8" filename
111111111111111111
222222222222222222
333333333333333333
4.,444,444,444,444,44e,+17

2. Replace the first match of each line

[ - ]

CODE:

Sed ' s/regexpr/anyword/' filename
Sed ' s/regexpr/anyword/1 ' filename


Example:

QUOTE:

Cat filename
1234567890 2345678901
3456789012 4567890123
Sed ' s/5/five/' filename
1,234,567,890 2345678901
3,456,789,012 4567890123

3. Line number Processing

1> Add a line number to a file

CODE:

sed = filename|sed ' n;s/\n/:/'
Cat filename
111111111111111111
222222222222222222
333333333333333333
444444444444444444


Example

[ - ]

CODE:

sed = filename|sed ' n;s/\n/:/' filename
1:1.,111,111,111,111,11e,+17
2:2.,222,222,222,222,22e,+17
3:3.,333,333,333,333,33e,+17
4:4.,444,444,444,444,44e,+17

2> Line numbers only for body lines in a file

[ - ]

CODE:

Cat filename
111111111111111111
< BR style= "Background-color:inherit;" >222222222222222222
333333333333333333

444444444444444444

sed/./= a|sed '/./n;s/\n/:/' filename
1:111111111111111111

3:222222222222222222
4:333333333333333333

6:444444444444444444

3>. String rollover

[ - ]

CODE:

Echo 1234567890|sed '/\n/! G;s/\ (. \) \ (. *\n\)/&\2\1/;//d;s/.//'
0987654321

#########################################################

#########################################################

Four. Selective output


1. Print document odd lines (interlaced output)

[ - ]

CODE:

Sed ' n;d '
Sed ' x;$! N;x '
Sed-n ' P;n '

1
3
5
7


2. Print even lines (interlaced output)

[ - ]

CODE:

Sed-n ' N;p '
Sed ' 1d;n;d; '
2
4
6
8


3. Delete consecutive duplicate rows (be careful when using the pattern space file too Large)

[ - ]

CODE:

Sed ' $! N /^\ (. *\) \n\1$/! P D
#使用 $! N Beware of memory overflow


Example

[ - ]

CODE:

Cat file
111111111111111111
222222222222222222
222222222222222222
333333333333333333
444444444444444444
444444444444444444
444444444444444444

sed" $! N /^\ (. *\) \n\1$/! P D ' filename
111111111111111111
333333333333333333
444444444444444444

4. Merging the upper and lower lines and separating them with spaces

[ - ]

CODE:

Cat file
1234567890
0987654321
After executing the command
1234567890 0987654321


5. Merge lines that end with the \ sign with the following line and separate them with spaces (punctuation breaks)

[ - ]

CODE:

Cat filename
1 111111111111111111\
2 222222222222222222
3 333333333333333333\
4 444444444444444444

SED-E: A-e '/\\$/n; s/\\\n//; Ta ' filename
1 111111111111111111 2 222222222222222222
3 333333333333333333 4 444444444444444444

6. Splicing Lines by keywords
If a row starts with =, merge to the previous line and replace = blank

[ - ]

CODE:

Cat file
111111111111111111
222222222222222222
=333333333333333333
444444444444444444

SED-E: A-e ' $! n;s/\n=//;ta '-e ' p;d ' filename
111111111111111111
222222222222222222 333333333333333333
444444444444444444


7. Output the next line that matches the row

[ - ]

CODE:

Cat filename
1 111111111111111111
2 222222222222222222
3 333333333333333333
4 444444444444444444

Sed-n '/^3/{n;p;} ' filename
4 444444444444444444

8. Displays the line number of the matching row and outputs the upstream, matching row, and downstream of the matching row

Sed-n-E '/regexpr/{=;x;1!p;g;$! n;p;d;} ' -E H
Example

[ - ]

CODE:

Cat filename
1 111111111111111111
2 222222222222222222
3 333333333333333333
4 444444444444444444

sed-n-E '/^3/{=;x;1!p;g;$! n;p;d;} ' -E h  filename
3                                        # Line number of the matching line
2 222222222222222222   #上一行
3 333333333333333333   #匹配行
4 444444444444444444   #下一行

9. Delete the keyword matching line in a flag area in the document

Delete a row in a document that contains MyWord from being to end in a block

[ - ]

CODE:

Sed '/^begin/,/^end/{/myword/d;} ' filename




QUOTE:

Cat filename
MyWord
Begin
MyWord
number!
MyWord
number!
MyWord
number!
MyWord
number!
End
MyWord
number!


Test

QUOTE:

MyWord
Begin
number!
number!
number!
number!
End
MyWord
number!

Five. String parsing

1. Parse two substrings from a string (first 2 characters and 9 chars)

[ - ]

CODE:

echo "Welovechinaunix" |sed-e ' h;s/\ (.. \). */\1/;x;s/.*\ (. \{9\}\) $/\1/;x; g;s/\n//'
We Chinaunix


2. Decomposition Date string

[ - ]

CODE:

Echo 20030922|sed ' s/\ (..... \) \ (.. \)\(.. \)/\1 \2 \3/' |read year Month Day
echo $year $month $day
2003 09 22

######################################################################################

########################################################################################

Cat file
1 192.148.99.253 [17/jun/2003:11:25:44/sc 
2 192.148.99.253 [17/jun/2003:11:18:21/si 
1 192.148.99.253 [17/jun/ 2003:11:20:34/SP&NBSP;
2 192.148.99.253 [17/JUN/2003:11:18:13/AP&NBSP;
1 192.148.99.253 [17/jun/2003:11:17:30/hou/ 
1 192.93.108.187 [17/jun/ 2003:14:49:14/SC&NBSP;
3 192.93.108.187 [17/JUN/2003:14:39:11/SI&NBSP;
5 192.68.82.78   [05/jun/2003:00:05:45/hou/ 
9 192.68.82.78   &NBSP;[05/JUN/2003:00:05:45/SS&NBSP;
4 192.228.210.10 [16/jun/ 2003:09:29:30/hou/


D=6
To fetch the sixth line of the file, the
Single quotation marks

[ - ]

CODE:

Sed-n ' "$d" '/P ' file
1 192.93.108.187 [17/JUN/2003:14:49:14/SC


Double quotes

[ - ]

CODE:

Sed-n "$d" p file or sed-n "${d}p" file
1 192.93.108.187 [17/JUN/2003:14:49:14/SC


The result is exactly the same.
and
Take the 1th to 6th line of the file:
Single quotes:

[ - ]

CODE:

Sed-n ' 1, ' "$d" ' p ' file
1 192.148.99.253 [17/JUN/2003:11:25:44/SC
2 192.148.99.253 [17/jun/2003:11:18:21/si
1 192.148.99.253 [17/jun/2003:11:20:34/sp
2 192.148.99.253 [17/jun/2003:11:18:13/ap
1 192.148.99.253 [17/jun/2003:11:17:30/hou/
1 192.93.108.187 [17/JUN/2003:14:49:14/SC


Linux sed for personal use

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.