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! :slight_smile:

In the meantime, it's back to google for me. :slight_smile:

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.

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:

/<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. :wink: