technopathe technopathe - 5 months ago 17
Bash Question

store awk output in variable

I ignore what is the problem with this code ?

#! /bin/bash
File1=$1
for (( j=1; j<=3; j++ ))
{
output=$(`awk -F; 'NR=='$j'{print $3}' "${File1}"`)
echo ${output}
}




File1 looks like this :

Char1;2;3;89
char2;9;6;66
char5;3;77;8




I want to extract on every line looped the field 3

so the result will be

3
6
7



Answer

It should be like this:

#! /bin/bash
File1=$1
for (( j=1; j<=3; j++ ))
{
                output=$(awk -F ';' 'NR=='$j' {print $3}' "${File1}")
                echo ${output}
}

It working well on my CentOS.