3

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
    done
done

but it gives me ccc in all 3 files

0

2 Answers 2

4

It should be possible with a single awk command

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

You can do something like this:

#!/usr/bin/env bash

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

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.