sed , awk script for printing matched line before regular expression


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sed , awk script for printing matched line before regular expression
# 1  
Old 05-07-2014
Blade 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
###############################################

Code:
 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

Code:
Ring :  module kkkk 
Ring : module lll

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

I am using this one liner
Code:
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

Last edited by Don Cragun; 05-07-2014 at 05:14 PM.. Reason: Add CODE tags.
# 2  
Old 05-07-2014
Code:
awk '/^module/ {m=$0} /^Ring/ {print $0 " : " m}' file

Code:
Ring  : module KKK
Ring  : module llll


You can modify the pattern accordingly.
# 3  
Old 05-07-2014
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:
Code:
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.
This User Gave Thanks to Don Cragun For This Post:
# 4  
Old 05-07-2014
An alternative using AWK's multiline-record capability:
Code:
awk '/Ring/ {print $1}' RS= FS='\n' file

Regards,
Alister
# 5  
Old 05-08-2014
Wrench Shell scripting sed Variable substituion problem

Hi All ,

I am setting variables as shown below

PHP Code:
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

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


HTML Code:
#!/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 ?
# 6  
Old 05-08-2014
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.

Last edited by clx; 05-09-2014 at 08:49 AM.. Reason: typos
These 2 Users Gave Thanks to clx For This Post:
# 7  
Old 05-08-2014
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):
Code:
c=1
TMP=MODULE$c
echo ${!TMP}

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help with awk script (syntax error in regular expression)

I've found this script which seems very promising to solve my issue: To search and replace many different database passwords in many different (.php, .pl, .cgi, etc.) files across my filesystem. The passwords may or may not be contained within quotes, single quotes, etc. #!/bin/bash... (4 Replies)
Discussion started by: spacegoose
4 Replies

2. UNIX for Advanced & Expert Users

Add a line after last matched expression

I thought this would be easy to Google, but I am having trouble getting a clean result that I can understand. I simply want to insert the the line: My Network 192.168.1.1 After the last line that begins with ACL localnet (15 Replies)
Discussion started by: glev2005
15 Replies

3. UNIX for Advanced & Expert Users

sed: -e expression #1, char 0: no previous regular expression

Hello All, I'm trying to extract the lines between two consecutive elements of an array from a file. My array looks like: problem_arr=(PRS111 PRS213 PRS234) j=0 while } ] do k=`expr $j + 1` sed -n "/${problem_arr}/,/${problem_arr}/p" problemid.txt ---some operation goes... (11 Replies)
Discussion started by: InduInduIndu
11 Replies

4. Programming

Perl: How to read from a file, do regular expression and then replace the found regular expression

Hi all, How am I read a file, find the match regular expression and overwrite to the same files. open DESTINATION_FILE, "<tmptravl.dat" or die "tmptravl.dat"; open NEW_DESTINATION_FILE, ">new_tmptravl.dat" or die "new_tmptravl.dat"; while (<DESTINATION_FILE>) { # print... (1 Reply)
Discussion started by: jessy83
1 Replies

5. Shell Programming and Scripting

How can I get the matched text when using regular expression.

Hello: (exp) : match "exp",the matched text is stored in auto named arrays. How can I get the matched text ? What is the name of the auto named arrays on linux shell ? (4 Replies)
Discussion started by: 915086731
4 Replies

6. Shell Programming and Scripting

matched characters - regular expression

Hi, I read the book of <<unix shell programming>>. The regular expression ^\(.\)\1 matches the first character on the line and stores it in register 1. Then the expression matches whatever is stored in the register 1, as specified by the \1. The net effect of this regular expression is to match... (2 Replies)
Discussion started by: jianma
2 Replies

7. Shell Programming and Scripting

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... (38 Replies)
Discussion started by: jaita
38 Replies

8. Shell Programming and Scripting

AWK script issue for the part regular expression

Hi I am having a file as shown below FILE 1 TXDD00, TXDD01, TXDD02, TXDD03, TXDD04, TXDD05, TXDD06, TXDD07, TXDD08, TXDD09, TXDD10, TXDD11, TXDD12, TXDD13, TXDD14, TXDD15, TXDD16, TXDD17, TXDD18, TXDD19, TXDDCLK, TXDJTAGAMPL0, TXDJTAGAMPL1,... (3 Replies)
Discussion started by: jaita
3 Replies

9. Shell Programming and Scripting

sed not printing lines before a regular expression.

Hey, I found a way to print the lines which is just before a regular expression, not including the expression. sed -n '/regexp/{n;p;}' myfile Now I'm looking for a way to print all lines, exept the regular expression and also the line before the same regular expression. Use code tags. (1 Reply)
Discussion started by: Livio
1 Replies

10. Shell Programming and Scripting

awk script to move a line after the matched pattern line

I have the following text format in a file which lists the question first and then 5 choices after that the explanantion and finally the answer. 1.The amount of time it takes for most of a worker’s occupational knowledge and skills to become obsolete has been declining because of the... (2 Replies)
Discussion started by: nanchil_guy
2 Replies
Login or Register to Ask a Question