Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Bucket Lines with same pattern to one file.....

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
HI All,

For example I have a file which contains the following,

1 CAT 34
2 CAT 33
3 CAT 88
4 DOG 90
5 DOG 12
6 PIG 34
7 PIG 33
8 PIG 25
:
:
Does anyone know how I can bucket the lines containing CAT and save it to one file named cat.txt, the same goes for DOG in dog.txt and for PIG in pig.txt?

I use bourne shell...

Thanks!
-CGLY
 
#! /bin/csh

set file = $1
foreach i ( `cut -d' ' -f2 $file |sort -u` )
set lc = `echo $i | sed -f lcase`
grep $i $file >! $lc.txt
end


--------

lcase is a file which contains 1 line

y/ABCDEFGHIJKLMNOPQRSTUVWXYZ/abcdefghijklmnopqrstuvwxyz/

-----

what the SED line does is make DOG -> dog and CAT -> cat. if you really don't care whether it is called

DOG.txt or dog.txt

you can eliminate this line. then the script would be....

#! /bin/csh

set file = $1
foreach i ( `cut -d' ' -f2 $file | uniq ` )
grep $i $file >! $i.txt
end

 
Hi tdatgod,

I am getting an error message, "cut : invalid delimeter"
Please advice...

Thanks,
CGLY
 

-d' space '

the post submission system makes it hard to represent a space.

Actually if the fields are separated by tabs that even better since the TAB is the default delimited for cut

cut -f2 $file

--

 


$i or $file isn't filed in properly and the grep is deteriating to

grep $i

or

grep $file

in which case it is waiting for input from STDIN.

you can debug csh scripts by adding -xv to the first line of the file

#! /bin/csh -xv


this will echo the script as it executes including all the substitutions.
 
I know CGLY mentioned bourne and tdatagod has a great csh solution but this is very easy with awk

#! /usr/bin/awk -f
$2 != "" { print > tolower($2) ".txt" }

Save those 2 lines to a script (chmod 700 ...) and run as:

myscript input.txt

I am assuming your path to awk is /usr/bin/awk Cheers,
ND [smile]

bigoldbulldog@hotmail.com
 
Hi ND,
One more thing, I am getting a message which says :
awk : too many output files...
this is when I have more than 10 files to be generated...
How can I resolve this???
Thanks!
CGLY
 
Another way in using bsh/ksh with a bit of awk would be

for i in `cat sourcefile | awk '{print $2}' | sort -u`
do
grep $i sourcefile >> $i.txt
done

MB.
 
sorry, I missed a line ...

for i in `cat soucefile | awk '{print $2}' | sort -u`
do
lower=`echo $i | tr [A-Z] [a-z]`
grep $i sourcefile >> ${lower}.txt
done
 
Try modifying bigoldbulldog's solution as follows.

2 != "" { fn = tolower($2)".txt"; print >> fn;close(fn) }

This is not the most efficient way of doing it, but it may work. CaKiwi
 
Hi MB, I am getting 2 or 3 error messages...
Hi CaKiWi, Same error with bigoldbulldog, it seems that the script can only look at at most 10 entries in the file if I added lines in the file more than 10, then I get the error message....

please advice,
CGLY
 
CGLY -

What system are you running? It seems you are only allowed 10 file desriptors open for a process. CaKiWi solved that problem (on my machine) and is right about the efficiency. If file is opened, written to, and closed for each line of input, this has got to be slow.

I'd try to get tdatagod or MB's solution to run. Show us your errors - or even a trace by running #! /bin/sh -x
or #! /usr/bin/csh -x Cheers,
ND [smile]

bigoldbulldog@hotmail.com
 
Hi ND, CaKiwi,

I'm not sure why it can only process upto 10 files in my system. Are there any alternative way to implement this?
Thanks....

Best Regards,
CGLY
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top