linux 中substr提取指定字符串

1、测试数据

root@PC1:/home/test2# ls
a.txt
root@PC1:/home/test2# cat a.txt    ## 测试数据
chenwu 05/99 4811 27
mary   02/22 1231 30
tom    09/15 1182 25

 

2、

root@PC1:/home/test2# ls
a.txt
root@PC1:/home/test2# cat a.txt
chenwu 05/99 4811 27
mary   02/22 1231 30
tom    09/15 1182 25
root@PC1:/home/test2# awk '{print substr($0,1,3)}' a.txt   ## 提取前三个字符
che
mar
tom

 

3、

root@PC1:/home/test2# ls
a.txt
root@PC1:/home/test2# cat a.txt
chenwu 05/99 4811 27
mary   02/22 1231 30
tom    09/15 1182 25
root@PC1:/home/test2# awk '{print substr($2,1,3), $0}' a.txt   ## 提取第二列的前三个字符
05/ chenwu 05/99 4811 27
02/ mary   02/22 1231 30
09/ tom    09/15 1182 25

 

4、

root@PC1:/home/test2# ls
a.txt
root@PC1:/home/test2# cat a.txt
chenwu 05/99 4811 27
mary   02/22 1231 30
tom    09/15 1182 25
root@PC1:/home/test2# awk '{print substr($3, length($3)-1,2)}' a.txt   ## 提取第三列的最后两个字符
11
31
82

 

posted @ 2022-01-21 23:34  小鲨鱼2018  阅读(673)  评论(0编辑  收藏  举报