user2761029 user2761029 - 1 year ago 88
Linux Question

Bash print out line if third column exists

I have a script file that includes first name, last name and phone number that are seperated by space. Not everyone have phone number and i want to print out the names that do have a phone number.
The file looks something like this

Andy Frey 592895
Gregory Simons 6356345
George Hillton
Fredrik Thomson 125423

Answer Source

You can do it easily with awk:

[cnicutar@piper ~]$ awk 'NF==3' names 
Andy Frey 592895
Gregory Simons 6356345
Fredrik Thomson 125423

With straight bash:

while read -a arr
    do if [ -n "${arr[2]}" ]
        echo ${arr[@]};
done < names
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download