awk to find pattern and add lines


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk to find pattern and add lines
# 1  
Old 01-19-2011
awk to find pattern and add lines

My file goes like this:

Code:
SID_LIST_HOSTNAME_LISTENER_3 =
  (SID_LIST =
    (SID_DESC =
      (SID_NAME = ORA0008)
      (ORACLE_HOME = /opt/oracle/product/ORA0008)
      (ENVS = "LD_LIBRARY_PATH=/opt/oracle/product/ORA0008/lib")
    )
    (SID_DESC =
      (SID_NAME = ORA0007)
      (ORACLE_HOME = /opt/oracle/product/ORA0007)
      (ENVS = "LD_LIBRARY_PATH=/opt/oracle/product/DBATST1/lib")
    )
    (SID_DESC =
      (SID_NAME = ORA0006)
      (ORACLE_HOME = /opt/oracle/product/ORA0006)
      (ENVS = "LD_LIBRARY_PATH=/opt/oracle/product/10.2.0.4.EE.2009Q2a/lib")
    )
..
..

I want use awk to add:

Code:
(SID_DESC =
      (SID_NAME = <SID>)
      (ORACLE_HOME = /opt/oracle/product/<SID>)
      (ENVS = "LD_LIBRARY_PATH=/opt/oracle/product/10.2.0.4.EE.2009Q2a/lib")
    )

immediately after:

Code:
SID_LIST_HOSTNAME_LISTENER_3 =
  (SID_LIST =

in my input file.

Can someone help me figure out a way to match 2 patterns and then add the lines..

TIA
Moderator's Comments:
Mod Comment
Please use code tags when posting data and code samples!

Last edited by vgersh99; 01-19-2011 at 03:58 PM.. Reason: code tags, please!
# 2  
Old 01-19-2011
Gotta be awk? I can do it in sed:
Code:
sed '
  s/regex_pattern.*/&\
add_line_1\
add_line_2\
add_line_3/
 ' infile >outfile

# 3  
Old 01-20-2011
This won't match pattern on 2 lines but on 1 line, i.e. if I use:

Code:
 
sed 's/^SID_LIST_.*=$/& \
   (SID_LIST =\
    (SID_DESC =\
      (SID_NAME = <ORACLE_SID>)\
      (ORACLE_HOME = \/opt\/oracle\/product\/<ORACLE_SID>)\
      )/'

The output shall be:
Code:
SID_LIST_HOSTNAME_LISTENER_3 =
(SID_LIST =
(SID_DESC =
(SID_NAME = <ORACLE_SID>)
(ORACLE_HOME = /opt/oracle/product/<ORACLE_SID>)
)
(SID_LIST =
(SID_DESC =
(SID_NAME = ORA0008)
(ORACLE_HOME = /opt/oracle/product/ORA0008)
(ENVS = "LD_LIBRARY_PATH=/opt/oracle/product/ORA0008/lib")
)
(SID_DESC =
(SID_NAME = ORA0007)
...
...
etc.

Whereas I need:
Code:
SID_LIST_HOSTNAME_LISTENER_3 =
(SID_LIST =
(SID_DESC =
(SID_NAME = <ORACLE_SID>)
(ORACLE_HOME = /opt/oracle/product/<ORACLE_SID>)
)
(SID_DESC =
(SID_NAME = ORA0008)
(ORACLE_HOME = /opt/oracle/product/ORA0008)
(ENVS = "LD_LIBRARY_PATH=/opt/oracle/product/ORA0008/lib")
)
(SID_DESC =
(SID_NAME = ORA0007)
...
...

Any clues?

---------- Post updated at 08:23 PM ---------- Previous update was at 08:03 PM ----------

Thi works:

Code:
 
sed '/^SID_LIST_.*=$/,/(SID_LIST =$/ s/(SID_LIST =$/& \
    (SID_DESC =\
      (SID_NAME = <ORACLE_SID>)\
      (ORACLE_HOME = \/opt\/oracle\/product\/<ORACLE_SID>)\
      )/'


Last edited by Scott; 01-20-2011 at 12:05 PM.. Reason: More code tags
# 4  
Old 01-20-2011
Code:
 
awk -v a1="$a" '/SID_LIST_HOSTNAME_LISTENER_3/ {print;c=NR;next} NR==c-1&&/\(SID_LIST/ {print;print a1;c=0;next}1' input_file

Where does "a" holds the needed replacement string.
# 5  
Old 01-20-2011
In sed, you can N when yoou see a promising first line to add the second line (or you can N your way for all lines 2-$ with two lines in the buffer) and then compare across the linefeed. In your case, I would pull all the "(SID_DESC =" through ")" lines into the buffer and then rework the buffer.
Code:
sed '
  /(SID_DESC =/{
    :loop
    N
    /\n *) *$/!b loop
    -- rework buffer substitutions --
   }
 '

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

awk to remove pattern and lines above pattern

In the awk below I am trying to remove all lines above and including the pattern Test or Test2. Each block is seperated by a newline and Test2 also appears in the lines to keep but it will always have additional text after it. The Test to remove will not. The awk executed until the || was added... (2 Replies)
Discussion started by: cmccabe
2 Replies

2. Shell Programming and Scripting

sed and awk to find pattern and add priffix

Original File Server1|poweredOn|268401| Server1/Server1.vmx|Red Hat Enterprise Linux 5 (64-bit) Need Output Server1|poweredOn|DR|T1|268401| Server1/Server1.vmx|Red Hat Enterprise Linux 5 (64-bit) Conduction to check find the string "SFCHT1" and "SR" and add prefix has... (4 Replies)
Discussion started by: ranjancom2000
4 Replies

3. UNIX for Beginners Questions & Answers

find pattern matches in consecutive lines in certain fields-awk

I have a text file with many thousands of lines, a small sample of which looks like this: InputFile:PS002,003 D -1 5 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 6 6 -1 -1 -1 -1 0 509 0 PS002,003 PSQ 0 1 7 18 1 0 -1 1 1 3 -1 -1 ... (5 Replies)
Discussion started by: jvoot
5 Replies

4. Shell Programming and Scripting

Find pattern; grep n lines before and after

Hi, I need help to grep a specific part of a log file (bold). 24/2/2017-16:57:17.056 frosti-1 M3UA-Tx: } 24/2/2017-16:57:17.056 frosti-1 M3UA-Tx: extensionContainer <Not Present> 24/2/2017-16:57:17.056... (8 Replies)
Discussion started by: vasil
8 Replies

5. Shell Programming and Scripting

Using awk or sed to find a pattern that has lines before and after it

Dear gurus, Please help this beginner to write and understand the required script. I am looking for useing awk for sed. I have a few thousand lines file whose contain are mostly as below and I am trying to achieve followings. 1. Find a string, say user1. Then hash the line containing the... (6 Replies)
Discussion started by: ran_bon_78
6 Replies

6. Shell Programming and Scripting

awk to skip lines find text and add text based on number

I am trying to use awk skip each line with a ## or # and check each line after for STB= and if that value in greater than or = to 0.8, then at the end of line the text "STRAND BIAS" is written in else "GOOD". So in the file of 4 entries attached. awk tried: awk NR > "##"' "#" -F"STB="... (6 Replies)
Discussion started by: cmccabe
6 Replies

7. Shell Programming and Scripting

sed -- Find pattern -- print remainder -- plus lines up to pattern -- Minus pattern

The intended result should be : PDF converters 'empty line' gpdftext and pdftotext?xml version="1.0"?> xml:space="preserve"><note-content version="0.1" xmlns:/tomboy/link" xmlns:size="http://beatniksoftware.com/tomboy/size">PDF converters gpdftext and pdftotext</note-content>... (9 Replies)
Discussion started by: Klasform
9 Replies

8. Shell Programming and Scripting

[awk] find pattern, change next two lines

Hi, hope you can help me... It seems like a straightforward problem, but I haven't had any success so far using my basic scripting and awk "skills": I need to find a pattern /VEL/ in an input file that looks like this: 1110SOL OW25489 1.907 7.816 26.338 -0.4365 0.4100 -0.0736 ... (3 Replies)
Discussion started by: origamisven
3 Replies

9. Shell Programming and Scripting

[Solved] Find duplicate and add pattern in sed/awk

<Update> I have the solution: sed 's/\{3\}/&;&;---;4/' The thread can be marked as solved! </Update> Hi There, I'm working on a script processing some data from a website into cvs format. There is only one final problem left I can't find a solution. I've processed my file... (0 Replies)
Discussion started by: lolworlds
0 Replies

10. Shell Programming and Scripting

Joining lines and find pattern

I have a file that does not have much formatting. I would like every time it finds a tag (something that starts with < and end with > like <Tag1> ) to be at the beginning of the line and joined with the next line. I tried sed with append and can not get it right and also hold and get in sed... (4 Replies)
Discussion started by: quixoticking11
4 Replies
Login or Register to Ask a Question