barp
barp

Reputation: 6929

reading particular text from file with awk

I need to read some values from a file with awk and then need to use the values that I read in bash.For example the structure of file will be

NICE = -13

LATENCY= 100

WMEM= 4 

I need to read the values -13, 100 and 4 from the file with awk and then use them in bash. The structure of script will be;

#!/bin/bash

awk'{}'

and then use the values here in script

Upvotes: 1

Views: 718

Answers (2)

tzelleke
tzelleke

Reputation: 15345

values=($(awk 'NF{print $NF}' file))

This will create an array in bash. The array contains all last token of non-empty lines. You can access the values in the array with

echo ${values[0]}

to echo the first values. Be aware there mustn't be spaces around the =

In the awk-Code the first NF filters out empty lines. You can leave it off, if your file does not contain empty lines.

Upvotes: 3

pankar
pankar

Reputation: 1701

If your input is in data.txt then try running the following command inside your script:

values=`cat data.txt | awk -F '=' '{print $2}' | xargs`

values variable will then contain: -13 100 4

Upvotes: 3

Related Questions