sed or awk script problem


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sed or awk script problem
# 1  
Old 12-15-2015
Wrench sed or awk script problem

Hi All

I am having a file as shown below

File1#

Code:
modyle 1 {
test jsj
hhsjh 13e3 
jsjjs 

}

memP(dbg_trace) {
ajjs
jsjs
jsjs

Test(claer) {
jsjs
}
TestStep ( RunTime ) {
RUN jksj
Controller (ddd) {
akk sns
sjsj
}
Controller (ddssd) {
akk sns
sjsj
}
Controller (ddssssd) {
akk sns
sjsj
}
}
}


modyless 2 {
test jsj
hhsjh 13e3 
jsjjs 

}


memP(dbg66_trace) {
ajjs
jsjs
jsjs

Test(claer) {
jsjs
}
TestStep ( RunTime ) {
RUN jksj
Controller (ddssd) {
akkss snss
sjssj
}
Controller (ddssssd) {
aksk snss
sssjsj
}
Controller (ssddssssd) {
askk ssns
ssjssj
}
}
}

modyless 3 {
test jsj
hhsjh 13e3 
jsjjs 

}

I am having another file as shown below

File 2 #


Code:
TestStep(RunExtra) {
      RunMode : RunTProg;
      Controller(WBP0) {
        ComparesGOID : On;
        ComparesGo : On;
          }
}

Now I want to insert File2 content in the File1 content
with a pattern match , Pattern match is complex

First we should match "memP" in File1 then it should check the last bracket "}" in memP wrapper and then insert the File Content "File2" before that "}"

Output File should be like this

File3 :

Code:
modyle 1 {
test jsj
hhsjh 13e3 
jsjjs 

}

memP(dbg_trace) {
ajjs
jsjs
jsjs

Test(claer) {
jsjs
}
TestStep ( RunTime ) {
RUN jksj
Controller (ddd) {
akk sns
sjsj
}
Controller (ddssd) {
akk sns
sjsj
}
Controller (ddssssd) {
akk sns
sjsj
}
}

TestStep(RunExtra) {
      RunMode : RunTProg;
      Controller(WBP0) {
        ComparesGOID : On;
        ComparesGo : On;
          }
}

}


modyless 2 {
test jsj
hhsjh 13e3 
jsjjs 

}


memP(dbg66_trace) {
ajjs
jsjs
jsjs

Test(claer) {
jsjs
}
TestStep ( RunTime ) {
RUN jksj
Controller (ddssd) {
akkss snss
sjssj
}
Controller (ddssssd) {
aksk snss
sssjsj
}
Controller (ssddssssd) {
askk ssns
ssjssj
}
}
TestStep(RunExtra) {
      RunMode : RunTProg;
      Controller(WBP0) {
        ComparesGOID : On;
        ComparesGo : On;
          }
}



}

modyless 3 {
test jsj
hhsjh 13e3 
jsjjs 

}

Colored one is new content which is being put

I tried below script
sed '/memP/ r File2' File1 > output.txt
But it is not selecting the last bracket in the memP wrapper

Please provide me your valuable suggestions
# 2  
Old 12-15-2015
How about
Code:
awk '
FNR == NR       {INS=INS DL $0
                 DL = ORS
                 next
                }
/^memP/         {C = 1
                }
C               {BRACNT += gsub (/{/, "&") - gsub (/}/, "&")
                }
C && !BRACNT    {print INS
                 C = 0
                }
1
' file2 file1

This User Gave Thanks to RudiC For This Post:
# 3  
Old 12-15-2015
Code:
awk '{if (/memP/) { start=1}
if (start == 1) { if (/\{/) {count=count+1}
if (/\}/) {count=count-1}
if (count== 0) {  while ((getline line < "file2") > 0){print line}start=0;close("file2")}
}
print }' file1

# 4  
Old 12-15-2015
Hi Rudic

Thanks a lot ! Its working fine
could you help me out by giving a small explanation of this working code?

Thanks
Kshitij
# 5  
Old 12-15-2015
Hi RudiC in below code.
Code:
awk '
FNR == NR       {INS=INS DL $0
                 DL = ORS
                 next
                }
/^memP/         {C = 1
                }
C               {BRACNT += gsub (/{/, "&") - gsub (/}/, "&")
                }
C && !BRACNT    {print INS
                 C = 0
                }
1
' file2 file1

Kindly explain the highlighted part. Why you need to assign DL with ORS separately.?
and what substraction value would we get from
Code:
gsub (/{/, "&") - gsub (/}/, "&")

Thanks
# 6  
Old 12-15-2015
Code:
awk '
FNR == NR       {INS=INS DL $0          # variable to be inserted is being populated with the first file's lines seperated by
                 DL = ORS               # delimiter DL which is empty on first occurrence and set to ORS (= "\n") here 
                 next
                }
/^memP/         {C = 1                  # search pattern: start of region of interest; C = 1 means: collect braces
                }
C               {BRACNT += gsub (/{/, "&") - gsub (/}/, "&")    # substitute opening and closing braces by themselves, add/subtr. count
                }                                               # gsub accounts for multiple braces in one line; BRACNT = level of braces
C && !BRACNT    {print INS              # if we're in the region AND the brace count is zero: last closing braces encountered, print INS
                 C = 0                  # out of region
                }
1                                       # default action: print current line (i.e. closing brace from last action)
' file2 file1

This User Gave Thanks to RudiC For This Post:
# 7  
Old 12-16-2015
Rudic There is a sudden change of my requirement
Can you provide your valuable comments

My output file should look like this

Code:
modyle 1 {
test jsj
hhsjh 13e3 
jsjjs 

}

memP(dbg_trace) {
ajjs
jsjs
jsjs

Test(claer) {
jsjs
}
TestStep ( RunTime ) {
RUN jksj
Controller (ddd) {
akk sns
sjsj
}
Controller (ddssd) {
akk sns
sjsj
}
Controller (ddssssd) {
akk sns
sjsj
}
}

TestStep(RunExtra) {
      RunMode : RunTProg;
    Controller(ddd) {
        ComparesGOID : On;
        ComparesGo : On;
          }

      Controller(ddssd) {
        ComparesGOID : On;
        ComparesGo : On;
          }
Controller(ddssssd) {
        ComparesGOID : On;
        ComparesGo : On;
          }



}

}


modyless 2 {
test jsj
hhsjh 13e3 
jsjjs 

}


memP(dbg66_trace) {
ajjs
jsjs
jsjs

Test(claer) {
jsjs
}
TestStep ( RunTime ) {
RUN jksj
Controller (ddssd) {
akkss snss
sjssj
}
Controller (ddssssd) {
aksk snss
sssjsj
}
Controller (ssddssssd) {
askk ssns
ssjssj
}
}
TestStep(RunExtra) {
      RunMode : RunTProg;
      Controller(ddssd) {
        ComparesGOID : On;
        ComparesGo : On;
          }
 Controller(ddssssd) {
        ComparesGOID : On;
        ComparesGo : On;
          }

Controller(ssddssssd) {
        ComparesGOID : On;
        ComparesGo : On;
          }


}



}

modyless 3 {
test jsj
hhsjh 13e3 
jsjjs 

}

The problem here it should read the controller in each Teststep and edit the Teststep of file2 so that controller of that teststep should be copied in the output file

Basically , It should match the memP and match the last "}" of the memP wrapper and insert the File2 by modifying it but insert the controller wrappers of the current TestStep wrapper
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Problem in formatting output in sed / awk

I have a file like this : ! 1 ! 542255 ! 50,140.00 ! ! 2 ! 551717 ! 5,805.00 ! ! 3 ! 551763 ! 8,130.00 ! ! 4 ! 551779 ! 750.00 ! ! 5 ! 551810 ! 56,580.00 ! ! 6 ! 551816 ! 1,350.00 ! ! 7 ! 551876 ! 360.00 ! ! 8 ! 551898 ! ... (10 Replies)
Discussion started by: adam1969in
10 Replies

2. Shell Programming and Scripting

Problem with output awk and sed

I have file, i am extracting email address from file. but problem is that output is very ugly. I am using this command REMOVED "CSS OFFENDING CODE"... While original filename have no such character. Please suggest. (20 Replies)
Discussion started by: learnbash
20 Replies

3. Shell Programming and Scripting

Some problem with awk or sed in if conditions

#set -x path=`pwd` while read line do dd=`echo $line|awk -F"," '{print $1}'|awk -F"/" '{print $2}'` if then mm=`echo $line|awk -F"," '{print $1}'|awk -F"/" '{print $1}'` yy=`echo $line|awk -F"," '{print $1}'|awk -F"/" '{print $3}'` var=`echo $line|awk -F"," '{print $2}'` var1=`echo... (2 Replies)
Discussion started by: nikhil jain
2 Replies

4. Shell Programming and Scripting

awk/sed problem

I have a bunch of files I need to change a letter to another letter only in the first column of a space delimited file. The problem is that after the first column, there's comments and such that cause problems with awk. Here's what my data looks like: 16a28.0a27.0a26.0a25.0a24.67.255.200... (3 Replies)
Discussion started by: supradave
3 Replies

5. Shell Programming and Scripting

awk and sed problem

Hi, I am able to generate a csv file which has 3 columns. But the header is not coming out exactly i.e it doesnot have a comma in between it but the data is getting produced with the comma. The first line i.e the header is coming out as:- Counter Number Counterparty1 Counterparty2 ... (10 Replies)
Discussion started by: scripter12
10 Replies

6. Shell Programming and Scripting

Problem facing with sed and awk

Hi All, I have a got a problem .. I have t files as below: 1.txt contains ----- ----- ----- column 1, "cat", column 24, "dog", column 100, "rat", ----- ----- ----- 2.sh should contain ----- ----- ----- awk 'BEGIN { printf ("%1s","cat")}' (19 Replies)
Discussion started by: jisha
19 Replies

7. Shell Programming and Scripting

awk...sed problem

Hi guys, I am tryingto delete or ignore columns in a large dataset using 'sed'. For example, I have the following dataset: - 20060714,X.XX,1,043004,Q,T,24.0000,1,25.5000,4, 20060714,X.XX,1,081209,Q,T,24.0000,1,25.5000,5, As you can see, there are 10 columns here and the table that I am... (1 Reply)
Discussion started by: aarif
1 Replies

8. Shell Programming and Scripting

awk,sed or cut problem

Good afternoon, Sir's, I would like to seek your assistance regarding on this matter. $cat file1 111 aaaa bbb aass aaa files file1 temp temp1 pix 222 11 22 1 33 44 desired output: aaaa bbb aass files file1 temp1 222 11 22 1 33 44 thanks (7 Replies)
Discussion started by: invinzin21
7 Replies

9. Shell Programming and Scripting

awk & sed problem

Hello, I am new to shell scripting. I want to optimize my one of the script. I have one file and i want to remove selected zones for domains from that file.In this file i have almost 3500 zones for domains.Sample data for the file.... named.backup... (0 Replies)
Discussion started by: nrbhole
0 Replies

10. UNIX for Dummies Questions & Answers

sed/awk String problem

I would appreciate it if any one can guide me in using awk perhaps sed in extracting some values from a long string. here is an example. .......some lines here........ ........ aaaa bbbb cccc ddddd eeeee fffff gggg (time:hhhh)........ ......some lines here also.......... How can I extract... (2 Replies)
Discussion started by: odogbolu98
2 Replies
Login or Register to Ask a Question