View Full Version : Altering a text file?
cuddles71 09-30-2008, 01:31 PM Afternoon folks. I'm writing a small bash script to automate some backend stuff for my site. Trouble is, I'm stuck at the final point.
Basically, I have a text file about 8,000 lines long. I need to insert a specific string every 92 lines. How can I do this?
Thanks in advance for the help!
It would be better to give some example data, to understand your requirement better. Checkout if sed can help you in the meantime.
cuddles71 09-30-2008, 01:47 PM Well, I've tried sed, and the closest I can come up with is "inserting a blank line every 5 lines".
Close, but not quite what I want.
As for the data, it's a playlist. A few lines from the text file would be:
/mnt/music/B/Bon Jovi/Bon Jovi - You Give Love A Bad Name.mp3
/mnt/music/E/Elton John/Elton John - Ticking.mp3
/mnt/music/L/Led Zeppelin/Led Zeppelin - Going to California.mp3
/mnt/music/P/Poison/Poison - Lay Your Body Down.mp3
/mnt/music/S/Space Monkey/Space Monkey - Only The Night.mp3
/mnt/music/A/Alice DeeJay/Alice DeeJay - Better Off Alone.mp3
/mnt/music/C/Cyndi Lauper/Cyndi Lauper - Lets Hear It For The Boys.mp3
/mnt/music/G/Garth Brooks/Garth Brooks - Mr. Right.mp3
Every 92 lines or so I need to insert a line like:
/mnt/audio/KJSR/Station_IDs/Charlie - Station ID.mp3
split command can split a big file into multiple small files.
Try to split your file to multiple files of 92 lines each.
http://www.oreillynet.com/linux/cmd/cmd.csp?path=s/split
For example, if the small files are: xaa, xab, xac and xad,
while joining, you can do something like:
cat xaa > finaldatafile
echo "/mnt/audio/KJSR/Station_IDs/Charlie - Station ID.mp3" >> finaldatafile
cat xab >> finaldatafile
echo "/mnt/audio/KJSR/Station_IDs/Charlie - Station ID.mp3" >> finaldatafile
cat xac>> finaldatafile
echo "/mnt/audio/KJSR/Station_IDs/Charlie - Station ID.mp3" >> finaldatafile
cat xad>> finaldatafile
There might be a much simpler solution, but this is what i can offer offhand.
Codebird 09-30-2008, 02:13 PM piece of advice is writing a php file that does what u need:
$i=1;
$newLine='/mnt/audio/KJSR/Station_IDs/Charlie - Station ID.mp3';
$lines=file(music_file_name);
$filePointer=fopen(music_file_name, w);//open the file for writing
foreach($lines as $line){
fwrite($filePointer, $line);//write the line you arrived to
if(($i%92)==0){
fwrite($filePointer, $newLine);//here you add the line
}
$i++; //you increment the counter
}
fclose($filePointer);
maybe the script needs some testing before, backup your file if u use it I wrote it fast.
040Hosting 09-30-2008, 02:16 PM My best guess.
awk 'ORS=NR%92?"\n":"\nyourlinetobeinserted\n"' yourinputfile > youroutputfile
cuddles71 09-30-2008, 02:27 PM My best guess.
I think this will work a wee bit better than splitting the file. ;) Testing it now, and I'll let you know how it turns out.
cuddles71 09-30-2008, 10:04 PM My best guess.
This worked, thanks!
040Hosting 10-01-2008, 03:32 AM You are welcome :)
Don't you love those one-liners :D:agree:
cuddles71 10-01-2008, 02:19 PM Definitely! :)
But, here's another one for you... Trying to pull selected lines from a running log, using sed, and pipe them to a different log. The line I have is:
tail -f /usr/local/autodj/logs/sc_trans.log | sed -n '/DECODE/p' >> /usr/local/autodj/logs/played.log
But for whatever reason, this gives me nothing. Any ideas?
040Hosting 10-01-2008, 02:22 PM Can you give an example of what you try to accomplish? i.e. source and expected destination output.
cuddles71 10-01-2008, 02:35 PM Certainly!
The source (sc_trans log) looks like:
<10/01/08@11:55:01> [STREAM] Sending stream information
<10/01/08@11:55:03> [MAIN] Title Updated
<10/01/08@11:56:24> [DECODE] Opened Shania Twain - I'm Gonna Getcha Good!.mp3
<10/01/08@11:58:00> [MAIN] Title Updated
<10/01/08@12:00:26> [DECODE] Opened Wicked - Finale.mp3
<10/01/08@12:02:03> [MAIN] Title Updated
<10/01/08@12:02:07> [DECODE] Opened Bow Wow Wow - I Want Candy.mp3
<10/01/08@12:03:43> [MAIN] Title Updated
<10/01/08@12:04:51> [DECODE] Opened Elton John - The Messenger.mp3
Ideally, I'd like the output (played.log) to be:
<10/01/08@14:20:05> Martha Davis - Same Old Boogie 1946.mp3
<10/01/08@14:22:39> Queen - Sweet Lady.mp3
<10/01/08@14:26:36> Tom Petty & The Heartbreakers - Learning To Fly.mp3
<10/01/08@14:30:36> Blue Rodeo - It Could Happen To You.mp3
But I don't mind if the "[DECODE] Opened" stay there or not. :)
040Hosting 10-01-2008, 02:42 PM If you dont mind the "[DECODE] Opened" to stayt why not do:
tail -f /usr/local/autodj/logs/sc_trans.log | grep "[DECODE]" >> /usr/local/autodj/logs/played.log
cuddles71 10-01-2008, 02:48 PM Same result as using sed. Works fine as long as I'm not trying to output to a file. Add the >> /usr/local/autodj/logs/played.log though, and I get nothing.
040Hosting 10-01-2008, 02:56 PM Same result as using sed. Works fine as long as I'm not trying to output to a file. Add the >> /usr/local/autodj/logs/played.log though, and I get nothing.
Okay.. so it seems to be in your output ... let me test this...
Btw..
tail -f /usr/local/autodj/logs/sc_trans.log | awk 'sub(/.\[DECODE\] Opened /," ")'
Would give the output you wanted. Not sure if a tail -f allows output redirection, have to check on that.
cuddles71 10-01-2008, 03:04 PM No joy. Just using:
tail -f /usr/local/autodj/logs/sc_trans.log | awk 'sub(/.\[DECODE\] Opened /," ")'
Gives no output, period.
And tail -f does allow redirection. :) I can do:
tail -f /usr/local/autodj/logs/sc_trans.log >> /usr/local/autodj/logs/played.log
and it works just fine.
040Hosting 10-01-2008, 03:21 PM Seems to have to do with buffers; not sure how to fix this in awk yet.. but for grep it works like this:
tail -f yourfile |grep --line-buffered "[DECODE]" > outputfile
040Hosting 10-01-2008, 03:27 PM No joy. Just using:
tail -f /usr/local/autodj/logs/sc_trans.log | awk 'sub(/.\[DECODE\] Opened /," ")'
Gives no output, period.
And tail -f does allow redirection. :) I can do:
tail -f /usr/local/autodj/logs/sc_trans.log >> /usr/local/autodj/logs/played.log
and it works just fine.
I used the input as you gave it and it shows me this:
# tail -f t | awk 'sub(/.\[DECODE\] Opened /," ")'
<10/01/08@11:56:24> Shania Twain - I'm Gonna Getcha
<10/01/08@12:00:26> Wicked - Finale.mp3
<10/01/08@12:02:07> Bow Wow Wow - I Want Candy.mp3
<10/01/08@12:04:51> Elton John - The Messenger.mp3
However if you redirect that again to a file it indeed doesnt show output.. See my previous message about grep; thats the best i seem to get up with in such a short time.
cuddles71 10-01-2008, 05:13 PM Oh, there's no rush. :) This is just a side project while we get everything else set up on the station.
And thanks for all the help so far!
040Hosting 10-01-2008, 05:15 PM Can you confirm this is working for you ?
tail -f yourfile |grep --line-buffered "[DECODE]" > outputfile
cuddles71 10-01-2008, 06:31 PM Well, it's writing to the output file, but it's writing the wrong lines...
<10/01/08@18:28:31> [STREAM] Creating stream socket
<10/01/08@18:28:31> [STREAM] Connected to host server
<10/01/08@18:28:31> [STREAM] Disconnecting from stream host [waiting 10s]
Of course, there's a live DJ on right now, so the autoDJ is cycling. :) I can try it again in about half an hour when we switch back to the autoDJ.
cuddles71 10-01-2008, 07:35 PM It works! It's outputting this:
<10/01/08@19:22:23> [DECODE] Opened Blue Rodeo - Lost Together.mp3
<10/01/08@19:27:47> [DECODE] Opened Elton John - Look Ma No Hands.mp3
<10/01/08@19:32:08> [DECODE] Opened Jimmie Durante - Inka Dinka Doo.mp3
<10/01/08@19:33:46> [DECODE] Opened Nana Kitade - Things Left Behind.mp3
040Hosting 10-06-2008, 07:44 AM Got a better one for you ... couldn't accept i didn't know how to get it done.. well found a way to get the STDOUT into a file... with a tail -f and a awk
tail -f INPUTFILENAME | awk '{FS="Opened "} { system("echo "$2" >> OUTPUTFILENAME") }'
Obviously you have to replace INPUTFILENAME and OUTPUTFILENAME with the names you want to use...
|