Advanced grep and sed


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Advanced grep and sed
# 1  
Old 04-02-2009
Advanced grep and sed

I am wondering if there is a way via grep and sed to extract a string that is on the 2nd line below a known marker as in this example:

TextRel 203 0 0 "WELL:"
SetPosAbs 1287 -6676
TextRel 210 0 0 "AEP #2"

The marker is WELL:, but the string I need is "AEP #2". Can grep/sed handle this or is some sort of read command required?

Thanks,

Paul H.
Denver
# 2  
Old 04-02-2009
Code:
nawk -v qq='"' 'c&&!--c {print substr($0, index($0,qq))};$NF ~ "WELL:" {c=2;next}' myFile

# 3  
Old 04-02-2009
I used your code in the following string:

Set Well = `nawk -v qq='"' 'c&&!--c {print substr($0, index($0,qq))};$NF ~ "WELL:" {c=2;next}' $file`
echo $Well

I got the following message:

1: Event not found

Perhaps I should have added that I am using Cshell and that the 3 data lines shown in my original post are part of a larger file. The string "WELL:" however is the first instance of that string in the file. Hope that doesn't make it more difficult.

Thanks,
Paul H.
# 4  
Old 04-02-2009
Quote:
Originally Posted by phudgens
I used your code in the following string:

Set Well = `nawk -v qq='"' 'c&&!--c {print substr($0, index($0,qq))};$NF ~ "WELL:" {c=2;next}' $file`
echo $Well

I got the following message:

1: Event not found

Perhaps I should have added that I am using Cshell and that the 3 data lines shown in my original post are part of a larger file. The string "WELL:" however is the first instance of that string in the file. Hope that doesn't make it more difficult.

Thanks,
Paul H.
it should have worked regardless of the shell, but csh might be a different story.
Try executing from the command line and see what it gives you.
# 5  
Old 04-03-2009
Using the following command at the command line prompt:

nawk -v qq='"' 'c&&!--c {print substr($0, index($0,qq))};$NF ~ "WELL:" {c=2;next}' asciipds

I got the following message:

123: Event not found

I know that the file asciipds is present and that it does contain the string "WELL:" as shown in my original post. Perhaps it is the csh after all.

Thanks,
Paul H.
# 6  
Old 04-03-2009
Another way:

Code:
awk '/"WELL:"/{getline;getline;print $5, $6; exit}' asciipds

Regards
# 7  
Old 04-03-2009
That works both at the prompt and from within the script. Thanks Loads,

Paul H.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Text manipulation with sed - Advanced technic

Hello everybody, I have the following input file: START ANALYSIS 1 DATA LINE DATA LINE DATA LINE DATA LINE Libray /home/me/myLibrary Source library_name_AAAAA DATA LINE DATA LINE DATA LINE BEGIN SOURCE ANALYSIS Function A Function B Function C Function D (4 Replies)
Discussion started by: namnetes
4 Replies

2. Shell Programming and Scripting

sed and awk usage to grep a pattern 1 and with reference to this grep a pattern 2 and pattern 3

Hi , I have a file where i have modifed certain things compared to original file . The difference of the original file and modified file is as follows. # diff mir_lex.c.modified mir_lex.c.orig 3209c3209 < if(yy_current_buffer -> yy_is_our_buffer == 0) { --- >... (5 Replies)
Discussion started by: breezevinay
5 Replies

3. Shell Programming and Scripting

Help with sed/grep

Hi, I have a file with reoccurring patterns and I want extract the 3rd line after the match, then delete another pattern from that third line. For example the file is in the following format: Hello Name: Abc Number: 123 Hello Name: FQE Number: 543 This occurs more than 100... (4 Replies)
Discussion started by: wsn
4 Replies

4. Linux

sed and grep

I am stranded with a problem. Please solve. How will you remove blank lines from a file using sed and grep? ( blank line contains nothing or only white spaces). I run the below commands of sed and grep but grep isn't giving output as desired. Why? sed '/^*$/d' blank grep -v "^*$" blank... (3 Replies)
Discussion started by: ravisingh
3 Replies

5. Shell Programming and Scripting

Advanced sed/awk help

I have thousands of files in HTML that looks like this: .... .... .... <!-- table horaire --> <!-- table horaire --> <table border="0" cellspacing="0" cellpadding="0" class="tblHoraires" summary="Table des horaires de la ligne 12"> <tr> <th scope="row"... (13 Replies)
Discussion started by: charafantah
13 Replies

6. UNIX for Dummies Questions & Answers

Advanced grep'in... grep for data next to static element.

I have a directory I need to grep which consists of numbered sub directories. The sub directory names change daily. A file resides in this main directory that shows which sub directories are FULL backups or INCREMENTAL backups. My goal is to grep the directory for the word "full" and then... (2 Replies)
Discussion started by: SysAdm2
2 Replies

7. UNIX for Dummies Questions & Answers

| help | unix | grep (GNU grep) 2.5.1 | advanced regex syntax

Hello, I'm working on unix with grep (GNU grep) 2.5.1. I'm going through some of the newer regex syntax using Regular Expression Reference - Advanced Syntax a guide. ls -aLl /bin | grep "\(x\)" Which works, just highlights 'x' where ever, when ever. I'm trying to to get (?:) to work but... (4 Replies)
Discussion started by: MykC
4 Replies

8. UNIX for Dummies Questions & Answers

sed or grep?

hello everybody! I have a html file which is not properly formatted meaning that the whole content is in one line. I want to to cut out certain parts of that file. Those parts are between ' #" ' and ' " ' and always start with ' sec_ ' and after the ' sec_ ' any number of characters and ' _... (2 Replies)
Discussion started by: MastaFue
2 Replies

9. Shell Programming and Scripting

using sed to grep

I have a file that contains many instances of double dollar signs. I want to use sed to get the first occurrence. for example, given the following data. #Beginning of file AB 34 $$ AB $$ AB 98 $$ I only want to pull out: AB 34 $$ (1 Reply)
Discussion started by: wxornot
1 Replies

10. Shell Programming and Scripting

Grep/Sed help?

I'm a UNIX novice and am currently using a grep stmt to search for a pattern and send the matching lines to a new file. But what I really want to do is to append the line after the matching line to the matching line in the new file. Any ideas? 3/17/04 I am using the Bourne shell. And... (3 Replies)
Discussion started by: CKS
3 Replies
Login or Register to Ask a Question