Awk to skip the blank lines
Solution 1
There are two approaches you can try to filter out lines:
awk 'NF' data.txt
and
awk 'length' data.txt
Just put these at the start of your command, i.e.,
awk -v variable=$bashvariable 'NF { print variable ... }' myinfile
or
awk -v variable=$bashvariable 'length { print variable ... }' myinfile
Both of these act as gatekeepers/if-statements.
The first approach works by only printining out lines where the number of fields (NF
) is not zero (i.e., greater than zero).
The second method looks at the line length and acts if the length is not zero (i.e., greater than zero)
You can pick the approach that is most suitable for your data/needs.
Solution 2
You could just add
/^\s*$/ {next;}
To the front of your script that will match the blank lines and skip the rest of the awk matching rules. Put it all together:
awk -v variable=$bashvariable '/^\s*$/ {next;} {print variable"\t single\t" $0"\t double"}' myinfile.c
Solution 3
may be you could try this out:
awk -v variable=$bashvariable '$0{print variable"\t single\t" $0"\t double"}' myinfile.c
Solution 4
Try this:
awk -v variable=$bashvariable '/^.+$/{print variable"\t single\t" $0"\t double"}' myinfile.c
Comments
-
Gil almost 4 years
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 tellawk
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 ofsed
andtr
way but I wantawk
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.