Erlaunis Erlaunis - 1 year ago 68
Bash Question

Get the third element of a line into a file with script shell

I'm doing a script shell and I want to read data inside a file. In the file, I have something like :

/path/to/file1 something 0

/path/to/file2 something2 1

/path/to/file3 something3 2

What I want is to get the third element of the line but I don't know how to do it.
In my code, I have:

while read line;
//must echo the third element of the line
done < file | sort -n -k 2 -t " "

I already tried with
but it didn't work.

How should I do please ?

Answer Source

you can try something like this;

while read line;
path=$(echo $line | awk '{print  $1}')
secondColumn=$(echo $line | awk '{print  $2}')
thirdColumn=$(echo $line | awk '{print  $3}')

echo $path
echo $secondColumn
echo $thirdColumn

done < test 
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download