How to pass a file name through a variable to read it awk

Good afternoon,

I was wondering how to pass the awk file name as a variable so that awk read it.

So far I have done:

echo    file1   >  Aenumerar
echo    file2   >> Aenumerar
echo    file3   >> Aenumerar

AE=`grep -c '' Aenumerar`
r=1
while [ $r -le $AE ]; do
    lista=`awk "NR==$r {print $0}" Aenumerar`
    AEList=`grep -c '' $lista`
    s=1
    while [ $s -le $AEList ]; do
        word=`awk -v var=$s 'NR==var {print $1}' $lista`
        echo $word
    let "s = s + 1"
    done
let "r = r + 1"
done

Thanks a lot in advance for any hint or other easy way to do this with the bash command line

+4
source share
2 answers

Instead:

awk "NR==$r {print $0}" Aenumerar

You need to use:

awk -v r="$r" 'NR==r' Aenumerar
+5
source

Judging by what you posted, you really don't need all things NR; you can replace your entire script with this:

while IFS= read -r lista ; do
    awk '{print $1}' "$lista"
done < Aenumerar

(This will print the first field of each line in each of file1, file2, file3. I think you're trying to do?)

+5

Source: https://habr.com/ru/post/1537384/


All Articles