0

I am trying to remove leading whitespace from awk output. When I use this command, a leading whitespace is displayed.

diff  test1.txt test.txt | awk '{print $2}'

output: 

asdfasdf.txt
test.txt
weqtwqe.txt

How can I remove the leading whitespace using awk?

Thanks in advance

Gautam
  • 21
  • 1
  • 6
  • `awk '{print $2}'` shouldn't print any whitespace, if you haven't changed the field separator. – user000001 Sep 25 '15 at 19:10
  • 1
    `awk '$2{print $2}'` may be ..... it is not clear what you want .... print if $2 exists ... `awk '$2!=""{print $2}'` better – Jose Ricardo Bustos M. Sep 25 '15 at 19:12
  • 2
    do you mean the blank line at the top of your output? If so, please edit your question to indicate that (DON'T reply here). Good luck. – shellter Sep 25 '15 at 19:17
  • @JoseRicardoBustosM. Ricardo Bustos M. That was exactly what I needed. Thanks for your help. That removed the leading whitespace from the output. – Gautam Sep 25 '15 at 19:18
  • 1
    Hopefully by `That` you mean `awk '$2!=""{print $2}'` and not `awk '$2{print $2}'` as the latter will fail when `$2` contains a value that evaluates numerically to zero. – Ed Morton Sep 25 '15 at 19:20

1 Answers1

1

if you want to print the lines where $2 exists you can do it conditionally on number of fields

awk 'NF>1{print $2}'

will do.

karakfa
  • 66,216
  • 7
  • 41
  • 56