awk field separator help -

Hi Experts ,

file :

[ Aug 19 21:02 apptime_flagECQAPP ] [ Aug 19 21:02 apptime_flagBWQAPP ] [ Aug 24 01:04 apptime_flagE2DAPP ]
[ Aug 24 01:43 apptime_flagECTAPP ] [ Aug 25 10:02 apptime_flagE2QAPP ] [ Aug 26 17:02 apptime_flagDELQAS ]
[ Aug 28 01:47 apptime_flagMOWSQ101 ] [ Aug 28 01:47 apptime_flagMOWSD101 ] [ Aug 28 01:47 apptime_flagMOWSQ102 ]
[ Aug 28 01:49 apptime_flagBWTAPP ] [ Aug 28 01:49 apptime_flagMOWSD001 ] [ Aug 28 01:49 apptime_flagMOWSQ002 ]
  • How to construct the awk filed separator so that $1, $2 $3 , can be assigned to the each "[ ]" range.

I am trying : awk -F"[\[\]]" '{print $1}'
but it is printing the entire file. Not first field.

The desired output needed for first field is:

[ Aug 19 21:02 apptime_flagECQAPP ]       
[ Aug 24 01:43 apptime_flagECTAPP ]        
[ Aug 28 01:47 apptime_flagMOWSQ101 ]
[ Aug 28 01:49 apptime_flagBWTAPP ]      

Thanks in advance.

depending on how picky your awk is:

awk -F'(\\[)|(\\])' '{print "[" $2 "]" }' myFile
1 Like

Try the awk below...

awk -F" ]" '{print $1 FS $2 FS $3}' file
1 Like

awk is messy with [ ] characters. I'm on Solaris so awk/nawk is even messier

Try: tr piped into awk as a general approach. If this works with the ancient BSD /usr/bin/awk of Solaris it will work with modern ones, too.

tr -s '\[' ' ' < filename | tr -s '\]' '|' | awk -F'|' '{print $1}'
1 Like

but then you loose the brakets in the output hehe... yep messy...

1 Like

What about

 awk '{gsub ("\] \[", "];["); print $1}' FS=";" file
[ Aug 19 21:02 apptime_flagECQAPP ]
[ Aug 24 01:43 apptime_flagECTAPP ]
[ Aug 28 01:47 apptime_flagMOWSQ101 ]
[ Aug 28 01:49 apptime_flagBWTAPP ]
1 Like

GNU awk 4+:

awk '{ print $1 }' FPAT='[[][^]]+[]]' infile

Standard awk:

awk 'match($0, /[[][^]]+[]]/) {
      print substr($0, RSTART, RLENGTH)
      }' infile 

If you need all the occurrences:

awk '{
  x = $0
  while (match(x, /[[][^]]+[]]/)) {
    data[++c] = substr(x, RSTART, RLENGTH)
    x = substr(x, RLENGTH + 1)
    }
  for (i = 0; ++i <= c;)
    print i, data
  c = x  
      }' infile 
3 Likes

Or

awk -F'[]] |[]]$' '{print $1 "]"}' file
1 Like

Thanks you .. all are great codes, and working.

---------- Post updated at 05:02 PM ---------- Previous update was at 05:01 PM ----------

This also working by taking one bracket as field separator : awk -F"]" '{print $1 "]"}' file

@rveri, yes, but then an extra space gets printed in case of field 2 or field 3.