Home > other >  checking line of a text file in bash
checking line of a text file in bash

Time:07-04

I am trying to read 3rd column in my text file without decimal points and output the value using this command,

while read line
      do
      Temp=`awk '{ printf("%.0f\n", $3) }' $line`
      echo $Temp
done< final.txt

my final.txt file looks like this

-10.0 -80.0 61.82 20.2163 1.80357 1.68673

-10.0 -79.5 61.82 20.2163 2.80357 1.68673

-10.0 -79.0 63.82 20.2163 1.80357 1.68673

but I get an error like this when I execute my command

awk: cannot open -10.0 (No such file or directory)

Output I want to get is

61

61

63

Can someone help me to understand what went wrong in my code please?

CodePudding user response:

You are trying to read a file named after $line

Temp=`awk '{ printf("%.0f\n", $3) }' $line`

which I don't think it's your idea and t should be more like

awk '{ printf("%.0f\n", $3) }' final.txt

You don't need the while loop.

CodePudding user response:

So I edited my code like this.

Temp=`echo $line | awk '{printf("%.0f\n", $3)}'`
      echo $Temp

and it worked !!!

  • Related