how to put searched results in different file

December 4, 2011 at 22:05:59
Specs: Windows Vista
i got a file in unix with dilimiter"|"
eg. aaaaaaaaa
bbbbbbbbdddd | aaaaaaaadcdcd
vdsdsv | ccccddd | aaaaaaddffd|
nnnn |
i want all records which start with 'a' in seperate file,start with 'b' in seperate file and rest in third file.
i have tried grep command but it only returns perticular line i.e it does not return records which contains space or which are continued to next line.........

See More: how to put searched results in different file

Report •

December 5, 2011 at 07:51:25
I may have misinterpreted what you want. What constitutes a record??

3 egrep commands can do what you want:

egrep "^a" data.txt > afile.txt
egrep "^b" data.txt > bfile.txt
egrep -v "^a|^b" data.txt > cfile.txt # print out linses that don't start with a or b

Or you might consider an awk program: (use nawk for Solaris):

rm -f afile.txt bfile.txt cfile.txt
awk ' {
   x=gsub("^[a]", "", $0)
   if(x > 0)
      print save >> "afile.txt"

   x=gsub("^[b]", "", $0)
   if(x > 0)
      print save >> "bfile.txt"

   # print everything else
   print save >> "cfile.txt"

} ' data.txt

Report •
Related Solutions

Ask Question