sed extension

1. Print the content between a specific line and a certain line in the file.
For example, the content of a file test is as follows:
ert
fff

[abcfd]
123
324
444
[

rty ]
How can fgfgf intercept
[abcfd]
123
324
444
[rty]
Where does this part come out?

Answer: sed -n '/[abcfd]/,/[rty]/p' test

2. How sed converts uppercase and lowercase letters
In sed, use \u for uppercase and \l for lowercase

  1. Capitalize the first lowercase letter of each word:
    sed 's/\b[az]/\u&/g' filename

  2. Change all lowercase to uppercase:
    sed 's/[az]/\u&/g' filename

  3. Uppercase to lowercase:
    sed 's/[AZ]/\l&/g' filename

3. sed adds a number at the end of a line in the file
sed 's/(^a.*)/\1 12/' test

#cat test
askdj
aslkd aslkdjf3e
skdjfsdfj
sdkfjk
fsdkfjksdjfkjsdf
12sdfesdf
aslkdjfkasdjf asdlfkjaskdfj

#sed 's/(^a.*)/\1 12/' test

askdj 12
aslkd aslkdjf3e 12
skdjfsdfj
sdkfjk
fsdkfjksdjfkjsdf
12sdfesdf
aslkdjfkasdjf asdlfkjaskdfj 12

4. sed deletes the next line of a keyword to the last line
[root@test200 ~]# cat test
a
b
c
d
e
f
[root@test200 ~]# sed '/c/{p;:a;N;$! ba;d}' test
a
b
c

Define a label a, match c, and then N add the next line to the pattern space, and only exit the label loop when the last line is matched, and then command d to clear all the content in the pattern space.

if matches "c"
:a
appends the next line
if does not match "$"
goto a
finally exits the loop, the d command deletes.

From the above, we can delete the matching line and the next line after the matching line through sed
sed -i '/sample/{N;d}' filename

//sample is the matching character, N is the next line here, d is the delete

5. How to use sed to print 1 to 100 lines containing a certain string
The requirement is actually to match the specified line range of sed, which is rare. accomplish:

  1. sed -n '1,100{/abc/p}' 1.txt

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325176558&siteId=291194637
sed