How to extract multiple values from a line of particular fields using awk?

I am having a file "file1.txt" which is having the data as follows:

rahul 1994 Australia 40000
akash 2000 India 50000

Now i want to extract values of field 1 and field 4 in some variable.
I am doing it as follows:

while read line  
do  
  name=$(awk '{print $1}'<< "$line")  
  salary=$(awk '{print $4}'<< "$line")  
  echo $name $salary
done < file1.txt

So i want to ask that can i assign values to name and salary by using only one awk command?

1 answer

  • answered 2018-10-13 19:02 Gilles Quenot

    Better do this in plain ;

    while read name _ _ salary; do    
      echo "$name $salary"
    done < file1.txt