linux 中删除文本中第一个.号之前的内容

 

001、方法1 利用cut

[root@pc1 test01]# ls
a.txt
[root@pc1 test01]# cat a.txt                   ## 测试数据
001.002.003.004.005
006.007.008.009.010
011.012.013.014.015
016.017.018.019.020
021.022.023.024.025
026.027.028.029.030
[root@pc1 test01]# cut -d "." -f 2- a.txt      ## cut
002.003.004.005
007.008.009.010
012.013.014.015
017.018.019.020
022.023.024.025
027.028.029.030

 

002、方法2 ,利用sed

[root@pc1 test01]# ls
a.txt
[root@pc1 test01]# cat a.txt
001.002.003.004.005
006.007.008.009.010
011.012.013.014.015
016.017.018.019.020
021.022.023.024.025
026.027.028.029.030
[root@pc1 test01]# sed 's/\S\+\.//' a.txt         ## 有点奇怪
005
010
015
020
025
030
[root@pc1 test01]# sed 's/\S[^.]\+\.//' a.txt     ## 需要多加一层否定
002.003.004.005
007.008.009.010
012.013.014.015
017.018.019.020
022.023.024.025
027.028.029.030

 

003、方法3, 利用awk

a、

[root@pc1 test01]# ls
a.txt
[root@pc1 test01]# cat a.txt      ## 测试数据
001.002.003.004.005
006.007.008.009.010
011.012.013.014.015
016.017.018.019.020
021.022.023.024.025
026.027.028.029.030                                               ## awk实现
[root@pc1 test01]# awk -F "." '{for(i = 2; i < NF; i++) {printf("%s.", $i)} {print $NF}}' a.txt
002.003.004.005
007.008.009.010
012.013.014.015
017.018.019.020
022.023.024.025
027.028.029.030

 

b、

[root@pc1 test01]# ls
a.txt
[root@pc1 test01]# cat a.txt          ## 测试文本
001.002.003.004.005
006.007.008.009.010
011.012.013.014.015
016.017.018.019.020
021.022.023.024.025
026.027.028.029.030
[root@pc1 test01]# awk '{sub(/[0-9]+\.*/, ""); print $0}' a.txt     ## 借助awk,sub正则替换命令实现
002.003.004.005
007.008.009.010
012.013.014.015
017.018.019.020
022.023.024.025
027.028.029.030

 

 

004、 借助循环 +  字符串处理

[root@pc1 test01]# ls
a.txt
[root@pc1 test01]# cat a.txt         ## 测试文本
001.002.003.004.005
006.007.008.009.010
011.012.013.014.015
016.017.018.019.020
021.022.023.024.025
026.027.028.029.030
[root@pc1 test01]# cat a.txt | while read i; do echo ${i#*.}; done  ## 借助循环 + 字符串处理实现
002.003.004.005
007.008.009.010
012.013.014.015
017.018.019.020
022.023.024.025
027.028.029.030

 。

 

posted @ 2023-10-10 10:29  小鲨鱼2018  阅读(103)  评论(0编辑  收藏  举报