SOLVED echo newline to output file

Code junkies hangout here

Moderators: ChrisThornett, LXF moderators

SOLVED echo newline to output file

Postby purplepenguin » Tue Jun 04, 2013 10:17 am

Hello

I've been working on a little script that searches my media server for .nfo files, extracts the title and plot information from said .nfo files and write the data to a txt file.

This is what I've got so far

Code: Select all
IFS=$'\n'

cd /home/$USER/Network/server/Videos/$1

for file in $( find . -iname '*.nfo' );
do cat $file | grep -e '<title>' -e '<plot>' >> ~/movielist.txt
done


I'am trying to make the output easier to read by putting a newline or line of ================= between the plot and the next title.

Sample output:

Code: Select all
 <title>Corpse Bride</title>
    <plot>Set in a 19th-century european village, this stop-motion animation feature follows the story of Victor, a young man whisked away to the underworld and wed to a mysterious corpse bride, while his real bride Victoria waits bereft in the land of the living.</plot>
    <title>WALL·E</title>
    <plot>WALL-E is the last robot left on an Earth that has been


What I would like is :
Code: Select all
 <title>Corpse Bride</title>
    <plot>Set in a 19th-century european village, this stop-motion animation feature follows the story of Victor, a young man whisked away to the underworld and wed to a mysterious corpse bride, while his real bride Victoria waits bereft in the land of the living.</plot>

    <title>WALL·E</title>
    <plot>WALL-E is the last robot left on an Earth that has been


I have been trying to add an echo line to the script, but I'm not sure if echo is the right command to use or even where to put it.

Could anyone suggest how I could amend the script?

Thanks
PP
Last edited by purplepenguin on Tue Jun 04, 2013 8:34 pm, edited 1 time in total.
So it looks like I'm back to LinuxMint again. Why you ask? Because my machine smell nicer.

I'm not too sure I'd want a Mint and Cinnamon muffin with my brew though. Guess I'm lucky they don't run my local bakery.
purplepenguin
LXF regular
 
Posts: 130
Joined: Wed Oct 05, 2011 2:19 pm
Location: Blissfully at the Command Line

Postby nelz » Tue Jun 04, 2013 11:05 am

echo is indeed the command to use, place it on the line before "done"

Code: Select all
echo >> ~/movielist.txt

or
Code: Select all
echo "=====================" >> ~/movielist.txt
"Insanity: doing the same thing over and over again and expecting different results." (Albert Einstein)
User avatar
nelz
Site admin
 
Posts: 8502
Joined: Mon Apr 04, 2005 11:52 am
Location: Warrington, UK

Postby purplepenguin » Tue Jun 04, 2013 11:18 am

Thanks Nelz

I've tried:

Code: Select all
IFS=$'\n'

cd /home/$USER/Network/server/Videos/$1

for file in $( find . -iname '*.nfo' );
do cat $file | grep -e '<title>' -e '<plot>'
echo >> ~/movielist.txt
done


It has the desired effect. But the output goes to the terminal. The txt file is created but empty, It's as if the
Code: Select all
for file in $( find . -iname '*.nfo' );
do cat $file | grep -e '<title>' -e '<plot>'
goes to stdout and the
Code: Select all
echo
follows >> ~/movielist.txt

Hmm
So it looks like I'm back to LinuxMint again. Why you ask? Because my machine smell nicer.

I'm not too sure I'd want a Mint and Cinnamon muffin with my brew though. Guess I'm lucky they don't run my local bakery.
purplepenguin
LXF regular
 
Posts: 130
Joined: Wed Oct 05, 2011 2:19 pm
Location: Blissfully at the Command Line

Postby nelz » Tue Jun 04, 2013 1:01 pm

Because you're no longr redirecting the output of grep. With your code, the file shouldn't be empty, it should contain a newline for every file processed. Incidentally, the use of cat with grep is usually unnecessary and undesirable

Code: Select all
for file in $( find . -iname '*.nfo' ); do
grep -e '<title>' -e '<plot>' $file >> ~/movielist.txt
echo >>~/movielist.txt
done
"Insanity: doing the same thing over and over again and expecting different results." (Albert Einstein)
User avatar
nelz
Site admin
 
Posts: 8502
Joined: Mon Apr 04, 2005 11:52 am
Location: Warrington, UK

Postby purplepenguin » Tue Jun 04, 2013 8:31 pm

cheers Nelz

I was confusing ">>" with ">".
I forgot that ">>" means append to file. I was trying to incorporate the "echo into the main part of the script before the first ">> ~/movielist.txt"

Of course, the ">> ~/movielist.txt" can be used more than once as it appends to the file not over writes it. Doh.

I get it now.
Process the the first .nfo file, write data to file and append the echo. Then process the second .nfo append data to end of file. etc etc.

Thanks for your help.
So it looks like I'm back to LinuxMint again. Why you ask? Because my machine smell nicer.

I'm not too sure I'd want a Mint and Cinnamon muffin with my brew though. Guess I'm lucky they don't run my local bakery.
purplepenguin
LXF regular
 
Posts: 130
Joined: Wed Oct 05, 2011 2:19 pm
Location: Blissfully at the Command Line


Return to Programming

Who is online

Users browsing this forum: No registered users and 0 guests

cron