Multiple line search, replace second line, using awk or sed


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Multiple line search, replace second line, using awk or sed
# 1  
Old 02-26-2014
[Solved] Multiple line search, replace second line, using awk or sed

All, I appreciate any help you can offer here as this is well beyond my grasp of awk/sed...

I have an input file similar to:

Code:
&LOG
&LOG Part: "@DB/TC10000021855/--F"
&LOG 
&LOG
&LOG Part: "@DB/TC10000021852/--F"
&LOG Cloning_Action: RETAIN
&LOG Part: "@DB/TCCP000010713/--A"
&LOG
&LOG Part: "@DB/TC10000024401/--E"
&LOG Cloning_Action: DEFAULT_DISP Naming_Technique: DEFAULT_NAMING Clone_Name: "@DB/TC42500004692/--A"
&LOG Container: user
&LOG Part_Type: object
&LOG Part_Name: "object-name"
&LOG Part_Description: "object-desc"

What I need to do is replace any instances of the following:

Code:
&LOG Part: "@DB/TCCP000010713/--A"
&LOG

with the following:

Code:
&LOG Part: "@DB/TCCP000010713/--A"
&LOG Cloning_Action: RETAIN

In pseudo code terms...

If a whole line matches: &LOG Part: "@DB/TCxxxxxxxxxxx/xxx" (where the "x"s could be any character)
and the next whole line matches: &LOG
replace the "next line" (&LOG) with: &LOG Cloning_Action: RETAIN

Note there may be multiple instances needing replacement within a given file as the sample used here indicates

When run properly the above input would come out looking like (the change is highlighted in red):

Code:
&LOG
&LOG Part: "@DB/TC10000021855/--F"
&LOG Cloning_Action: RETAIN
&LOG
&LOG Part: "@DB/TC10000021852/--F"
&LOG Cloning_Action: RETAIN
&LOG Part: "@DB/TCCP000010713/--A"
&LOG Cloning_Action: RETAIN
&LOG Part: "@DB/TC10000024401/--E"
&LOG Cloning_Action: DEFAULT_DISP Naming_Technique: DEFAULT_NAMING Clone_Name: "@DB/TC42500004692/--A"
&LOG Container: user
&LOG Part_Type: object
&LOG Part_Name: "object-name"
&LOG Part_Description: "object-desc"

Thanks in advance for any help you can offer...

Last edited by KarmaPoliceT2; 02-27-2014 at 10:45 AM.. Reason: resolved question
# 2  
Old 02-26-2014
Hi, try:
Code:
sed -e '/\&LOG Part: "@DB\/TC.*\/.\{3\}"/{;N;/\n\&LOG *$/s/\(\n\&LOG\) */\1 Cloning_Action: RETAIN/g;}' file

Regards.
This User Gave Thanks to disedorgue For This Post:
# 3  
Old 02-26-2014
Quote:
Originally Posted by disedorgue
Hi, try:
Code:
sed -e '/\&LOG Part: "@DB\/TC.*\/.\{3\}"/{;N;/\n\&LOG *$/s/\(\n\&LOG\) */\1 Cloning_Action: RETAIN/g;}' file

Regards.
This didn't seem to work, didn't seem to do anything at all:

Code:
linux:/new/clones # sed -e '/\&LOG Part: "@DB\/TC.*\/.\{3\}"/{;N;/\n\&LOG *$/s/\(\n\&LOG\) */\1 Cloning_Action: RETAIN/g;}' DZ02620-03.clone_250 > temp
linux:/new/clones # diff temp DZ02620-03.clone_250
linux:/new/clones #

# 4  
Old 02-27-2014
Work fine with input given:
Code:
$ cat plop
&LOG
&LOG Part: "@DB/TC10000021855/--F"
&LOG
&LOG
&LOG Part: "@DB/TC10000021852/--F"
&LOG Cloning_Action: RETAIN
&LOG Part: "@DB/TCCP000010713/--A"
&LOG
&LOG Part: "@DB/TC10000024401/--E"
&LOG Cloning_Action: DEFAULT_DISP Naming_Technique: DEFAULT_NAMING Clone_Name: "@DB/TC42500004692/--A"
&LOG Container: user
&LOG Part_Type: object
&LOG Part_Name: "object-name"
&LOG Part_Description: "object-desc"
$ sed -e '/\&LOG Part: "@DB\/TC.*\/.\{3\}"/{;N;/\n\&LOG *$/s/\(\n\&LOG\) */\1 Cloning_Action: RETAIN/g;}' plop >plop2
$ diff plop plop2
3c3
< &LOG
---
> &LOG Cloning_Action: RETAIN
8c8
< &LOG
---
> &LOG Cloning_Action: RETAIN

Regards.
# 5  
Old 02-27-2014
Try..
Code:
$  sed -n 'H
> ${
> x
> s/\(&LOG Part: \"@DB\/TC.\{11\}\/...\"\n&LOG\) *\(\n\)/\1 Cloning_Action: RETAIN \2/g
> p
> }' infile

&LOG
&LOG Part: "@DB/TC10000021855/--F"
&LOG Cloning_Action: RETAIN
&LOG
&LOG Part: "@DB/TC10000021852/--F"
&LOG Cloning_Action: RETAIN
&LOG Part: "@DB/TCCP000010713/--A"
&LOG Cloning_Action: RETAIN
&LOG Part: "@DB/TC10000024401/--E"
&LOG Cloning_Action: DEFAULT_DISP Naming_Technique: DEFAULT_NAMING Clone_Name: "@DB/TC42500004692/--A"
&LOG Container: user
&LOG Part_Type: object
&LOG Part_Name: "object-name"
&LOG Part_Description: "object-desc"
$


Last edited by michaelrozar17; 02-27-2014 at 10:12 AM..
# 6  
Old 02-27-2014
Quote:
Originally Posted by disedorgue
Work fine with input given:
Code:
$ cat plop
&LOG
&LOG Part: "@DB/TC10000021855/--F"
&LOG
&LOG
&LOG Part: "@DB/TC10000021852/--F"
&LOG Cloning_Action: RETAIN
&LOG Part: "@DB/TCCP000010713/--A"
&LOG
&LOG Part: "@DB/TC10000024401/--E"
&LOG Cloning_Action: DEFAULT_DISP Naming_Technique: DEFAULT_NAMING Clone_Name: "@DB/TC42500004692/--A"
&LOG Container: user
&LOG Part_Type: object
&LOG Part_Name: "object-name"
&LOG Part_Description: "object-desc"
$ sed -e '/\&LOG Part: "@DB\/TC.*\/.\{3\}"/{;N;/\n\&LOG *$/s/\(\n\&LOG\) */\1 Cloning_Action: RETAIN/g;}' plop >plop2
$ diff plop plop2
3c3
< &LOG
---
> &LOG Cloning_Action: RETAIN
8c8
< &LOG
---
> &LOG Cloning_Action: RETAIN

Regards.
You're right, my apologies, it does work against this, which is really weird because i copy pasted it directly from the input file i am using (though it is a snippet of a larger file, it matches the pattern)...

I'll investigate a little more and see what i can find is different...

---------- Post updated at 09:30 AM ---------- Previous update was at 09:28 AM ----------

Quote:
Originally Posted by michaelrozar17
Try..
Code:
$  sed -n 'H
> ${
> x
> s/\(&LOG Part: \"@DB\/TC.\{11\}\/...\"\n&LOG\) *\(\n\)/\1 Cloning_Action: RETAIN \2/g
> p
> }' infile

&LOG
&LOG Part: "@DB/TC10000021855/--F"
&LOG Cloning_Action: RETAIN
&LOG
&LOG Part: "@DB/TC10000021852/--F"
&LOG Cloning_Action: RETAIN
&LOG Part: "@DB/TCCP000010713/--A"
&LOG Cloning_Action: RETAIN
&LOG Part: "@DB/TC10000024401/--E"
&LOG Cloning_Action: DEFAULT_DISP Naming_Technique: DEFAULT_NAMING Clone_Name: "@DB/TC42500004692/--A"
&LOG Container: user
&LOG Part_Type: object
&LOG Part_Name: "object-name"
&LOG Part_Description: "object-desc"
$

This also didn't seem to work on my real file... I wonder what's going on...

---------- Post updated at 09:37 AM ---------- Previous update was at 09:30 AM ----------

so what I've found is that the blank &LOG lines have a trailing space on them in my original file, but when i copy paste out of the forum here it does not have a trailing space... wondering if this might be the issue
Code:
vi plop
:set listchars=eol:$,tab:>-,trail:~,extends:>,precedes:<
:set list

Code:
&LOG Part: "@DB/TC10000035314/--G"$
&LOG Cloning_Action: RETAIN~$
&LOG Part: "@DB/TCCP000004574/--A"$
&LOG~$
&LOG Part: "@DB/TCCP100001225/--F"$
&LOG Cloning_Action: RETAIN~$


Yet another update... I ran the first above response against a snippet even with a blank space and it worked... investigation continues

---------- Post updated at 09:43 AM ---------- Previous update was at 09:37 AM ----------

damnit!! apparently something was in dos format... ran dos2unix on the file and now it works...

Thanks guys, sorry for my stupidity on the dos2unix...
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Multiple Line awk search and replace

I have a log file that contains many lines but contains the following line three times: related_pin : "t_bypass"; Here are the 3 occurrences and the two lines after from my file.txt: related_pin : "t_bypass"; sdf_cond : "rstq_b"; timing_sense : negative_unate; ... (6 Replies)
Discussion started by: bobbygb2003
6 Replies

2. Homework & Coursework Questions

sed Multiple Pattern search and delete the line

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: I have file which has got the following content sam 123 LD 41 sam 234 kp sam LD 41 kam pu sam LD 61 Now... (1 Reply)
Discussion started by: muchyog
1 Replies

3. Shell Programming and Scripting

sed command to replace a line at a specific line number with some other line

my requirement is, consider a file output cat output blah sdjfhjkd jsdfhjksdh sdfs 23423 sdfsdf sdf"sdfsdf"sdfsdf"""""dsf hellow there this doesnt look good et cetc etc etcetera i want to replace a line of line number 4 ("this doesnt look good") with some other line ... (3 Replies)
Discussion started by: vivek d r
3 Replies

4. Shell Programming and Scripting

SED : Replace whole line on multiple execution

Hi, I am have one file with a line group=project_live I need to replace it with line group=project_live_support before I execute some application related script. The potentianl problem is when I replace this with sed using command sed... (2 Replies)
Discussion started by: bhaskar_m
2 Replies

5. Shell Programming and Scripting

awk multiple-line search and replace one-liner

Hi I am trying to search and replace a multi line pattern in a php file using awk. The pattern starts with <div id="navbar"> and ends with </div> and spans over an unknown number of lines. I need the command to be a one liner. I use the "record separator" like this : awk -v... (8 Replies)
Discussion started by: louisJ
8 Replies

6. Shell Programming and Scripting

perl search and replace - search in first line and replance in 2nd line

Dear All, i want to search particular string and want to replance next line value. following is the test file. search string is tmp,??? ,10:1 "???" may contain any 3 character it should remain the same and next line replace with ,10:50 tmp,123 --- if match tmp,??? then... (3 Replies)
Discussion started by: arvindng
3 Replies

7. Shell Programming and Scripting

sed replace multiple occurrences on the same line, but not all

Hi there! I am really enjoying working with sed. I am trying to come up with a sed command to replace some occurrences (not all) in the same line, for instance: I have a command which the output will be: 200.300.400.5 0A 0B 0C 01 02 03 being that the last 6 strings are actually one... (7 Replies)
Discussion started by: ppucci
7 Replies

8. Shell Programming and Scripting

Does Sed Search/Replace Work For Multiple Matches On The Same Line?

Hello, I would like to delete all the footnotes in all my htm files. Hence, I have to delete the whole font tag pairs, i.e. deleting everything between the begin/end font tags. I create a testfile, of which data parts of all four lines are the same except for the number of font tag pairs,... (3 Replies)
Discussion started by: cibalo
3 Replies

9. UNIX for Dummies Questions & Answers

How to search and replace a particular line in file with sed command

Hello, I have a file and in that, I want to search for a aprticular word and then replace another word in the same line with something else. Example: In file abc.txt, there is a line <host oa_var="s_hostname">test</host> I want to search with s_hostname text and then replace test with... (2 Replies)
Discussion started by: sshah1001
2 Replies

10. Shell Programming and Scripting

sed search and replace in next line

Hello, I am hoping someone can provide some guidance on using context based search and replace to search for a pattern and then do a search and replace in the line that follows it. For example, I have a file that looks like this: <bold>bold text </italic> somecontent morecontent... (3 Replies)
Discussion started by: charissaf67
3 Replies
Login or Register to Ask a Question