Awk to skip the blank lines

Viewed 45501

The output of my script is tab delimited using awk as :

awk -v variable=$bashvariable  '{print variable"\t single\t" $0"\t double"}' myinfile.c

The awk command is run in a while loop which updates the variable value and the file myinfile.c for every cycle. I am getting the expected results with this command . But if the inmyfile.c contains a blank line (it can contain) it prints no relevant information. can I tell awk to ignore the blank line ?

I know it can be done by removing the blank lines from myinfile.c before passing it on to awk . I am in knowledge of sed and tr way but I want awk to do it in the above mentioned command itself and not a separate solution as below or a piped one.

sed '/^$/d' myinfile.c
tr -s "\n" < myinfile.c

Thanks in advance for your suggestions and replies.

5 Answers

I haven't seen this solution, so: awk '!/^\s*$/{print $1}' will run the block for all non-empty lines. \s metacharacter is not available in all awk implementations, but you can also write !/^[ \t]*$/.

https://www.gnu.org/software/gawk/manual/gawk.html

\s Matches any space character as defined by the current locale. Think of it as shorthand for ‘[[:space:]]’.

Related