2018-11-14直播课笔记

打印某行到某行之间的内容http://ask.apelearn.com/question/559

sed -n '/root/,/halt/p' /etc/passwd 。

[root@liang-00 ~]# sed -n '/root/,/bin/p' /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
[root@liang-00 ~]# 

 

sed转换大小写 http://ask.apelearn.com/question/7758

1. 把每个单词的第一个小写字母变大写:

sed 's/\b[a-z]/\u&/g' filename

\b 单词的边界,

2. 把所有小写变大写:

sed 's/[a-z]/\u&/g' filename

[root@liang-00 ~]# sed 's/[a-z]/\u&/g' test.txt 
A
B
S
D
E
F
[root@liang-00 ~]# 

3. 大写变小写:

sed 's/[A-Z]/\l&/g' filename

 

sed在某一行最后添加一个数字http://ask.apelearn.com/question/288

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

sed -r 's/(^a.*)/& 12/' test

[root@liang-00 ~]# sed 's/^[b].*/& 12/' test.txt 
a
b 12
s
d
e
f
[root@liang-00 ~]# 

 

删除某行到最后一行 http://ask.apelearn.com/question/213

sed '/c/{p;:a;N;$!ba;d}' test

定义一个标签a,匹配c,然后N把下一行加到模式空间里,匹配最后一行时,才退出标签循环,然后命令d,把这个模式空间里的内容全部清除。

 

参考 https://coolshell.cn/articles/9104.html

1、准备文件 pets.txt

2、内容:

This is my cat
  my cat's name is betty
This is my dog
  my dog's name is frank
This is my fish
  my fish's name is george
This is my goat
  my goat's name is adam

3、操作:

1)每一行的行首加上“#”

[root@liang-00 ~]# sed 's/^/#/g' pets.txt 
#This is my cat
#  my cat's name is betty
#This is my dog
#  my dog's name is frank
#This is my fish
#  my fish's name is george
#This is my goat
#  my goat's name is adam

2)每一行的行未加上“---”

[root@liang-00 ~]# sed 's/$/---/g' pets.txt 
This is my cat---
  my cat's name is betty---
This is my dog---
  my dog's name is frank---
This is my fish---
  my fish's name is george---
This is my goat---
  my goat's name is adam---
[root@liang-00 ~]# 

3)词首和词尾的表示

  • \< 表示词首。 如:\<abc 表示以 abc 为首的词;
  • \> 表示词尾。 如:abc\> 表示以 abc 结尾的词。
[root@liang-00 ~]# sed -nr '/(\<ope)/'p /etc/passwd
operator:x:11:0:operator:/root:/sbin/nologin

[root@liang-00 ~]# sed -nr '/(down\>)/'p /etc/passwd
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
[root@liang-00 ~]# 

4)N命令: 把下一行的内容纳入当成缓冲区做匹配。

[root@liang-00 ~]# sed 'N;s/my/your/' pets.txt
This is your cat
  my cat's name is betty
This is your dog
  my dog's name is frank
This is your fish
  my fish's name is george
This is your goat
  my goat's name is adam
[root@liang-00 ~]# sed 'N;s/\n/,/' pets.txt
This is my cat,  my cat's name is betty
This is my dog,  my dog's name is frank
This is my fish,  my fish's name is george
This is my goat,  my goat's name is adam
[root@liang-00 ~]# 

5)a命令就是append, i命令就是insert,它们是用来添加行的。

第一行前插入一行

[root@liang-00 ~]# head -n5 /etc/passwd|sed '1 i aaa' 
aaa
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
[root@liang-00 ~]# 

第一行后插入一行

[root@liang-00 ~]# head -n5 /etc/passwd|sed '1 a aaa' 
root:x:0:0:root:/root:/bin/bash
aaa
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
[root@liang-00 ~]# 

 

参考 http://www.cnblogs.com/51linux/archive/2011/09/24/2189420.html

参考 https://www.cnblogs.com/f-ck-need-u/p/7496916.html

 

1. N、D、P  处理多行模式空间的问题;

2. H、h、G、g、x  将模式空间的内容放入存储空间以便接下来的编辑;

3. :、b、t  在脚本中实现分支与条件结构。

 

1.1 N 当在sed中遇到N,会将下一行连带当前行汇总在一起处理

sed -n '/abc/{N;p}' test.txt

[root@liang-00 ~]# sed -n '/halt/{N;p}' /etc/passwd
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
[root@liang-00 ~]# 

sed -n  '/tss/{N; s/nologin\npostfix/111/;p}' test.txt

[root@liang-00 ~]# sed -n '/games/{N;s/nologin/aaaa/g;p}' /etc/passwd
games:x:12:100:games:/usr/games:/sbin/aaaa
ftp:x:14:50:FTP User:/var/ftp:/sbin/aaaa
[root@liang-00 ~]# 

1.2 P 只打印模式空间中第一个\n前面的部分

# cat 1.txt

1 b

a 2

3

2 x

5

# sed '/2/ {N;p}' 1.txt

1 b

a 2

3

a 2

3

2 x

5

2 x

5

# sed '/2/ {N;P}' 1.txt

1 b

a 2

a 2

3

2 x

2 x

5

 

1.3 D 和P类似,它只删除模式空间中第一个\n前面的部分

[root@aminglinux-01 ~]# cat 1.txt

1 b

a 2

3

2 x

5

[root@aminglinux-01 ~]# sed '/2/ {N;D}' 1.txt

1 b

3

5

[root@aminglinux-01 ~]# sed '/2/ {N;d}' 1.txt

1 b

 

2.1

H  [address[,address]]H 将pattern space中的内容append到hold space\n后

h:[address[,address]]h 将pattern space中的内容拷贝到hold space中,原来的hold space里的内容被清除

 

3  为了使使用者在书写sed脚本的时候真正的"自由",sed还允许在脚本中用":"设置记号,然后用"b"和"t"命令进行流程控制。顾名思义,"b"表示"branch","t"表示"test";前者就是分支命令,后者则是测试命令。

:lable定义标签,lable可以随便用什么名字,这个标签放置在你希望流程所开始的地方,单独放一行,以冒号开始。冒号与标签之间不允许有空格或者制表符,标签最后如果有空格的话,也会被认为是标签的一部分。






 

打印1到100行含某个字符串的行 http://ask.apelearn.com/question/1048

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

 

awk 中使用外部shell变量http://ask.apelearn.com/question/199

a=2; echo "a:b:c:d"|awk -F ":" -v get_a=$a '{print $get_a}'

[root@liang-00 ~]# a=3
[root@liang-00 ~]# echo $a
3
[root@liang-00 ~]# awk -v b=$a -F: '/halt/{print $b}' /etc/passwd
7
[root@liang-00 ~]# awk -v b=$a -F: '{print $b}' /etc/passwd
0
1
2
3
4
5
6
7
8
11
12

awk 合并一个文件 http://ask.apelearn.com/question/493

awk '{print NR,FNR}' 1.txt  2.txt //首先理解NR和FNR的不同

当NR==FNR其实就是第一个文件的内容

当NR>FNR,其实就是第二个文件的内容

 

把一个文件多行连接成一行 http://ask.apelearn.com/question/266

方法一:

a=`cat file`;echo $a

方法二:

awk '{printf("%s ",$0)}' file

方法三:

cat file |xargs

[root@liang-00 ~]# cat test.txt |xargs 
a b s d e f
[root@liang-00 ~]#

把多行连成一行并且相加求和(用bc和python求和)

[root@liang-00 ~]# awk -F: '{print $3}' /etc/passwd |xargs |sed 's/ /+/g'
0+1+2+3+4+5+6+7+8+11+12+14+99+192+81+999+74+89+998+1000+997+72+59
[root@liang-00 ~]# awk -F: '{print $3}' /etc/passwd |xargs |sed 's/ /+/g'|bc
4733
[root@liang-00 ~]# python
Python 2.7.5 (default, Apr 11 2018, 07:36:10) 
[GCC 4.8.5 20150623 (Red Hat 4.8.5-28)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> 0+1+2+3+4+5+6+7+8+11+12+14+99+192+81+999+74+89+998+1000+997+72+59
4733
>>> 

 

awk中gsub函数的使用 http://ask.apelearn.com/question/200

awk 'gsub(/www/,"abc")' /etc/passwd  // passwd文件中把所有www替换为abc

awk -F ':' 'gsub(/www/,"abc",$1) {print $0}' /etc/passwd  // 替换$1中的www为abc

 

awk 截取指定多个域为一行 http://ask.apelearn.com/question/224

用awk指定分隔符把文本分为若干段。如何把相同段的内容弄到一行?

以/etc/passwd为例,该文件以":"作为分隔符,分为了7段。

for i in `seq 1 7`

do

   awk -F ':' -v a=$i '{printf $a " "}' /etc/passwd

   echo

done

 

过滤两个或多个关键词 http://ask.apelearn.com/question/198

grep -E '123|abc' filename  // 找出文件(filename)中包含123或者包含abc的行

egrep '123|abc' filename    //用egrep同样可以实现

awk '/123|abc/'  filename // awk 的实现方式

 

用awk生成以下结构文件 http://ask.apelearn.com/question/5494

awk 'BEGIN{for(i=1;i<=100;i++)printf("%d,%d,%010d,%010d,%010d,%010d,%010d,%010d,%d\n",i,i,i,i,i,i,i,i,strftime("%Y%m%d%H%M%S"))}'

 

awk用print打印单引号 http://ask.apelearn.com/question/1738

awk 'BEGIN{print "a'"'"'s"}'

awk 'BEGIN{print "a'\''s"}'

awk 'BEGIN{print "a\"s"}'

打印但单引号,双引号

[root@liang-00 ~]# awk 'BEGIN{print "a'"'"'s"}'
a's
[root@liang-00 ~]# awk 'BEGIN{print "a'\''s"}'
a's
[root@liang-00 ~]# awk 'BEGIN{print "a\"s"}'
a"s
[root@liang-00 ~]# 

 

合并两个文件 http://ask.apelearn.com/question/945

awk的BEGIN和END http://blog.51cto.com/151wqooo/1309851

awk的参考教程 http://www.cnblogs.com/emanlee/p/3327576.html

 

转载于:https://my.oschina.net/u/3993922/blog/2877138

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值