Thursday, April 7, 2022

[SOLVED] Print only if field is not empty

Issue

I have a text file that I want to pull host and IP info from only if the IP exists in column 4. For example:

cat hostlist.txt
Server One 255.255.255.255 123.123.123.123
Server Two 255.255.255.255 
Server Three 255.255.255.255 123.123.123.123

In this case I would only want to see Server One and Three as Server Two has no data in the fourth column.


Solution

awk '{if ($4) print $0;}' < hostlist.txt

does the trick. It's functionally equivalent to the earlier solution but is simpler since you only check for existence rather than matching a regex.



Answered By - Kyle Banerjee
Answer Checked By - Mary Flores (WPSolving Volunteer)