awk - BASH append line to earlier line if preceded by spaces -


i have file looks this:

t2_this_is_some_output_80_pool             address 12.34.56.78             state down             address 13.34.56.78             state down t2_this_is_a_different_output_80_pool             address 14.34.56.78 t2_this_is_another_output_80_pool             address 15.34.56.78             state     

i wnat output looks like:

t2_this_is_some_output_80_pool address 12.34.56.78 state down t2_this_is_some_output_80_pool address 13.34.56.78 state down t2_this_is_a_different_output_80_pool address 14.34.56.78 t2_this_is_another_output_80_pool 15.34.56.78 state 

i've been trying bash, awk , sed, nothing have done has been able give me output need it.

one of things i've tried:

replace 12 spaces @ beginning = append previous line if line starts =

cat file.txt | sed 's/            /=/' | sed -e :a -e '$!n;s/\n=/ /;ta' -e 'p;d' 

but that's not working...

any received :-)

awk ' /^[^[:blank:]+]/{                     # search record/line not start blank             if(s){print s; s=""}      # whenever state not found print variable s             p=$0                      # store record in varibale p                next                      # stop processing go next line } {                                    gsub(/^[ \t]+/,"")              # suppress starting space/tab char        s = s ? s ofs $0: p ofs $0      # if variable s has concatenate variable s current record else variable p , current record } /state/{                              # if record has word state        print s; s=""                   # print variable s , reset variable } end{                                  # end block      if(s)print s                     # if s has print s }' file  

oneliner

$ awk '/^[^[:blank:]+]/{if(s){print s; s=""}p=$0; next}{gsub(/^[ \t]+/,"");s = s ? s ofs $0: p ofs $0; }/state/{print s; s=""}end{if(s)print s}' file 

input

$ cat f t2_this_is_some_output_80_pool             address 12.34.56.78             state down             address 13.34.56.78             state down t2_this_is_a_different_output_80_pool             address 14.34.56.78 t2_this_is_another_output_80_pool             address 15.34.56.78             state  

output

$ awk ' /^[^[:blank:]+]/{             if(s){print s; s=""}             p=$0;              next } {       gsub(/^[ \t]+/,"")       s = s ? s ofs $0: p ofs $0  } /state/{       print s; s="" } end{      if(s)print s }' f t2_this_is_some_output_80_pool address 12.34.56.78 state down t2_this_is_some_output_80_pool address 13.34.56.78 state down t2_this_is_a_different_output_80_pool address 14.34.56.78 t2_this_is_another_output_80_pool address 15.34.56.78 state   

Comments

Popular posts from this blog

Command prompt result in label. Python 2.7 -

javascript - How do I use URL parameters to change link href on page? -

amazon web services - AWS Route53 Trying To Get Site To Resolve To www -