-2

input:

#word1  #word2  #word3  #word4
1.00     2.00    3.00    4.00
#end
#word1  #word3  #word4
11.00    13.00   14.00
#end
#word1  #word2  #word3  #word4
21.00    22.00   23.00   24.00
#end
#word1  #word3  #word4
31.00    33.00   34.00
#end

output:

#word1  #word3  #word4
1.00     3.00    4.00
#end
#word1  #word3  #word4
11.00    13.00   14.00
#end
#word1  #word3  #word4
21.00    23.00   24.00
#end
#word1  #word3  #word4
31.00    33.00   34.00
#end

What I like to do is to print out the certain lines between the matching strings. I can do it if all the same, but in input sometimes there are two columns, sometimes four columns. How can I get that output from the input?

usermrt
  • 53
  • 8

1 Answers1

1

If you want to omit column2 if the line has four columns, you can use

awk 'NF==4{ print $1,$3,$4; next }{ print }' input.txt

or shorter

awk 'NF==4{ print $1,$3,$4; next }1' input.txt

This prints columns 1, 3 and 4 if the line has four columns or the complete line otherwise.

Freddy
  • 4,548
  • 1
  • 7
  • 17
  • @usermrt Would it be okay to format the output with `column -t`, i.e. `awk 'NF==4{ print $1,$3,$4; next }1' input.txt | column -t`? – Freddy Dec 23 '19 at 22:20
  • Thank you for your answer but I like to know if I can select "matching string". Column is great but I will combine that code with mine. still appreciated for your answer – usermrt Dec 23 '19 at 22:25
  • @usermrt, that's the reason we ask OP to add their efforts, now with your comment it completely looks like a new requirement, so please do add your efforts clearly in your question and let us know then. – RavinderSingh13 Dec 23 '19 at 22:26