AWK Script Issue insert newline for a regular expression match

Hi ,

I am having an issue with the Awk script to insert newline for a regular expression match

Having a file like this

FILE1
####################
RXOER , RXERA , RXERC , RXERD
.RXEA(RXBSN), RXERD , REXCD
input RXEGT
buffer RXETRY 
#######################

Want to match the RXE Regular expression in the FILE1 and insert a new line after the expression but with some conditions as shown in output

Output should be like this 
######################
RXOER,
RXERA , 
RXERC , 
RXERD
.RXEA(RXBSN)
RXERD ,
REXCD
input RXEGT
buffer RXETRY 
###########################

I am trying this script like this
awk '{gsub(/RXE/,"RXE\n"}' FILE1

but not getting the output as shown in the output file
Tried to Split it also but could not able to work with Split command of Awk

Please help me out

Thanks
Jaita

Something like this?

perl -p0e 's/,/,\n/g' file

Can you please help me out with an AWK or Sed script for this problem

Thanks
Jaita

awk '{gsub(",",",\n")}1' file
1 Like

Hi Bartus .

Thanks for the Reply
But I want to match the Regular expression RXE and on the basis of the match it should insert the newline but selective matching should be there on the basis on conditions

Matching the RXE and inserting newline to after matching with the pins having RXE

Thanks
Jaita

But in your sample input you matched "RXO" and "REX" and inserted newline after them as well.

Thats True

its a sample file from a very big File
I need to match only RXE and wherever it matches RXE , it should insert newline

For example
Reframing the code once again for more clarification

Code is like this

 
ROXER , RTOEX , RXERT , RXERY , RXERU
.RAEY(TXBSIM) , .RREU(TXDSIN),  .RXEY(TSTY) , .RXEI(TSTR)
input RXETY
output RXEUI

Output should be like this

 
ROXER , RTOEX , 
RXERT , 
RXERY , 
RXERU
.RAEY(TXBSIM) , .RREU(TXDSIN),  
.RXEY(TSTY) ,
.RXEI(TSTR)
 
input RXETY
 
output RXEUI

Thanks
Jaita

cat input | sed 's/,  *\([^ ]*RXE\)/,|\1/g' | tr '|' '\n' >ouput

Instead of | ,you could use any character that do not already appear in the input you want to parse.

# cat input
ROXER , RTOEX , RXERT , RXERY , RXERU
.RAEY(TXBSIM) , .RREU(TXDSIN),  .RXEY(TSTY) , .RXEI(TSTR)
input RXETY
output RXEUI
# cat input | sed 's/,  *\([^ ]*RXE\)/,|\1/g' | tr '|' '\n'
ROXER , RTOEX ,
RXERT ,
RXERY ,
RXERU
.RAEY(TXBSIM) , .RREU(TXDSIN),
.RXEY(TSTY) ,
.RXEI(TSTR)
input RXETY
output RXEUI
#

Thanks ctgsnb ,

Actually this code worked , I am little new to Sed
Can you explain how this code works , bit of explanation of this Code

Thanks and Regards
Jaita

You get a Useless Use of Cat Award :slight_smile: Try

sed 's/,  *\([^ ]*RXE\)/,|\1/g' input | tr '|' '\n' >ouput

If GNU awk avaliable:

#awk '!/put/{a=gensub(/([\.]*)RXE/,"\n\\1RXE","G");print a;next}{print;}' input 
ROXER , RTOEX , 
RXERT , 
RXERY , 
RXERU
.RAEY(TXBSIM) , .RREU(TXDSIN),  
.RXEY(TSTY) , 
.RXEI(TSTR)
input RXETY
output RXEUI

Thanks ctgsnb ,

Now I am trying the code in a reverse manner but it is not working

 
FILE1
##################
 
ROXER , RTOEX , RXERT , RXERY , RXERU
.RAEY(TXBSIM) , .RREU(TXDSIN),  .RXEY(TSTY) , .RXEI(TSTR)
RXHMAN, RXADCLK , RXBDCLK , RXCDCLK, RXDDCLK 
.RXTTY(TRYN), .RXHMA(TRNAM)
RXFT, RXHMAN, RXDDN , RXDNR
input RXETY
output RXEUI
#############################
 

output

 
###############
 
 
ROXER , RTOEX , RXERT , RXERY , RXERU
.RAEY(TXBSIM) , .RREU(TXDSIN),  .RXEY(TSTY) , .RXEI(TSTR)
RXHMAN, 
RXADCLK , RXBDCLK , RXCDCLK, RXDDCLK 
.RXTTY(TRYN), .RXHMA(TRNAM)
RXFT, RXHMAN, 
RXDDN , RXDNR
input RXETY
output RXEUI

#############################

The matching pattern is RXH whenever the script will see RXH** and the , it should insert a newline

 
sed -e s/\(RXH[^ ]*\), */,%/g' FILE1 | tr '%' '\n' > output 

Pattern is like this RXH and the not space zero or more after RXH in the delimit pattern,
, zero or more . inserting a newline , but this code is not giving exprected result

Please let me know what is wrong in this

Thanks
Jaita

Hi

You forgot the leading simple quote ' as well as the \1 (in red below)
Try this :

sed -e 's/\(RXH[^ ]*\), */\1,%/' FILE1 | tr '%' '\n'

Thanks Ctgsnb,

Hi ! In this script

sed -e 's/\(RXH[^ ]*\), */\1,%/' FILE1 | tr '%' '\n'

instead of special character % , can we use any expression like "REPLACE" or any other expression to tie it up with tr for replacement of the \1 pattern

Thanks
Jaita

No,

tr handle character per character translation, but you can use or or # or whatever uncommon character that is not supposed to appear in the file you want to parse.

Or give a try to this (dont forget any simple or double quote)

sed -e 's/\(RXH[^ ]*\), */\1,'"\n"'/' FILE1
1 Like

Thanks Ctgsnb for your valuable inputs
Still getting familiar with the sed .

having new coding issue ->

 
FILE1 
#########################
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
};
 
######################
 
OUTPUT 
####################
pin (RXEDF) {
input 
output 
};
 
pin (RXCER) {
input ioti
output nfnf
};
 
#######################

Need to match regular expression RXH and delete the content

I am using the following sed script

 
sed '/RXH[^ ]*/,/}/d' 

but it is deleting only the limited content as it is matching first } bracket only

Output what I am getting is

 
Wrong Output 
 
#####################3
 
pin (RXEDF) {
input 
output 
};
 
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
};
}
}

Please share your valuable inputs with me

Your example is ambiguous with your requirement : the record containing RXEHRR doesn't appear in your output file whereas it doesn't match RXH ...
?
By the way i am a bit confused : is your file correctly formatted (with those } and }; )
Are you sure no ; are missing ?

Hi Ctgsnb

This could be correct file

FILE1

pin (RXEDF) {
input 
output 
};
 
pin (RXHRR) {
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
};
 

Output

pin (RXEDF) {
input 
output 
};

pin (RXCER) {
input ioti
output nfnf
};
 

with

sed '/RXH[^ ]*/,/}/d' 

but it will take care of the first expression not for the second match because it has to delete till all the 3 } } } brackets

Try this (from line containing RXH to the first empty line found) :

sed '/RXH/,/^$/d' input

Note : watch out : if "RHX" appears inside a non "RHX" block, the proposed code would not be suitable example :

if a block looks like

pin (REHRR) {
input kdk
<whatever> RXH <whatever>
output kdkdk 
};

the corresponding unexpected output will be produced:

pin (REHRR) {
 input kdk

Hi Ctgsnb,

Thanks for the Code ^$ matches with the blank lines but in my file
between the pin configurations blank lines are not there
This code will delete everything after the first match
Can u suggest something else to match up with the 3rd } after matching the pattern and deleting the configuration

Thanks
Jaita