Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Awk to skip the blank lines

Tags:

regex

bash

sed

awk

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.

like image 335
Gil Avatar asked Jul 27 '12 11:07

Gil


People also ask

How do I skip blank lines in awk?

\s metacharacter is not available in all awk implementations, but you can also write !/^[ \t]*$/ . \s Matches any space character as defined by the current locale. Think of it as shorthand for '[[:space:]]'.

Is awk '{ print $2?

awk '{ print $2; }' prints the second field of each line. This field happens to be the process ID from the ps aux output. xargs kill -${2:-'TERM'} takes the process IDs from the selected sidekiq processes and feeds them as arguments to a kill command. The signal is either $2 or, if $2 is not defined, SIGTERM.

How do I ignore blank lines in Linux?

By Using [: space:]grep's –v option will help print lines that lack blank lines and extra spacing that is also included in a paragraph form. You will see that extra lines are removed and output is in sequenced form line-wise. That's how grep –v methodology is so helpful in obtaining the required goal.

How do I ignore blank lines in grep?

To match empty lines, use the pattern ' ^$ '. To match blank lines, use the pattern ' ^[[:blank:]]*$ '. To match no lines at all, use the command ' grep -f /dev/null '.


2 Answers

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.

like image 130
Levon Avatar answered Sep 25 '22 13:09

Levon


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 
like image 23
beny23 Avatar answered Sep 23 '22 13:09

beny23