Use matching value of a RegExp to name the output file.
        Posted  
        
            by fx42
        on Stack Overflow
        
        See other posts from Stack Overflow
        
            or by fx42
        
        
        
        Published on 2010-04-15T08:17:56Z
        Indexed on 
            2010/04/15
            8:23 UTC
        
        
        Read the original article
        Hit count: 310
        
I have this file "file.txt" which I want to split into many smaller ones. 
Each line of the file has an id field which looks like "id:1" for a line belonging to id 1.
For each id in the file, I like to create a file named idid.txt and put all lines that belong to this id in that file.
My brute force bash script solution reads as follows.
count=1
while [ $count -lt 19945 ]
do
  cat file.txt | grep "id:$count " >> ./sets/id$count.txt
  count='expr $count + 1'
done
Now this is very inefficient as I have do read through the file about 20.000 times. Is there a way to do the same operation with only one pass through the file? - What I'm probably asking for is a way to use the value that matches for a regular expression to name the associated output file.
© Stack Overflow or respective owner