【问题标题】:While using awk some of the folder names are missing使用 awk 时缺少一些文件夹名称
【发布时间】:2020-07-21 17:32:56
【问题描述】:

我正在尝试在 /share/volume_repository 之后 grep 文件夹名称

c20_testprd_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/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 的组合来获取,如果我使用字段分隔符 _20 在 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

【问题讨论】:

    标签: regex awk


    【解决方案1】:

    请您尝试以下操作。使用所示示例编写和测试。

    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.
    

    【讨论】:

      【解决方案2】:

      使用 GNU awk:

      awk '{print $4}' FS='/|_....-..-....' file
      

      输出:

      c20_testprd_108 test_testprd_20 c20_testprd_109 hp_testprd_2003 hp_testprd_3335 hp_testprd_2002 fnolan_ha_testprd_02

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-11-07
        • 1970-01-01
        • 1970-01-01
        • 2018-07-15
        • 1970-01-01
        • 2014-04-15
        • 2019-11-09
        相关资源
        最近更新 更多