AWK Script Issue insert newline for a regular expression match

Then try :

sed '/RXH/,/\}\;/d' input

Thanks Ctgsnb

Yeah I also tried like this and it worked perfectly fine with one file
but in another file I dont have

;

after the

} 

, in that case what to do ?

 
pin (RXEDF) {
input 
output 
}
 pin (RXEHRR) {
input kdk
output kdkdk 
}
pint (RXHERT) {
input jdjd
output kkd
timing () {
   rise_constraint (setup_template_2x2) {
         index_1 ("1.000, 195.000") ;
   }
fall_constraint (setup_template_2x2) {
         index_1 ("1.000, 195.000") ;
   }
     }
timing () {
       related_pin : "TXHDCLK" ;
       timing_type : hold_rising ;
rise_constraint (hold_template_2x2) {
index_1 ("1.000, 195.000") ;
}
fall_constraint (hold_template_2x2) {
index_1 ("1.000, 195.000") ;
}
}
}
pin (RXCER) {
input ioti
output nfnf
}

Any suggestion for this ? no blank lines as well as no

; 

Thanks
Jaita

sed 's/^pin/#&/' input | tr '#' '\n' | sed '/RXH/,/^$/d'

note that this code will add an empty line before every lines beginning with 'pin'
you can then delete them again with a grep -v

note that instead of # you should choose a charactere that appear nowhere else in your code (�,�, ....) up to you to choose a right one

Try this:

awk '/RXH/{f=1}; f{c+=/}/?1:/{/?-1:0}; !f; f && !c{f=0}' file
2 Likes

Thanks Ctgsnb ,

It worked perfectly fine !! Good Thinking to modulate the code for our requirement

Hi Franklin ,

Can you explain ur code , Not having good expertise on the advanced Awk ,
Will be helpful for me .

Thanks
Jaita

@jaita

Note that this code assume your file has its { and } correctly formated :

If somewhere in your code you have

This kind of things :

timing () {   rise_constraint (setup_template_2x2) {
         index_1 ("1.000, 195.000") ;
   }
}

instead of

timing () {
   rise_constraint (setup_template_2x2) {
         index_1 ("1.000, 195.000") ;
   }
}

This may lead to unexpected results (this is just an example of bad formating)

If several { are on the same line whereas the corresponding } are on differents lines, the counter may be mistaken.

To secure Franklin code, you should count the number of { and } per line and increment and decrement the counter accordingly for every hit

Thanks Ctgsnb ,

I tried below code and it also worked well

sed -e 's/\(^ pin[^ ]\)/#\1/g' ro.lib | tr '#' '\n' > temp1

Now I am having issue with range of the regular patterns to be matched

For example having a file like this

 
File1
 
##############
 
pin RXA {
input 
output 
}
 
pin RXB {
input 
output 
}
 
pin RXC {
input 
output 
}
 
pin RXD {
input 
output 
}
 
pin RXE {
input 
output 
}
 
pin RXF {
input 
output 
}

Now I need to match range of the regular expression like RXB to RXE and delete their corresponding blocks

 
Output 
 
pin RXA {
input 
output 
}
 
pin RXF {
input 
output 
}

I tried to use below script

sed -e '/RXB/,/RXE/d' 

but will not delete the whole content of the RXE Block till RXE Regular expression line it will match and input , output and } will remain as it is

One way is to Remove the black lines between blocks of RXB to RXE
like this

 
input
 
pin RXA {
input 
output 
}
 
pin RXB {
input 
output 
}
 pin RXC {
input 
output 
}
 pin RXD {
input 
output 
}
 pin RXE {
input 
output 
}
 
pin RXF {
input 
output 
}

and use the script

 
sed -e '/RXB/,/^$/d' input 

but how to do that in both the situations for matching with range of regular expression and deleting the blank lines for the range of regular expression matches

sed '/RX[BE]/,/^ *$/d' in
# sed '/RX[BE]/,/^ *$/d' in
pin RXA {
input
output
}

pin RXC {
input
output
}

pin RXD {
input
output
}

pin RXF {
input
output
}
#
1 Like

Hi Ctgsnb ,

Thanks for the reply

but I want to delete chunk of Blocks from a range of Regular expression RXB to RXE

Reframing the code again

 
pin RXA {
input 
output 
}

pin RXTRY {
input 
output 
}
pin TXCRY {
input 
output 
}
pin BXDRY {
input 
output 
}
pin MTRE {
input 
output 
}

pin RXF {
input 
output 
}


 

Matching the regular expression from range RXTRY to MTRE and delete the blocks

Output

 
pin RXA {
input 
output 
}

pin RXF {
input 
output 
}

Thanks
Jaita

sed '/RX[B-E]/,/^ *$/d' in
# sed '/RX[B-E]/,/^ *$/d' in
pin RXA {
input
output
}

pin RXF {
input
output
}

Hi

I tried this one to the below code but here it is deleting everything

Code

 
pin RX_R_01 {
input 
output 
}
 
pin RX_R_02 {
input 
output 
}
 
pin RX_R_03 {
input 
output 
}
 
pin RX_R_04 {
input 
output 
}
 
pin RX_R_05 {
input 
output 
}
 
pin RX_R_06 {
input 
output 
}

Script

 
sed '/RX_[R_02-R_05]/,/^ *$/d' input 

Wanted to delete the Block from RX_R_02 to RX_R_5

to give out the output

Output

 
 
pin RX_R_01 {
input 
output 
}

pin RX_R_06 {
input 
output 
}

but I am getting empty file , it is deleting everything

sed '/RX_R_0[2-5]/,/^ *$/d' input

Franklin ,
Your code is not clear to me

 
awk '/RXH/{f=1}; f{c+=/}/?1:/{/?-1:0}; !f; f && !c{f=0}' file

Could not understand how the flags will be set , unset and counter will be incremented and decremented

Suppose I am having a file like this

 
 
FILE 1
 pin ykdh {
 ndn ndn 
 hdjhd jd 
 } 
 pin rtrx {
 in nmd
 mdkd kd
 }
 pin rtry {
 im dnd
 dnd mdmd
}
 pin hdh {
 ndn ndn 
 hdjhd jd 
 }
and I am applying the below script 
awk '/rxt/{f=1}; f{c+=/}/?1:/{/?-1:0}; !f; f && !c{f=0}' file
it is giving me a right output 
output 
pin ykdh {
 ndn ndn 
 hdjhd jd 
 } 
pin hdh {
 ndn ndn 
 hdjhd jd 
 }

But if I am changing a small parameter in the FILE1 { to ]

 
FILE2 
pin ykdh [
 ndn ndn 
 hdjhd jd 
 ] 
 pin rtrx [
 in nmd
 mdkd kd
 ]
 pin rtry [
 im dnd
 dnd mdmd
]
 pin hdh [
 ndn ndn 
 hdjhd jd 
 ]

and applying below script

 
awk '/rxt/{f=1}; f{c+=/]/?1:/[/?-1:0}; !f; f && !c{f=0}' file

it is not giving the output and script getting failed
Could not understand the script as such ,
after matching with the regular expression rxt f is getting set and assigned to value = 1
after that could not able to understand the code

f{c+=/]/?1:/[/?-1:0}; !f; f && !c{f=0}

' setting and unsetting of the flags , incrementing and decrementing
the counters and prining the relevant lines
Please make me understand this code , how it works

Thanks
Jaita

# cat in3
pin RXA {
input
output
}

pin RXTRY {
input
output
}
pin TXCRY {
input
output
}
pin BXDRY {
input
output
}
pin MTRE {
input
output
}

pin RXF {
input
output
}
# awk '/RXA/{f=1}(f==1)&&/^$/{f=2}/RXF/{f=0;printf RS}(f!=2)' in3
pin RXA {
input
output
}

pin RXF {
input
output
}
# tr '{}' '[]' <in3 >in4
# awk '/RXA/{f=1}(f==1)&&/^$/{f=2}/RXF/{f=0;printf RS}(f!=2)' in4
pin RXA [
input
output
]

pin RXF [
input
output
]
#

If your file doesn't contains empty line so that the block are delimited by a line like } ... you can also use } instead of blank line :

# awk '/RXA/{f=1}(f==1)&&/^}$/{f=2}/RXF/{f=0;printf "}"RS}(f!=2)' in3
pin RXA {
input
output
}
pin RXF {
input
output
}
# awk '/RXA/{f=1}(f==1)&&/^]$/{f=2}/RXF/{f=0;printf "]"RS}(f!=2)' in4
pin RXA [
input
output
]
pin RXF [
input
output
]
#
1 Like

Thanks Ctgsnb for your reply !

Can u explain me the Frankiln's and ur code as well and
the query I raised for the Franklin's Code in my previous post

Thanks
Jaita

awk '/RXTRY|MTRE/{_++} /}/ && !(_-2) {_=x;next}!_' file
awk '/RXH/{f=1}; f{c+=/}/?1:/{/?-1:0}; !f; f && !c{f=0}' file

Explanation:

/RXH/{f=1} | 	If the line contains RXH, set the flag f
f{c+=/}/?1: | 	If f is set and the line contains a "}" increase counter c ..
/{/?-1:0} | 	..else if the line contains a "{" decrease c
!f | 	print the line if f is not set
f && !c{f=0} | 	If f is set and c == 0 unset the flag