sed extension

  1. print line to line content  http://ask.apelearn.com/question/559
例如:有个文件test的内容如下:
ert
fff
**
[abcfd]
123
324
444
[rty]
**
fgfgf
怎么能截取
 [abcfd]
123
324
 444
[rty]
 这一部分出来呢?
[root@linux-128 test]# sed -n  '/\[abcfd\]/,/\[rty\]/'p 1.txt
[abcfd]
123
324
444
[rty]
  • Note here: when matching [abcfd] first, you must first remove the [], and add -r to remove the meaning, use \ to remove the meaning;
[root@linux-128 test]# sed -n  '/\[abcfd\]/'p 1.txt
[abcfd

sed convert case  http://ask.apelearn.com/question/7758

  • In sed, use \u for uppercase and \l for lowercase
  1. Capitalize the first lowercase letter of each word:
[root@linux-128 test]# sed   's/\b[a-z]/\u&/g' 1.txt
Ert
Fff
**
[Abcfd]
123
324
444
[Rty]
**
Fgfgf
  • Note: \b: Indicates that \b is the nearest match, for example, \bAB matches A, AB\b matches B; \b can only match letters, numbers, Chinese characters, and underscores. & means the previous match
  1. Convert all lowercase to uppercase:
[root@linux-128 test]# sed   's/[a-z]/\u&/g' 1.txt
ERT
FFF
**
[ABCFD]
123
324
444
[RTY]
**
FGFGF
  1. Uppercase to lowercase:

[root@linux-128 test]# cat 2.txt
ERT
FFF
**
[ABCFD]
123
324
444
[RTY]
**
FGFGF


[root@linux-128 test]# sed 's/[A-Z]/\l&/'g 2.txt
ert
fff
**
[abcfd]
123
324
444
[rty]
**
fgfgf
  1. sed adds a number at the end of a line http://ask.apelearn.com/question/288
 [root@linux-128 test]# cat 3.txt
 askdj
 aslkd aslkdjf3e
 skdjfsdfj
 sdkfjk
 fsdkfjksdjfkjsdf
12sdfesdf
 aslkdjfkasdjf asdlfkjaskdfj
  1. Add the number 123 after all lines
[root@linux-128 test]# sed -r  's/(.*)/&123/'g 3.txt
askdj123
aslkd aslkdjf3e123
skdjfsdfj123
sdkfjk123
fsdkfjksdjfkjsdf123
12sdfesdf123
aslkdjfkasdjf asdlfkjaskdfj123
  1. Add the number 123 to the end of the line starting with a
[root@linux-128 test]# sed -r  's/(^a.*)/&123/'g 3.txt
askdj123
aslkd aslkdjf3e123
skdjfsdfj
sdkfjk
fsdkfjksdjfkjsdf
12sdfesdf
aslkdjfkasdjf asdlfkjaskdfj123
  1. Use \1 to represent the previous () content
[root@linux-128 test]# sed -r  's/(^a.*)/\1123/'g 3.txt
askdj123
aslkd aslkdjf3e123
skdjfsdfj
sdkfjk
fsdkfjksdjfkjsdf
12sdfesdf
aslkdjfkasdjf asdlfkjaskdfj123
  1. Delete a line to the last line  http://ask.apelearn.com/question/213
[root@linux-128 test]# cat 3.txt
askdj
aslkd aslkdjf3e
skdjfsdfj
sdkfjk
fsdkfjksdjfkjsdf
12sdfesdf
aslkdjfkasdjf asdlfkjaskdfj

  1. delete sdkfjk to the last line
[root@linux-128 test]# sed  '/sdkfjk/,$'d 3.txt
askdj
aslkd aslkdjf3e
skdjfsdfj

  1. Delete the next line to the last line of the specified line
[root@linux-128 test]# cat 2.txt
ERT
FFF
**
[ABCFD]
123
324
444
[RTY]
**
FGFGF


[root@linux-128 test]# sed '/123/{p;:a;N;$!ba;d}' 2.txt
ERT
FFF
**
[ABCFD]
123

  1. Print 1 to 100 lines containing a string  http://ask.apelearn.com/question/1048
[root@linux-128 test]# cat 2.txt
ERT
FFF
**
[ABCFD]
123
324
444
[RTY]
**
FGFGF

method one:

[root@linux-128 test]# sed -n '1,10{/123/p}' 2.txt
123

Method Two:

[root@linux-128 test]# sed -n '1,10'p 2.txt |sed -n '/123/'p
123

Guess you like

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