Hi, all

I use awk to extract rows from a text file:

    awk 'NR==FNR{vals[$1];next} ($1) in vals' indiv.txt file1.txt > new_file1.txt

But how can I use the same code for multiple files ?

I can use :

    awk 'NR==FNR{vals[$1];next} ($1) in vals' indiv.txt file1.txt > new_file1.txt
    awk 'NR==FNR{vals[$1];next} ($1) in vals' indiv.txt file2.txt > new_file2.txt
    awk 'NR==FNR{vals[$1];next} ($1) in vals' indiv.txt file3.txt > new_file3.txt
    awk 'NR==FNR{vals[$1];next} ($1) in vals' indiv.txt file4.txt > new_file4.txt
    awk 'NR==FNR{vals[$1];next} ($1) in vals' indiv.txt file5.txt > new_file5.txt
    . . . .        .    .     .     .    .     .      .     .    .    .    .    .  
    awk 'NR==FNR{vals[$1];next} ($1) in vals' indiv.txt file29.txt > new_file29.txt

But there is some ways to do it automaticaly ? for example a loop or file*.txt > new_file*.txt.

Thanks!

Abdel
There is a variable called `FILENAME` which provide the current filename `awk` reads in. You can take this to create a filename `awk` should print its output.


    $ awk 'NR==FNR{vals[$1];next} ($1) in vals { print $0 >> "new_"FILENAME}' indiv.txt file1.txt file2.txt file3.txt

fin swimmer

