The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Shell script getting input from output aspect_p Shell Programming and Scripting 1 03-11-2008 07:37 AM
Unix Shell Script with output to CSV File heather.morton@ UNIX for Advanced & Expert Users 1 01-28-2008 01:18 PM
redirecting SQL output from within a shell script soliberus Shell Programming and Scripting 2 11-23-2007 08:29 AM
reading command output from shell script sri b Shell Programming and Scripting 4 11-16-2006 12:52 AM
Capturing shell script command output designflaw Shell Programming and Scripting 2 03-01-2006 04:24 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 09-19-2008
sharpi03 sharpi03 is offline
Registered User
  
 

Join Date: Sep 2008
Posts: 5
Question Shell Script Output

I'm trying to automate numerous grep searches via shell scripting. However, the output of echo is ONE line whereas grepping from the command line would give line breaks as seen in the file. For example:

test.txt:
12345 - hi
12345 - how
54321 - are
12345 - you

grep output would be:
>grep "12345" test.txt
12345 - hi
12345 - how
12345 - you

using echo within my script to display the grep:
>echo `grep "12345" test.txt`
12345 - hi 12345 - how 12345 - you

Is there anyway to fix that last output to match the previous, i.e. with line breaks rather than all on one line? Or is there some other method?

Thanks!
  #2 (permalink)  
Old 09-19-2008
frank_rizzo frank_rizzo is offline Forum Advisor  
Resident BOFH
  
 

Join Date: Dec 2007
Posts: 427
if grep gives you the output you want why do you want to use it echo?
  #3 (permalink)  
Old 09-19-2008
DeCoTwc DeCoTwc is offline
Registered User
  
 

Join Date: Mar 2008
Location: NYC
Posts: 74
to get the results you're looking for wrap quotes around it:

Code:
echo "`grep "12345" test`"
I'd imagine that if this is in a script, it may be the case that it's preferable to echo the results of an earlier grep than to waste resources grepping the same thing repeatedly...

Last edited by DeCoTwc; 09-19-2008 at 10:33 PM.. Reason: fixed code tag
  #4 (permalink)  
Old 09-19-2008
cfajohnson's Avatar
cfajohnson cfajohnson is offline Forum Advisor  
Shell programmer, author
  
 

Join Date: Mar 2007
Location: Toronto, Canada
Posts: 2,361
Quote:
Originally Posted by DeCoTwc View Post
to get the results you're looking for wrap quotes around it:

Code:
echo "`grep "12345" test`"
I'd imagine that if this is in a script, it may be the case that it's preferable to echo the results of an earlier grep than to waste resources grepping the same thing repeatedly...

Whether it is in a script or not, the output is the same as, but slower to execute than:

Code:
grep 12345 test

In all shells but ksh93, command substitution is a slow operation.

  #5 (permalink)  
Old 09-19-2008
DeCoTwc DeCoTwc is offline
Registered User
  
 

Join Date: Mar 2008
Location: NYC
Posts: 74
Quote:
Originally Posted by cfajohnson View Post

Whether it is in a script or not, the output is the same as, but slower to execute than:

Code:
grep 12345 test

In all shells but ksh93, command substitution is a slow operation.

You know what, for some reason I was thinking in terms of him echoing a variable that held the results of an earlier grep...so yeah, echoing it doesn't seem to make a whole lot of sense I suppose.
Closed Thread

Bookmarks

Tags
echo, grep

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 05:19 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0