I have a file, xxx.lst with the following data:

aaa 111  
bbb 222  
ccc 333  

I need to make 3 files called 111.lst, 222.lst and 333.lst respectively with aaa in 111.lst bbb in 222.lst and ccc in 333.lst

I tried something like this:

for x in $(cat xxx.lst|awk '{print $1}');do
    for y in $(cat xxx.lst|awk '{print $2}'|cut -d_ -f3|sort|uniq);do
        echo $x > $y.lst

but it gives me ccc in all 3 files


It should be possible with a single awk command

awk '{print $1 > $2".lst"}' xxx.lst
  • Yea, awesome answer, thanks. Somewhere in back of my mind i knew it is extremely simple. – user165435 Apr 12 '16 at 13:40

You can do something like this:

#!/usr/bin/env bash

while read var1 var2; do
    echo "$var1" > "$var2".lst
done < xxx.lst

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.