1

In a file, I have a certain construct:

------------------------------
|  1   123.456    789.1011 T |
|  2   789.123    234.1234 T |
------------------------------
 All true

I can

grep -s -B 3 "All true" file.out | head -n 2

to get only the relevant lines

|  1   123.456    789.1011 T |
|  2   789.123    234.1234 T |

How would I now go on to get the first numbers (123.456 and 789.123) in variables, but multiplied by a certain value x

Is there a way to get the n'th argument of a grepped line or something?

1
  • 2
    You are looking for cut. See man cut and option -f
    – Philippos
    Jul 17, 2017 at 8:13

2 Answers 2

1

Short grep + awk approach:

Let's say, the multiplier is 3:

grep -sB 3 "All true" file.out | awk -v x=3 '$1=="|"{ print $3*x }'

The output:

370.368
2367.37

  • -v x=3 - passing multiplier as x variable into awk script
1

Credit to @Philippos, I managed to get the answer:

number=$(grep -s -B 3 "All true" file.out | head -n 2 | cut -d' ' -f6 )

for i in ${number}
do
  echo "$i*$x" | bc -l
done

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .