The question is really simple, I have read all the questions and still cannot make it! I have a plain file like this
$cat file1.txt
ALA
AJD
KSF
And I want awk to use each of the values as regex to print lines from another file to another one:
$cat file2.txt
name,st,ed,le
ALA,10,12,12
ALA,2,5,4
ALA,6,5,8
AJD,5,8,7
KSF,5,8,7
So my script is
while read p;
awk -F"," 'NR==1{print $0}' file2.txt > $p.csv
awk -F"," '/$p/{print $0}' file2.txt >> $p.csv
done <file1.txt
And the desired output would be:
$cat ALA.csv
name,st,ed,le
ALA,10,12,12
ALA,2,5,4
ALA,6,5,8
$cat AJD.csv
name,st,ed,le
AJD,5,8,7
$cat KSF.csv
name,st,ed,le
KSF,5,8,7
Unfortunately, I only get the headers printed in each file. I have manually put each value from file1.txt replacing $p and it works perfectly. So I think the problem is that the variable $p is not been well interpreted. I tried with quotes, double simple. I tried also many different suggestions I found, but nothing seems to work!