简体   繁体   中英

Awk to skip the blank lines

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.

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, ie,

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 (ie, greater than zero).

The second method looks at the line length and acts if the length is not zero (ie, greater than zero)

You can pick the approach that is most suitable for your data/needs.

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

也许你可以试试这个:

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

尝试这个:

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

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

Based on Levon's answer, you may just add | awk 'length { print $1 }' | 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 }' | awk 'NF { print $1 }' instead.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM