regex - How to have a good output in bash when printing? -


i have command here, , have problem achieving format.

in lines,

date*2014*09*23 val*0001*abc n3*sample val*0002*xyz 

my desired output here this:

["abc", "xyc"] 

i tried code:

perl -nle 'print $& if /val\*[0-9]*\*\k.*/' file | awk '{ printf "\"%s\",", $0 }' 

resulting only:

"abc","xyz", 

another thing when printing 1 value.

if happens file this:

date*2014*09*23 val*0001*abc n3*sample 

my desired output (ignoring output of having []):

 "abc" 

you can awk:

#!/usr/bin/awk -f  begin {fs="*"; i=0; ors=""} $1=="val" {a[i++]=$3}  end {      if (i>1) {         print "[\"" a[0]         (j = 1; j < i; j++)             print  "\",\"" a[j]         print "\"]"     }     if (i==1)         print "\"" a[0] "\"" } 

Comments

Popular posts from this blog

php - Submit Form Data without Reloading page -

linux - Rails running on virtual machine in Windows -

php - $params->set Array between square bracket -