The UNIX and Linux Forums  

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 here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
grep the line only if next line matches pbsrinivas Shell Programming and Scripting 11 07-10-2007 05:51 AM
Printing the following line that matches an string jgarcia Shell Programming and Scripting 7 05-09-2007 10:31 PM
Select matches between line number and end of file? Jerrad Shell Programming and Scripting 4 05-24-2006 04:50 AM
Appending to filename a string of text grep finds HLee1981 Shell Programming and Scripting 3 09-06-2005 11:44 AM
String in text matches folder name shackman66 Shell Programming and Scripting 2 05-05-2004 02:46 PM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 06-16-2005
Registered User
 

Join Date: Feb 2004
Posts: 91
Question Appending Text To Each Line That Matches Grep

I'm currently digging for a way to append a line to a text file where each line begins with the word "setmqaut". This is a continuation of my IBM MQSeries backup script I'm working on to make my life a little easier.

What I would like to do is have each line that looks like this:
setmqaut -m TEST -n SYSTEM.DEFAULT.PROCESS -t process -g root +inq +set +chg +dlt +dsp

Look like this:
setmqaut -m TEST -n SYSTEM.DEFAULT.PROCESS -t process -g root +inq +set +chg +dlt +dsp >> $LOGFILE

I'm still trying to figure out how to do a grep and make a for go through and add this line to each line that starts with the setmqaut command. I'm guessing sed would be the best way to do this, but unfortunately I'm not as experienced as I'd like with it. Any suggestions would rock!

In the meantime, it's back to google for me.
Reply With Quote
Forum Sponsor
  #2  
Old 06-16-2005
vgersh99's Avatar
Moderator
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 3,029
sed '/setmqaut/s/$/>> $LOGFILE/' textFile
Reply With Quote
  #3  
Old 06-16-2005
Registered User
 

Join Date: Feb 2004
Posts: 91
Quote:
Originally Posted by vgersh99
sed '/setmqaut/s/$/>> $LOGFILE/' textFile
Thanks vgersh, this works excellent.

What I kept trying was this:

cat test.sh | sed '/setmqaut/ a\ linetoinsert' > demo.txt

And tried a whole bunch of things in place of a\ to see if it would append to the end of the line, but I can't seem to find a simple option to put in it's place that would do it.
Reply With Quote
  #4  
Old 06-16-2005
Bughunter Extraordinaire
 

Join Date: May 2005
Location: In the leftmost byte of /dev/kmem
Posts: 1,262
What vger99 showed you was in short form a fundamental mechanism for regexps, which is mostly unknown and/or not used to its full capability:

Code:
/<expression1>/ {
                    command 1
                    command 2
                    ...
                  }
This will perform command 1 through command n only to lines, which match the expression. The expression itself does not necessarily have anything to do with the operations performed, it just acts as a filter to decide, onto which lines to apply your commands.

So his sed-oneliner reads in fact: "apply to all lines containing 'setmqaut' the following: replace the end-of-line ('$') with the string '>> ...'"

The a-subcommand gets used only, when you want to append a certain piece of text after a complete line.

bakunin
Reply With Quote
  #5  
Old 06-16-2005
Registered User
 

Join Date: Feb 2004
Posts: 91
Thumbs up

Quote:
Originally Posted by bakunin
What vger99 showed you was in short form a fundamental mechanism for regexps, which is mostly unknown and/or not used to its full capability:

Code:
/<expression1>/ {
                    command 1
                    command 2
                    ...
                  }
This will perform command 1 through command n only to lines, which match the expression. The expression itself does not necessarily have anything to do with the operations performed, it just acts as a filter to decide, onto which lines to apply your commands.

So his sed-oneliner reads in fact: "apply to all lines containing 'setmqaut' the following: replace the end-of-line ('$') with the string '>> ...'"

The a-subcommand gets used only, when you want to append a certain piece of text after a complete line.

bakunin
Thank you for the explanation. No wonder I was having such a hard time getting mine to work correctly.
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 05:12 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0