LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   split files using awk (or similar) (https://www.linuxquestions.org/questions/programming-9/split-files-using-awk-or-similar-333061/)

lgualteri 06-13-2005 09:10 AM

split files using awk (or similar)
 
All,

I have a huge file like this:

aaa 222 444 rrr
aaa 234 123 lop
bbb 234 234 kio
bbb 567 345 jjj
ccc 765 567 ghj
ccc 544 678 fre
ccc 444 789 der

I basically need to split the file in a way that:

file aaa.txt shall contain only rows where the first field is aaa:
aaa 222 444 rrr
aaa 234 123 lop

file bbb.txt shall contain only rows where the first field is bbb:
bbb 234 234 kio
bbb 567 345 jjj

etc, etc.


I am sure this is simple and can be done using awk or c-shell script .........


many thanks,
Luca

keefaz 06-13-2005 09:17 AM

I don't know c-shell nor awk, but in bash :
Code:

while read line; do
    pre=`echo $line | cut -d' ' -f1`
    echo $line >> $pre.txt
done < file.txt

where file.txt is your huge file


All times are GMT -5. The time now is 10:08 PM.