Skip to content
Advertisement

Convert number from text file

I have a file:

 id name date
 1  paul 23.07
 2  john 43.54
 3  marie 23.4
 4  alan  32.54
 5  patrick 32.1

I want to print names that start with “p” and have an odd numbered id

My command: grep "^p" filename | cut -d ' ' -f 2 | ....

result:

paul
patrick

Advertisement

Answer

Awk can do it all:

$ awk 'NR > 1 && $2 ~ /^p/ && ($1 % 2) == 1 { print $2 }' op.txt
paul
patrick

EDIT
To use : as the field separator:

$ awk -F: 'NR > 1 && $2 ~ /^p/ && ($1 % 2) == 1 { print $2 }' op.txt

NR > 1
Skip the header

$2 ~ /^p/
Name field starts with p

$1 % 2 == 1
ID field is odd

If all of the above are true:
{ print $2 }
Print the name field

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement