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:

&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:

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

with the following:

&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):

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

Hi, try:

sed -e '/\&LOG Part: "@DB\/TC.*\/.\{3\}"/{;N;/\n\&LOG *$/s/\(\n\&LOG\) */\1 Cloning_Action: RETAIN/g;}' file

Regards.

1 Like

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

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 #

Work fine with input given:

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

Try..

$  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"
$

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

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

vi plop
:set listchars=eol:$,tab:>-,trail:~,extends:>,precedes:<
:set list
&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...