Linux Format forums Forum Index Linux Format forums
Help, discussion, magazine feedback and more
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

SOLVED echo newline to output file

 
Post new topic   Reply to topic    Linux Format forums Forum Index -> Programming
View previous topic :: View next topic  
Author Message
purplepenguin
LXF regular


Joined: Wed Oct 05, 2011 3:19 pm
Posts: 121
Location: Blissfully at the Command Line

PostPosted: Tue Jun 04, 2013 11:17 am    Post subject: SOLVED echo newline to output file Reply with quote

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:
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:
 <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:
 <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
_________________
Debian Testing 64bit KDE i5 CPU 8GB RAM

Firefox 29 wont let me move the Reload and Back buttons. It's my computer Mozilla not yours.


Last edited by purplepenguin on Tue Jun 04, 2013 9:34 pm; edited 1 time in total
Back to top
View user's profile Send private message
nelz
Site admin


Joined: Mon Apr 04, 2005 12:52 pm
Posts: 8457
Location: Warrington, UK

PostPosted: Tue Jun 04, 2013 12:05 pm    Post subject: Reply with quote

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

Code:
echo >> ~/movielist.txt

or
Code:
echo "=====================" >> ~/movielist.txt

_________________
"Insanity: doing the same thing over and over again and expecting different results." (Albert Einstein)
Back to top
View user's profile Send private message
purplepenguin
LXF regular


Joined: Wed Oct 05, 2011 3:19 pm
Posts: 121
Location: Blissfully at the Command Line

PostPosted: Tue Jun 04, 2013 12:18 pm    Post subject: Reply with quote

Thanks Nelz

I've tried:

Code:
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:
for file in $( find . -iname '*.nfo' );
do cat $file | grep -e '<title>' -e '<plot>'
goes to stdout and the
Code:
echo
follows >> ~/movielist.txt

Hmm
_________________
Debian Testing 64bit KDE i5 CPU 8GB RAM

Firefox 29 wont let me move the Reload and Back buttons. It's my computer Mozilla not yours.
Back to top
View user's profile Send private message
nelz
Site admin


Joined: Mon Apr 04, 2005 12:52 pm
Posts: 8457
Location: Warrington, UK

PostPosted: Tue Jun 04, 2013 2:01 pm    Post subject: Reply with quote

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:
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)
Back to top
View user's profile Send private message
purplepenguin
LXF regular


Joined: Wed Oct 05, 2011 3:19 pm
Posts: 121
Location: Blissfully at the Command Line

PostPosted: Tue Jun 04, 2013 9:31 pm    Post subject: Reply with quote

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.
_________________
Debian Testing 64bit KDE i5 CPU 8GB RAM

Firefox 29 wont let me move the Reload and Back buttons. It's my computer Mozilla not yours.
Back to top
View user's profile Send private message
View previous topic :: View next topic  
Display posts from previous:   
Post new topic   Reply to topic    Linux Format forums Forum Index -> Programming All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
Linux Format forums topic RSS feed 


Powered by phpBB © 2001, 2005 phpBB Group


Copyright 2011 Future Publishing, all rights reserved.


Web hosting by UKFast