user2761029 user2761029 - 4 months ago 11
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

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]}" ]
    then
        echo ${arr[@]};
    fi
done < names