Erlaunis Erlaunis - 1 month ago 8
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;
do
//must echo the third element of the line
done < file | sort -n -k 2 -t " "


I already tried with
awk
but it didn't work.

How should I do please ?

Answer

you can try something like this;

while read line;
do
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