shell中awk一些用法

awk删除 匹配指定字符串前的字符串

首先查找到test字符串的位置,然后这时候RSTART的变量的值就是test的开始的位置,而后利用substr函数从此处开始到结束位置。

awk ‘{match($0,"/jiangli");print substr($0,RSTART)}’ a.txt

[root@localhost ~]# find /opt/test -type f -print |  awk '{match($0,"/test");print substr($0,RSTART)}'
/test/a/c.txt
/test/a/b.txt
/test/c/c.txt
/test/b/b.txt
/test/b/c.txt
[root@localhost ~]# find /opt/test -type f -print
/opt/test/a/c.txt
/opt/test/a/b.txt
/opt/test/c/c.txt
/opt/test/b/b.txt
/opt/test/b/c.txt

awk删除某一行的某个字段

gsub函数则使得在所有正则表达式被匹配的时候都发生替换
gsub(regular expression, subsitution string, target string);简称 gsub(r,s,t)

[root@localhost ~]# cat test.txt 
svn地址/release/a.txt
www.gingerpower.cn/release/gegewgewgeg/SvipController.class.php
www.gingerpower.cn/aaaa/



[root@localhost ~]# cat test.txt | grep -vE '^$|/$' | awk '$0  ~/\/release/ {gsub("/release", "", $0); print $0}' | awk '{print "/data/www/jiangli/"$0}'
/data/www/jiangli/svn地址/a.txt
/data/www/jiangli/www.gingerpower.cn/gegewgewgeg/SvipController.class.php

Guess you like

Origin blog.csdn.net/qq_44379042/article/details/120550423