32

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.

Gil
  • 1,518
  • 4
  • 16
  • 32

7 Answers7

67

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.

Levon
  • 138,105
  • 33
  • 200
  • 191
11

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
beny23
  • 34,390
  • 5
  • 82
  • 85
2

may be you could try this out:

awk -v variable=$bashvariable  '$0{print variable"\t single\t" $0"\t double"}' myinfile.c
Kent
  • 189,393
  • 32
  • 233
  • 301
  • ur solution falters when row being read in contains only an `ASCII` `"0"`, `"-0"`,`"0.0000"` etc (basically anything `awk` tries to be too clever and interpret it numerically on ur behalf) : `echo "0" | awk -v variable=$bashvariable '$0{print variable"\t single\t" $0"\t double"}' < nothing gets printed. new shell prompt > %` – RARE Kpop Manifesto Jul 28 '22 at 01:13
1

Try this:

awk -v variable=$bashvariable '/^.+$/{print variable"\t single\t" $0"\t double"}' myinfile.c
Mithrandir
  • 24,869
  • 6
  • 50
  • 66
0

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:]]’.

pjboro
  • 111
  • 4
0

Based on Levon's answer, you may just add | awk 'length { print $1 }' to the end of the command.

So change

awk -v variable=$bashvariable  '{ whatever }' myinfile.c

to

awk -v variable=$bashvariable  '{ whatever }' myinfile.c | awk 'length { print $1 }'

In case this doesn't work, use | awk 'NF { print $1 }' instead.

Mohsenasm
  • 2,916
  • 1
  • 18
  • 22
0

another awk way of only trimming out actually zero length lines but keep the ones with only spaces tabs is this :

awk 8 RS= 

just doing awk NF trims out lines 3 (zero length) and 5 (spaces and tabs) …..

 1   abc 
 2  def
 3  
 4   3591952
 5          
 6   93253
         1   abc 
         2  def
    
         3   3591952
         4          
         5   93253
 
 1   abc 
 2  def
 3   3591952
 4   93253

but the RS= approach keeps line 5 for u:

 1   abc 
 2  def
 3   3591952
 4          
 5   93253

** lines with \013 \v VT :: \014 \f FF :: \015 \r CR aren't skipped by default FS = " ", despite them also belonging to POSIX [[:space:]]

RARE Kpop Manifesto
  • 2,453
  • 3
  • 11