regex - AWK - Search for a pattern-add it as a variable-search for next line that isn't a variable & print it + variable -
i have given file:
application_1.pp application_2.pp #application_2_version => '1.0.0.1-r1', application_2_version => '1.0.0.2-r3', application_3.pp #application_3_version => '2.0.0.1-r4', application_3_version => '2.0.0.2-r7', application_4.pp application_5.pp #application_5_version => '3.0.0.1-r8', application_5_version => '3.0.0.2-r9',
i able read file , search string
".pp"
when string found, adds line variable , stores it. reads next line of file. if encounters line preceded # ignores , moves onto next line.
if comes across line not contain ".pp" , doesn't start # should print out line next last stored variable in new file.
the output this:
application_1.pp application_2.pp application_2_version => '1.0.0.2-r3', application_3.pp application_3_version => '2.0.0.2-r7', application_4.pp application_5.pp application_5_version => '3.0.0.2-r9',
i achieve awk. if knows how , simple solution happy if share me. if more complex, helpful know in awk need understand in order know how (arrays, variables, etc). can achieved awk or tool necessary?
thanks,
i'd say
awk '/\.pp/ { if(nr != 1) print line; line = $0; next } nf != 0 && substr($1, 1, 1) != "#" { line = line $0 } end { print line }' filename
this works follows:
/\.pp/ { # if line contains ".pp" if(nr != 1) { # unless started print line # print last assembled line } line = $0 # , remember new 1 next # , we're done here. } nf != 0 && substr($1, 1, 1) != "#" { # otherwise, unless line empty # or comment line = line $0 # append line we're building } end { # in end, print line # print last line. }
Comments
Post a Comment