Regex 使用awk时,某些文件夹名称丢失
我正在尝试grep/share/volume\u存储库后面的文件夹名称 c20_测试PRD_108Regex 使用awk时,某些文件夹名称丢失,regex,awk,Regex,Awk,我正在尝试grep/share/volume\u存储库后面的文件夹名称 c20_测试PRD_108 /share/volume_repository/c20_testprd_108_2018-01-0912:15:51.469 /share/volume_repository/test_testprd_20_2019-03-0504:03:24.24 /share/volume_repository/c20_testprd_109_2018-01-0912:11:32.915 /share/vo
/share/volume_repository/c20_testprd_108_2018-01-0912:15:51.469
/share/volume_repository/test_testprd_20_2019-03-0504:03:24.24
/share/volume_repository/c20_testprd_109_2018-01-0912:11:32.915
/share/volume_repository/hp_testprd_2003_2018-10-2917:51:24.724
/share/volume_repository/hp_testprd_3335_2019-01-2220:00:17.139
/share/volume_repository/hp_testprd_2002_2018-10-2917:49:15.605
/share/shared_volume_repository/fnolan_ha_testprd_02_2018-06-2621:31:23.405
我尝试通过cut和awk的组合进行提取,在awk中,如果我使用字段分隔符,它会删除一些文件夹名称
cat abc |cut -d '/' -f 4|awk -F '_20' '{print $1}'
输出:
c20_testprd_108
test_testprd
c20_testprd_109
hp_testprd
hp_testprd_3335
hp_testprd
fnolan_ha_testprd_02
c20_testprd_108
test_testprd_20
c20_testprd_109
hp_testprd_2003
hp_testprd_3335
hp_testprd_2002
fnolan_ha_testprd_02
预期产量为
c20_testprd_108
test_testprd_20
c20_testprd_109
hp_testprd_2003
hp_testprd_3335
hp_testprd_2002
fnolan_ha_testprd_02
你能试试下面的吗。使用显示的样本进行书写和测试
awk '
match($0,/\/share\/(shared_)?volume_repository\/[^:]*/){
value=substr($0,RSTART,RLENGTH)
gsub(/.*\/|_[0-9]+-[0-9]+-[0-9]+$/,"",value)
print value
}
' Input_file
说明:添加上述内容的详细说明
awk ' ##Starting awk program from here.
match($0,/\/share\/(shared_)?volume_repository\/[^:]*/){ ##Using match function to match regex from share till colon here.
value=substr($0,RSTART,RLENGTH) ##Creating var value with sub-string for current line.
gsub(/.*\/|_[0-9]+-[0-9]+-[0-9]+$/,"",value) ##Globally substituting everything till / OR last date timings from value here.
print value ##Printing value here.
}
' Input_file ##Mentioning Input_file name here.
使用GNU awk:
awk '{print $4}' FS='/|_....-..-....' file
输出:
c20_testprd_108
test_testprd
c20_testprd_109
hp_testprd
hp_testprd_3335
hp_testprd
fnolan_ha_testprd_02
c20_testprd_108
test_testprd_20
c20_testprd_109
hp_testprd_2003
hp_testprd_3335
hp_testprd_2002
fnolan_ha_testprd_02
c20_测试PRD_108
测试项目20
c20_测试PRD_109
hp_testprd_2003
hp_测试PRD_3335
hp_测试PRD_2002
fnolan_ha_testprd_02