Appending Text To Each Line That Matches Grep


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Appending Text To Each Line That Matches Grep
# 1  
Old 06-16-2005
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! Smilie

In the meantime, it's back to google for me. Smilie
# 2  
Old 06-16-2005
sed '/setmqaut/s/$/>> $LOGFILE/' textFile
# 3  
Old 06-16-2005
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.
# 4  
Old 06-16-2005
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
# 5  
Old 06-16-2005
MySQL

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. Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Match text to lines in a file, iterate backwards until text or text substring matches, print to file

hi all, trying this using shell/bash with sed/awk/grep I have two files, one containing one column, the other containing multiple columns (comma delimited). file1.txt abc12345 def12345 ghi54321 ... file2.txt abc1,text1,texta abc,text2,textb def123,text3,textc gh,text4,textd... (6 Replies)
Discussion started by: shogun1970
6 Replies

2. Shell Programming and Scripting

Appending a text to the top of each line

Platform : Oracle Linux 6.8 Shell : bash I have a file which has lines like below. These are SELECT queries (SQL) In each line, I want the word just after FROM keyword to be copied and printed on the top along with the word PROMPT. The words after FROM clause below are table names. So, they... (6 Replies)
Discussion started by: John K
6 Replies

3. Shell Programming and Scripting

Print line if values in fields matches number and text

datafile: 2017-03-24 10:26:22.098566|5|'No Route for Sndr:RETEK RMS 00040 /ZZ Appl:PF Func:PD Txn:832 Group Cntr:None ISA CntlNr:None Ver:003050 '|'2'|'PFI'|'-'|'EAI_ED_DeleteAll'|'EAI_ED'|NULL|NULL|NULL|139050594|ActivityLog| 2017-03-27 02:50:02.028706|5|'No Route for... (7 Replies)
Discussion started by: SkySmart
7 Replies

4. Shell Programming and Scripting

Using regex's from file1, print line and line after matches in file2

Good day, I have a list of regular expressions in file1. For each match in file2, print the containing line and the line after. file1: file2: Output: I can match a regex and print the line and line after awk '{lines = $0} /Macrosiphum_rosae/ {print lines ; print lines } ' ... (1 Reply)
Discussion started by: pathunkathunk
1 Replies

5. Shell Programming and Scripting

Increasing a number and appending it to next line of a text file

Hi all, I have text file having a number P100. what i need is when i run a script, it should add 1 to the above number and append it to the next line of a same text file.. when i use the script next time it should check the last line and add 1 to the last number and so on.. like the text... (5 Replies)
Discussion started by: smarty86
5 Replies

6. Shell Programming and Scripting

grep a line from a text file

Hi all, I need to grep a line from a log file which ensures me that the application server script is executed successfully. Some body please help me on this. I also need to write a while loop in which i need to use the status of the above grep output. Could some one please tell me how to use... (12 Replies)
Discussion started by: firestar
12 Replies

7. UNIX for Dummies Questions & Answers

| help | unix | grep - Can I use grep to return a string with exactly n matches?

Hello, I looking to use grep to return a string with exactly n matches. I'm building off this: ls -aLl /bin | grep '^.\{9\}x' | tr -s ' ' -rwxr-xr-x 1 root root 632816 Nov 25 2008 vi -rwxr-xr-x 1 root root 632816 Nov 25 2008 view -rwxr-xr-x 1 root root 16008 May 25 2008... (7 Replies)
Discussion started by: MykC
7 Replies

8. UNIX for Dummies Questions & Answers

Using grep to move files that contain a line of text

I have a folder with about 4000 files in it. I need to extract the files that contain a certain line of text to another directory. for example files with 'ns1.biz.rr.com' need to be extracted to the directory above or some other directory. I tried using the following but was not successful. (6 Replies)
Discussion started by: spartan22
6 Replies

9. Shell Programming and Scripting

grep the line only if next line matches

Hi I have an Input of following sort AAAA: ProgName="PROGRAM" BBBB: ProgName="BBBBBB" CCCC: DDDD: ProgName="PROGRAM" SSSS: ProgName="PROGRAM" ZZZZ: ProgName="PROGRAM" I want to find the Lines which are followed by ProgName="PROGRAM" Out Put AAAA: (11 Replies)
Discussion started by: pbsrinivas
11 Replies

10. Shell Programming and Scripting

Appending to filename a string of text grep finds

I am wanting to automate a process that includes the step of appending to a filename a string of text that's contained inside the file. I.e. if filename A.fileA contains a string of text that reads 1234 after the phrase ABC, I want the shell script file to rename the file 1234_FileChecked_A.fileA.... (3 Replies)
Discussion started by: HLee1981
3 Replies
Login or Register to Ask a Question