4
1
Alright, so I have a file full of thousands of strings. Each one on it's own line. I want to make a script that will allow me to take this file, call it list.txt
, and take the items from each line, and place it into separate files based on the first letter or number. As an example, say the first few lines of the file are like this:
cheese
pizza
pepperoni
lettuce
grahamCrackers
0-0Foods
chicken
lentils
1-2Items
I need to break it down into these:
c.txt
cheese
chicken
g.txt
grahamCrackers
l.txt
lettuce
lentils
p.txt
pizza
pepperoni
0.txt
0-0Foods
1.txt
1-2Items
I would like to accomplish this with BASH, on OS X. Thanks.
Oh, if it helps. Items on each line will NEVER have a space, they will always be contained as one word. E.G. (Never Chicken Soup, instead Chicken-Soup)
I just tried using that exact code and just replaced list.txt with my file. However, it took awhile, but after it was done. Nothing happened. Was I suppose to do something else? – Josiah – 2013-02-02T18:19:39.017
No, it works for me out of the box. Try 'set -x' and then run the command, and pastebin the output, perhaps I can help then. – KoviRobi – 2013-02-02T18:23:05.877
One thing though, is there a way you can put these outputed files in a folder first. – Josiah – 2013-02-02T18:25:34.480
1Yes, 'mkdir' that folder first, then change '${i:0:1}.txt' to 'folder/${i:0:1}.txt' – KoviRobi – 2013-02-02T18:26:05.473
Works great, thanks a lot. But I have one more for you... (I can't make this too easy. ;) ) Say I want to divide based on the first two letters, can that be easily accomplished? – Josiah – 2013-02-02T18:34:35.710
Never mind, I got it. Thanks a lot for your help. I really appreciate it. – Josiah – 2013-02-02T18:48:04.043
3Whoa... this is much more elegant than the grep/sed-based solution I was going to write up. It's always surprising what bash can do by itself. With that said, you can avoid messing around with $IFS & generally simplify things by using a while loop instead of that for loop. Replace
for i in "${file[@]}"; do
withwhile read i; do
, and replacedone
withdone <list.txt
, and then you can ditch all the stuff outside of the loop. – evilsoup – 2013-02-02T18:56:38.463@evilsoup you can't beat
gawk
for simplicity when it comes to this sort of thing. See my answer for an example. – terdon – 2013-02-02T21:12:02.890