Shell Script – Assign Every Word from a Line to a Variable

awkshell-script

I am very new to shell programming.
I have a file named quaternary_splitted.csv on macOS.
Every line has 4 words. I want to take every word from each line and assign it to a variable. Please suggest some kind of awk command in for loop
The value of each of those 4 variable I want to use further in my shell program.

Thanks for the help.
Few of the lines from the file:

Ta Cr Mo W  
Nb Cr Mo W  
Nb Ta Mo W  
Nb Ta Cr W  
Nb Ta Cr Mo

Best Answer

You can do this with a while read loop like so:

while read -r col1 col2 col3 col4 trash; do
    something with "$col1"
    something with "$col2"
    something with "$col3"
    something with "$col4"
done < /path/to/quaternary_splitted.csv

This will read through each line of quaternary_splitted.csv and set the first column to col1, second column to col2, etc.

The trash parameter is used to catch anything that may be in your file and unwanted. Say you had a line: Nb Ta Cr W what is this doing here?. Without trash you would get:

col1=Nb
col2=Ta
col3=Cr
col4='W what is this doing here?`
Related Question