While using awk some of the folder names are missing

Viewed 63

I am trying to grep folder name present after /share/volume_repository

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

I tried to fetch by the combination of cut & awk, in awk if I am using field separator _20 it removes some of the folder names.

cat abc |cut -d '/' -f 4|awk -F '_20' '{print $1}'

Output:

c20_testprd_108
test_testprd
c20_testprd_109
hp_testprd
hp_testprd_3335
hp_testprd
fnolan_ha_testprd_02

The expected output is

c20_testprd_108
test_testprd_20
c20_testprd_109
hp_testprd_2003
hp_testprd_3335
hp_testprd_2002
fnolan_ha_testprd_02
2 Answers

Could you please try following. Written and tested with shown samples.

awk '
match($0,/\/share\/(shared_)?volume_repository\/[^:]*/){
  value=substr($0,RSTART,RLENGTH)
  gsub(/.*\/|_[0-9]+-[0-9]+-[0-9]+$/,"",value)
  print value
}
'  Input_file

Explanation: Adding detailed explanation for above.

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.

With GNU awk:

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

Output:

c20_testprd_108
test_testprd_20
c20_testprd_109
hp_testprd_2003
hp_testprd_3335
hp_testprd_2002
fnolan_ha_testprd_02
Related