create txt file form data file

File A.txt

LL07 LL07_B_1 20	
LL85 LL85_A_1 40	
LL85 LL85_B_1 40	
LL85 LL85_C_1 30	
LL37 LL37_A_1 60	
LL37 LL37_B_1 20	
LL37 LL37_C_1 50	

I want cretae diffrent tex file base of above file

Should be threee text file

LL07.txt
LL85.txt
LL37.txt

Eaach text file have below data as per file A

LL07.txt

Hello
prl
Fi Na=LL07_B_1 no 20
bye
q

LL85.txt

Hello                    
prl                      
Fi Na=LL85_A_1 no=40     
Fi Na=LL85_B_1 no=40  
Fi Na=LL85_C_1 no=30  
bye                      
q

LL85.txt

Hello                    
prl                      
Fi Na=LL37_A_1 no=60     
Fi Na=LL37_B_1 no=20  
Fi Na=LL37_C_1 no=50  
bye
q                  

Thanks a lot in advance

for key in `awk ' { print $1 } ' A.txt | sort | uniq`
do
        echo "Hello\nprl" > ${key}.txt
        awk '/'$key'/ { print "Fi Na=" $2 " no=" $3 } ' A.txt >> ${key}.txt
        echo "bye\nq" >> ${key}.txt
done

Note: please use -e option for echo if you are using bash.

1 Like

All in awk:

awk '{ $1 = $1 ".txt"  ;if (a[$1]) { a[$1] = a[$1] "\n"; };   a[$1] = a[$1] "Fi Na=" $2 " no=" $3;} END {  for (i in a) { print "Hello\nprl\n" a "\nbye\nq" > i } }' A.txt
1 Like