sed , awk script for printing matched line before regular expression

hi All ,

I am having a large file with lots of modules as shown below
###############################################

 module KKK 
kksd 
kskks  
jsn;lsm
jsnlsn;
Ring 
jjsjsj
kskmsm
jjs
endmodule

 module llll 
1kksd11 
k232skks  
j33sn;l55sm
js333nl33sn;
Ring 
jj33sj4sj
k33sk33msm
j33js
endmodule

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

I need to match Ring regular expression and script should print corresponding module related to that "Ring" expression ...Ring lies in different modules ...

Output file

Ring :  module kkkk 
Ring : module lll

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

I am using this one liner

awk '/Ring/ {print  x} {x=$0}' file

but it is priting one line above the Ring expression ...
I need to find out the corresponding module of the Ring where it lies and create a output file

Please help me out

awk '/^module/ {m=$0} /^Ring/ {print $0 " : " m}' file
Ring  : module KKK
Ring  : module llll

You can modify the pattern accordingly.

Since you didn't use CODE tags when you posted your question, clx couldn't tell that the lines containing module do not have that text at the beginning of the line.

Now that I've added CODE tags for you, and we can better see your input file contents, maybe the following slight modification of clx's script will work better for you:

awk '/^ *module/ {m=$1 " " $2} /^Ring/ {print $1 " : " m}' file

although this will provide consistent column alignment instead of the seemingly random number of spaces after the colon you included in your sample output.

1 Like

An alternative using AWK's multiline-record capability:

awk '/Ring/ {print $1}' RS= FS='\n' file

Regards,
Alister

Hi All ,

I am setting variables as shown below

setenv MODULE1 modem_1_3 
setenv MODULE2 modem5_1_2 
setenv MODULE3 modem5_1 
setenv MODULE4 modem5_2_top_a

setenv COUNT 4

I am having another bash script as shown below

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

#!/bin/bash 

for (( c=1; c<=${COUNT}; c++ ))

do
   
sed -n '/module '${MODULE}'${c} /,/endmodule/p' prescan.v > temp${c}.v

sed -i '0,/o_func_clk /s/o_func_clk/o_func_clk, DFT_ClkEnScanIn1 ,scan_clock_en , DFT_ClkgenScanEnable  /' temp${c}.v
#sed -i '0,/i_UDR_DFT_free_running;/i_UDR_DFT_free_running;/i_UDR_DFT_free_running , DFT_ClkEnScanIn1 , DFT_ClkgenScanEnable ;/' temp${c}.v

sed -i 's/i_UDR_DFT_free_running;/i_UDR_DFT_free_running , DFT_ClkEnScanIn1 , DFT_ClkgenScanEnable ;/' temp${c}.v

sed -i '/\[0\:0\] i_func_clk_en/a output  scan_clock_en ;' temp${c}.v
sed -i '/scan_clock_en_reg/,+1d' temp${c}.v

sed -i '/endmodule/i M8E35B_SDFF2X1  scan_clock_en_reg (.q ( scan_clock_en ) , .d (pre_scan_clock_en ) , .te ( DFT_ClkgenScanEnable ) , .ti ( DFT_ClkEnScanIn1 ) , .phi ( i_scan_clk ));' temp${c}.v

done

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

This script should go in loop and create different files temp1.v temp2.v temp3.v and temp4.v

My problem is in the first line

sed -n '/module '${MODULE}'${c} /,/endmodule/p' prescan.v > temp${c}.v
variable substition is not happening and temp is not getting created

I tried with double quotes but then also its not working ...

Incrementing the $c in the loop and $Module1 or $Module2 like that should be read as defined in environment variable and substitution should happen but I am getting empty temp files

Please let me know how to tackle this issue ?

How this problem is related to your initial problem?
Moreover, if its different, then create a new thread.
Did your initial problem solve with the solutions provided in various posts? We deserve an acknowledgment at least whether it has worked or not.
Still if you are not clear with anything, rephrase your problem with the real example, sample input and output and explain everything you want to achieve and you have tried so far.

2 Likes

Your quoting is not correct, that ${c} after module will not be evaluated. And, if I infer sort of correctly from what you say, you're trying "dynamic" variables. That does not work that way. As you seem to be using bash, try indirect expansion (see man bash):

c=1
TMP=MODULE$c
echo ${!TMP}