bash - Detect column separators in awk -


i'm trying separate in various files initial *.txt file using awk. got following format.

inline  xline   x    y     horizon  time     1       159  806313 939258 kf2      0.80 .... 81      149  805004 948030 fallriver 0.85965 .... 243     146  804252 965837 tensleepbbase 1.1862 

in case separator fifth column (kf2,fallriver,tensleepbbase). idea iterate , break loop when value of fifth column change don't know how structure algorithm in awk.

the expected result 3 txt files. 1 each horizon key word:

file1.txt

inline  xline   x    y     horizon  time     1       159  806313 939258 kf2      0.80 ... end of kf2 horizon keyword 

file2.txt

inline  xline   x    y     horizon  time     81      149  805004 948030 fallriver 0.85965 ... end of fallriver horizon keyword 

....

thank you.

using input file,

inline  xline   x    y     horizon  time     1       159  806313 939258 kf2      0.80 2       9  806313 939258 kf2      0.80 3       59  806313 939258 kf2      0.80 81      149  805004 948030 fallriver 0.85965 82      345  5678   948030 fallriver 0.85965 243     146  804252 965837 tensleepbbase 1.1862 

i this:

awk 'nr==1 { hdr=$0;next}            # pick column headers, , avoid other processing            { hrz=$5;                 # save current horizon              if(hrz!=oldhrz){        # check if horizon has changed                 if(length(oldhrz)>0)print "end of ",oldhrz > file                 file=++f ".txt"      # work out name of output file                 print hdr > file     # print column headers new file                 oldhrz=hrz           # remember current horizon              }               print > file            }      end   { print "end of ",hrz > file}' input.txt 

output

1.txt

inline  xline   x    y     horizon  time 1       159  806313 939258 kf2      0.80 2       9  806313 939258 kf2      0.80 3       59  806313 939258 kf2      0.80 end of  kf2 

2.txt

inline  xline   x    y     horizon  time 81      149  805004 948030 fallriver 0.85965 82      345  5678   948030 fallriver 0.85965 end of  fallriver 

3.txt

inline  xline   x    y     horizon  time 243     146  804252 965837 tensleepbbase 1.1862 end of  tensleepbbase 

Comments

Popular posts from this blog

php - Zend Framework / Skeleton-Application / Composer install issue -

c# - Better 64-bit byte array hash -

python - PyCharm Type error Message -