awk与sed命令面试题整理

1、sed命令
123abc456
456def123
567abc789
789def567
要求输出:
456ABC123
123DEF456
789ABC567
567DEF789
答案:
sed -r -i 's#(...)(.)(.)(.)(...)#\5\u\2\u\3\u\4\1#g' 22.txt
返回
sed -r -i 's#(...)(.)(.)(.)(...)#\5\l\2\l\3\l\4\1#g' 22.txt

2、awk命令
100
a 100
b -50
c -20
d -30
要求输出结果为:
100
a 100
200
b -50
150
c -20
130
d -30
答案:
awk 'sum+=$1+$2{if(NR==1) print $1;else if(NR==5)print $0;else print $1"\t"$2"\n"sum}' 11.txt

3、腾讯一 shell试题.
假设qq. tel文件内容:
12334:13510014336
12345:12334555666
12334:12343453453
12099:13598989899
12334:12345454545
12099:12343454544
分类如下:
[12334]
13510014336
12343453453
...........
[12099]
13598989899
12343454544

答案:
[root@localhost ~]# cat qq.tel | sort -r | awk -F: '{if (tmp!=$1) {tmp=$1;print "["tmp"]"} print $2}'
[12345]
12334555666
[12334]
13510014336
12345454545
12343453453
[12099]
13598989899
12343454544

4、[腾讯面试题]:一个文本类型的文件,里面每行存放
登陆者的IP (某些行是重复的),写一个shell脚本输出
登陆次数最多的用户。
IP内容如下:
219.217.49. 14
175.43.4.87
87.48.98.1
59.73.38.25
219.217.50.14
59.92.48.32
219.217.49.14
59.72.38.142
59.73.38.25
219.217.49.14


5、处理一 下文件内容,将域名取出并进行计数排数,如处理: ;
http: //www . baidu. com/ index. html
http: / / ww .baidu. com/1.html
http:/ / www . baidu. com/2. html
http: / /post . baidu. com/ index . html
http: / /mp3. baidu. com/ index. html
http:/ / www . baidu. com/3. html
http: / /post.baidu. com/2. html
得到如F结果:域名的出现次数,域名
4 www . baidu. com
2 post .baidu. com
1 mp3. baidu. com

答案:
[root@localhost ~]# awk -F/ '{print $3}' yuming.txt | sort -r | uniq -c
4 www.baidu.com
2 post.baidu.com
1 mp3.baidu.com

猜你喜欢

转载自www.cnblogs.com/lyqlyqlyq/p/11489904.html
今日推荐