2

I was wondering if there's any command to look for blank field values of a text file and remove the entire line.

Example

 Hello:Its Me
 Hello:How are you
 Hello:
 Hello:Bye

And expected output

 Hello:Its Me
 Hello:How are you
 Hello:Bye
1
  • 3
    do add your own efforts to solve to question when asking... your previous question didn't get an answer, but somehow you've got it for this one.. :)
    – Sundeep
    Dec 13, 2017 at 16:21

4 Answers 4

5

Very standard case, the simplest solution with awk would be

awk -F: '$2 != ""' file

and with grep:

grep :. file 
3

If you mean to skip Hello: lines you can use something like:

awk -F: '{if($2 != "") print }' input_file 
3

At least with GNU awk, and I think with any version of awk, the default action when something evaluates to true is to print. So, if you set the field delimiter to :, to print lines that have a second field, all you need is:

$ awk -F: '$2' file
Hello:Its Me
Hello:How are you
Hello:Bye

However, this will also print cases where the second field is whitespace (a space, or a tab etc), but will not print the line if second field is one or multiple 0s, because awk evaluates this as false.

0
0

Sometimes it is easier to define the remove conditions (grep -v). Example remove when last field is empty:

grep -v ':$' file

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .