LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 09-14-2012, 02:26 PM   #1
ISStaras
LQ Newbie
 
Registered: Sep 2012
Posts: 9

Rep: Reputation: Disabled
How to get output to be added into a file instead of replacing the file


So i'm running this command;

Code:
grep -f match.dat config > test.txt
and lets say I have another config file, say called config2... so now I've created a .sh file that looks like this

Code:
grep -f match.dat config > test.txt
grep -f match.dat config2 > test.txt
but the ">" character makes output of the 2nd line replace the output of the first instead of just adding to the file.. I would also like there to be a separator between the 2 outputs so I can clearly see where first ends and second starts.. lets say the output looks like this:

Code:
environment=${devel:env}
devel.ldapConfig.url=ldap\://155.165.192.12\:389
devel.ldapPool.timeBetweenEvictionRunsMillis=1234556
devel.ldapPool.initialPoolSize=2
devel.ldapPool.maxActiveConnections=2
and would like the test.txt file to look like this after both outputs are added:
Code:
-sh-3.2$ cat test.txt
config
environment=${devel:env}
devel.ldapConfig.url=ldap\://155.165.192.12\:389
devel.ldapPool.timeBetweenEvictionRunsMillis=1234556
devel.ldapPool.initialPoolSize=2
devel.ldapPool.maxActiveConnections=2

config2
environment=${devel:env}
devel.ldapConfig.url=ldap\://155.165.192.13\:389
devel.ldapPool.timeBetweenEvictionRunsMillis=777
devel.ldapPool.initialPoolSize=5
devel.ldapPool.maxActiveConnections=5

I'm definitely a newbie so any help would be appreciated, thanks!
 
Old 09-14-2012, 02:29 PM   #2
TobiSGD
Moderator
 
Registered: Dec 2009
Location: Germany
Distribution: Whatever fits the task best
Posts: 17,131
Blog Entries: 2

Rep: Reputation: 4833Reputation: 4833Reputation: 4833Reputation: 4833Reputation: 4833Reputation: 4833Reputation: 4833Reputation: 4833Reputation: 4833Reputation: 4833Reputation: 4833
If you don't want the text to be replaced you have to use >> instead of >, so that your script looks like this
Code:
echo "config" > test.txt
grep -f match.dat config >> test.txt
echo "\nconfig2" >> test.txt
grep -f match.dat config2 >> test.txt
 
1 members found this post helpful.
Old 09-14-2012, 02:35 PM   #3
ISStaras
LQ Newbie
 
Registered: Sep 2012
Posts: 9

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by TobiSGD View Post
If you don't want the text to be replaced you have to use >> instead of >, so that your script looks like this
Code:
echo "config" > test.txt
grep -f match.dat config >> test.txt
echo "\nconfig2" >> test.txt
grep -f match.dat config2 >> test.txt
How can I insert a line break between the two? Also, what's the \n for?
 
Old 09-14-2012, 02:36 PM   #4
TobiSGD
Moderator
 
Registered: Dec 2009
Location: Germany
Distribution: Whatever fits the task best
Posts: 17,131
Blog Entries: 2

Rep: Reputation: 4833Reputation: 4833Reputation: 4833Reputation: 4833Reputation: 4833Reputation: 4833Reputation: 4833Reputation: 4833Reputation: 4833Reputation: 4833Reputation: 4833
the \n is the linebreak.
 
1 members found this post helpful.
Old 09-14-2012, 02:42 PM   #5
ISStaras
LQ Newbie
 
Registered: Sep 2012
Posts: 9

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by TobiSGD View Post
the \n is the linebreak.

I see, not sure what i'm doing wrong but this is what i'm getting:


Code:
-sh-3.2$ cat report.sh
echo "config" > test.txt
grep -f match.dat config >> test.txt
echo "\nconfig2" >> test.txt
grep -f match.dat config2 >> test.txt
-sh-3.2$
-sh-3.2$ ./report.sh
-sh-3.2$ cat test.txt
config
environment=${devel:env}
devel.ldapConfig.url=ldap\://155.165.192.12\:389
devel.ldapPool.timeBetweenEvictionRunsMillis=1234556
devel.ldapPool.initialPoolSize=2
devel.ldapPool.maxActiveConnections=2
\nconfig2
environment=${devel:env}
devel.ldapConfig.url=ldap\://155.165.192.32\:389
devel.ldapPool.timeBetweenEvictionRunsMillis=3334556
devel.ldapPool.initialPoolSize=5
devel.ldapPool.maxActiveConnections=5
 
Old 09-14-2012, 02:48 PM   #6
TobiSGD
Moderator
 
Registered: Dec 2009
Location: Germany
Distribution: Whatever fits the task best
Posts: 17,131
Blog Entries: 2

Rep: Reputation: 4833Reputation: 4833Reputation: 4833Reputation: 4833Reputation: 4833Reputation: 4833Reputation: 4833Reputation: 4833Reputation: 4833Reputation: 4833Reputation: 4833
Sorry, my error. If you want that echo can use the backslash characters, like \n, it has to be started with the -e option. Change that line to
Code:
echo -e "\nconfig2" >> test.txt
 
1 members found this post helpful.
Old 09-14-2012, 02:52 PM   #7
ISStaras
LQ Newbie
 
Registered: Sep 2012
Posts: 9

Original Poster
Rep: Reputation: Disabled
Talking

Quote:
Originally Posted by TobiSGD View Post
Sorry, my error. If you want that echo can use the backslash characters, like \n, it has to be started with the -e option. Change that line to
Code:
echo -e "\nconfig2" >> test.txt

Awesome! thanks TobiSGD!
 
Old 09-16-2012, 07:56 AM   #8
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958
I suggest that it's more readable just to use another separate echo.

Code:
echo "config" > test.txt
grep -f match.dat config >> test.txt
echo >> test.txt
echo "config2" >> test.txt
grep -f match.dat config2 >> test.txt
It's also better because there are NO standardized options in echo. POSIX doesn't specify any, so each shell has its own interpretation of them. When you need full portability, printf is the preferred choice.

Last edited by David the H.; 09-16-2012 at 07:57 AM.
 
1 members found this post helpful.
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Mencoder adds the extension of the original file to the output file ex: file.flv.mp4 linuxlicious Linux - General 2 04-17-2012 03:22 PM
Mencoder adds the extension of the original file to the output file ex: file.flv.mp4 linuxlicious Linux - General 1 04-15-2012 05:07 AM
[SOLVED] Sed – how do I save output to file with filename from content of another file? misarab Programming 4 02-28-2011 03:12 PM
sed -- replacing a string in a file with the contents of another file? CGP314 Linux - Newbie 10 06-17-2010 05:29 PM


All times are GMT -5. The time now is 06:49 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration