linux学习lesson29
程序员文章站
2022-05-31 16:22:44
...
目录
1 复习1
- 打印某行到某行之间的内容
例如:有个文件test的内容如下:
ert
fff
**
[abcfd]
123
324
444
[rty]
**
fgfgf
怎么能截取
[abcfd]
123
324
444
[rty]
这一部分出来呢?
[[email protected] test]# sed -n '/\[abcfd\]/, /\[rty\]/'p test.txt
[abcfd]
123
324
444
[rty]
sed转换大小写
sed中,使用\u表示大写,\l表示小写
- 1. 把每个单词的第一个小写字母变大写:
sed 's/\b[a-z]/\u&/g' filename // \b是boundary(边界)的意思,表示单词到此结束
[[email protected] test]# cat test.txt
ert
fff
**
[abcfd]
123
324
444
[rty]
**
fgfgf
[[email protected] test]# sed 's/\b[a-z]/\u&/g' test.txt
Ert
Fff
**
[Abcfd]
123
324
444
[Rty]
**
Fgfgf
[[email protected] test]# sed 's/[a-z]/\u&/' test.txt
Ert
Fff
**
[Abcfd]
123
324
444
[Rty]
**
Fgfgf
- 2. 把所有小写变大写:
sed 's/[a-z]/\u&/g' filename
[[email protected] test]# sed 's/[a-z]/\u&/g' test.txt
ERT
FFF
**
[ABCFD]
123
324
444
[RTY]
**
FGFGF
- 3. 大写变小写:
sed 's/[A-Z]/\l&/g' filename //小写字母L
[[email protected] test]# cat test1
ERT
FFF
**
[ABCFD]
123
324
444
[RTY]
**
FGFGF
[[email protected] test]# sed 's/[A-Z]/\l&/g' test.txt
ert
fff
**
[abcfd]
123
324
444
[rty]
**
fgfgf
- sed在某一行最后添加一个数字
[[email protected] test]# cat test2
askdj
aslkd aslkdjf3e
skdjfsdfj
sdkfjk
fsdkfjksdjfkjsdf
12sdfesdf
aslkdjfkasdjf asdlfkjaskdfj
[[email protected] test]# sed 's/\(^a.*\)/\1 123/' test2
askdj 123
aslkd aslkdjf3e 123
skdjfsdfj
sdkfjk
fsdkfjksdjfkjsdf
12sdfesdf
aslkdjfkasdjf asdlfkjaskdfj 123
- 删除某行到最后一行
[[email protected] test]# cat test3
a
b
c
d
e
f
[[email protected] test]# sed '/c/{p;:a;N;$!ba;d}' test3
a
b
c
- 删除某行到下一行
[[email protected] test]# sed '/c/{N;d}' test3
a
b
e
f
- 打印1到100行含某个字符串的行
[[email protected] test]# cat test2
askdj
aslkd aslkdjf3e
skdjfsdfj
sdkfjk
fsdkfjksdjfkjsdf
12sdfesdf
aslkdjfkasdjf asdlfkjaskdfj
[[email protected] test]# sed -n '1,100{/a/p}' test2
askdj
aslkd aslkdjf3e
aslkdjfkasdjf asdlfkjaskdfj