Need Help with a sed command involving Regex


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need Help with a sed command involving Regex
# 1  
Old 11-28-2013
Need Help with a sed command involving Regex

Hi,
Iam a newbie to SED. I'm faced with a problem as described.

Given the file with text
Code:
1  rwerwerwe rere                         
2  fdfefefe fsdfds                        
3  rerere ffff trtrt                      
4  aaaa 1234 asadsdsd                     
5  hfjfjfjsjfsf shfshfsjhshs              
6  dssssdsdsdd                            
7  dsgdshgsdgdsdgssgdsgdsdhsgdshgshdshdg  
8  kjjkjkj         0---+---0              
9  gfhdfgdfhfgdhhfgdhfgd                  
10 sgdhsgdhsdgsdhgsdgsdhhdg               
11 1234hjhhh ghgsggdgdg                   
12 dddhhdjdhdh                            
13 dagdhdgahadgadg  0---0---+

I want to output two lines , line no 8 and line no 4 .The condition being the first occurrence of 0- or -0 and then the looking back and selecting the line with 1234.
So the two lines out put would be
Code:
aaaa 1234 asadsdsd  
kjjkjkj         0---+---0

Rgds
SShinde

Last edited by Scott; 11-28-2013 at 12:55 PM.. Reason: Please use code tags for code and data
# 2  
Old 11-28-2013
You may try awk approach

Code:
$ awk '/1234/{p=$0;X=NR}/0\-|\-0/ && NR-X == 4{print p RS $0}' file
aaaa 1234 asadsdsd
kjjkjkj 0---+---0

OR
Code:
$ tac file| awk '/0\-|\-0/{p=$0;X=NR}/1234/ && NR - X == 4{print p RS $0}' | tac
aaaa 1234 asadsdsd
kjjkjkj 0---+---0

# 3  
Old 11-28-2013
Hi,
@SShinde: Why lines 11 and 13 don't work ?

Regards.
# 4  
Old 11-28-2013
Hi Akshay,
Works like a charm on the text snippet that I had attached earlier.
Somehow it is not working on the text snippet that I'm working on :-

Snippet starts :-
Code:
-------------------------------------------------
               Load step no.                          =     1                       
               Load increment scaled to                                             
               maximum load level                                                   
                                                                                    
               Load increment                         =    1.000                    
               New load level                         =    1.000                    
               Current stiffness parameter            =    1.000                    
               Solution accuracy parameter            =  3.848*E-00009              
               Determinant of tangential matrix       =  4.427*E189782              
               Number of Negative Pivot Element       =        0                    
               Total energy absorbtion                =  8.734*E 00006              
                                                                                    
                                                                                    
                                                                                    
  --------    I N T E R A C T I O N   F U N C T I O N   V A L U E S  Fb(Fy) ---     
                                                                                    
     ELEM  ES     Node1         Midspan         Node2                               
                                                                                    
     1446   0 -0.44(-0.29)  -0.73(-0.65)  -1.00(-1.00)                              
     1447   0 -0.51(-0.47)  -0.55(-0.52)  -0.60(-0.57)                              
     1448   0 -0.42(-0.38)  -0.46(-0.42)  -0.51(-0.47)                              
     1449   0 -0.40(-0.25)  -0.71(-0.63)  -0.98(-0.98)                              
     1451   0 -0.51(-0.48)  -0.55(-0.52)  -0.59(-0.56)                              
     1453                                               Yield at end 1              
     1453                                               Yield at end 2              
     1453   3 -0.07( 0.21)  -0.82(-0.74)  -0.21( 0.04)         O---+---O            
     1454   0 -0.12(-0.05)  -0.24(-0.18)  -0.38(-0.33)                              
     1455                                               Yield at end 2

-----------Snippet Ends------------

I'm working to isolate two lines namely :-
Code:
New load level                         =    1.000  
1453   3 -0.07( 0.21)  -0.82(-0.74)  -0.21( 0.04)         O---+---

I did try to change the awk command but I'm getting a null string as output.
Rgds
SShinde

---------- Post updated at 01:42 PM ---------- Previous update was at 12:34 PM ----------

Hi Akshay,
Discovered a shortcoming in the awk command .
The awk command is
Code:
awk '/1234/{p=$0;X=NR}/0\-|\-0/ && NR-X == 4{print p RS $0}' file

In the awk command, the absolute value of 4 is creating the problem.
After finding 0- or -0 , I look back till I find 1234, which may occur more than 4 lines behind the first occurrence of 0- or -0.

Can this be rectified !

Rgds
SShinde

Moderator's Comments:
Mod Comment Please use code tags for code and data

Last edited by Scrutinizer; 11-28-2013 at 03:18 PM.. Reason: code tags
# 5  
Old 11-28-2013
Hi,
I don't all understand, but try:
Code:
sed -n '/0-/{x;p;x;p;q;};/-0/{x;p;x;p;q;};/1234/x' file

Regards.
# 6  
Old 11-28-2013
Hi disedorgue,
Thanks, it works.
Would it be possible for you to explain this sed command , as I want to learn SED.
Rgds
SShinde
# 7  
Old 11-28-2013
Moving targets make solutions difficult. Plus, is it "O---+---O" or "0---+---0" that you are looking for? Anyhow, try
Code:
tac file | awk '/O[+-]+O/ {L=1; print} /New load/ && L {L=0; print} ' | tac
               New load level                         =    1.000                    
     1453   3 -0.07( 0.21)  -0.82(-0.74)  -0.21( 0.04)         O---+---O

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Regex with sed

hi i would like to say "DATABASENAME=" to "TABLESNAME=" remove "," and press enter myconfig file thanks (1 Reply)
Discussion started by: mnnn
1 Replies

2. Shell Programming and Scripting

Sendmail K command regex: adding exclusion/negative lookahead to regex -a@MATCH

I'm trying to get some exclusions into our sendmail regular expression for the K command. The following configuration & regex works: LOCAL_CONFIG # Kcheckaddress regex -a@MATCH +<@+?\.++?\.(us|info|to|br|bid|cn|ru) LOCAL_RULESETS SLocal_check_mail # check address against various regex... (0 Replies)
Discussion started by: RobbieTheK
0 Replies

3. Shell Programming and Scripting

Help with sed substitution / regex

Hi all, please can anyone show me how to use sed and regular expressions to achieve the following. If a line contains a capital A followed by exactly 5 or 6 characters followed by an angled bracket then insert an asterix before the angled bracket. So: XCONFIGA12345<X Becomes: ... (5 Replies)
Discussion started by: Jedimark
5 Replies

4. UNIX for Advanced & Expert Users

Sed regex problem

Hi, I tried to extract the time from `date` with sed. (I know it works with `date +%H:%M:%S` as well) I got three solutions of which just one worked. I thought "+" should repeat the previous expression 1 or more times and {n} should repeat the previous expression n times. $ date Thu... (9 Replies)
Discussion started by: thiuda
9 Replies

5. Shell Programming and Scripting

Converting perl regex to sed regex

I am having trouble parsing rpm filenames in a shell script.. I found a snippet of perl code that will perform the task but I really don't have time to rewrite the entire script in perl. I cannot for the life of me convert this code into something sed-friendly: if ($rpm =~ /(*)-(*)-(*)\.(.*)/)... (1 Reply)
Discussion started by: suntzu
1 Replies

6. Shell Programming and Scripting

Using SED command in a shell script: Unterminated address regex

Hi All, I am trying to use a sed command in a shell script in order to delete some lines in a file and I got the following error message. I don't understand why it is not working 'cause I have tried with simple quotes, then with double-quotes, and it is not working. sed: -e expression #1,... (7 Replies)
Discussion started by: Alpha3363
7 Replies

7. Shell Programming and Scripting

SED command help: Can we pass predefined variables in place of regex

Hi All, I have a doubt. Can we assign a regular expression for pattern searching to a variable in a script and then use that variable in place of a regular expression in sed command.I tried but got some syntax error!!Is it not possible.Because my requirement is that i have a generic script to get... (8 Replies)
Discussion started by: usha rao
8 Replies

8. Shell Programming and Scripting

sed - using regex and | need help

From my understanding when using regex1|regex2 the matching process tries each alternative in turn, from left to right, and the first one that succeeds is used. When im trying to extract the name from those examples: A) name.can.be.different.20.03.2009.boom B)... (2 Replies)
Discussion started by: TehOne
2 Replies

9. Shell Programming and Scripting

Sed and regex help needed

Hi all, I'm writing a script that replaces a value in a file. The file is formatted as follows: So, for this example, I'd like to replace the value for param_two. The value for param_two can be a one, or two-digit number. It replaces the value in file.cfg, and directs the... (9 Replies)
Discussion started by: marknu1
9 Replies

10. UNIX for Dummies Questions & Answers

sed regex

I would like to do this: replace the word "prod" with the word "special" but it may occur through the file naturally without a command, I only want it to happen when it has a specific command in front of it. The command will always look like this &lt;IMG,###,###,##,&gt;prod/directory/IMG/file ... (4 Replies)
Discussion started by: Shakey21
4 Replies
Login or Register to Ask a Question