Advanced grep and sed


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Advanced grep and sed
# 8  
Old 04-03-2009
Quote:
Originally Posted by phudgens
That works both at the prompt and from within the script. Thanks Loads,

Paul H.
Most likely you have embedded spaces in your 'WELL:' string.
here's the data file I've been testing with:
Code:
TextRel 203 0 0 "WELL:"
SetPosAbs 1287 -6676
TextRel 210 0 0 "AEP #2"

If you still care, just post your data file using the BB codes
# 9  
Old 04-03-2009
The actual file is immense, so I'll just add a few lines of particular interest to me. There is a problem reading other lines where the number of parameters varies as in the following:

TextRel 217 0 0 "COMPANY: "
SetPosAbs 1287 -6656
TextRel 224 0 0 "BATTELLE MEMORIAL INSTITUTE"

In this case I only get "BATTELLE MEMORIAL" returned. The string that I need to retrieve in each case will always be enclosed in double quotes at the end of the second line below the line containing the marker. Within those quotes, there may be any number of spaces. Sorry I didn't clarify that earlier. Is that still doable?

THanks,
Paul H.
# 10  
Old 04-03-2009
Code:
nawk -v qq='"' 'c&&!--c {print substr($0, index($0,qq))};/"[ ]*COMPANY:[ ]*"/ {c=2;next}' myFile

# 11  
Old 04-04-2009
Or:

Code:
awk -F "\"" '/COMPANY/{getline;getline;print $2}' file

Regards
# 12  
Old 04-04-2009
Another one

Quote:
sed -n '/WELL:/{n;n;p;}' filename | awk -F\" '{ print $2 }'
# 13  
Old 04-06-2009
Advanced sed and awk

Despite trying various modifications of the awk command I was not able to get it to work.
The sed command came very close though. It returned the desired string, but also a second undesired line. My input lines of interest are:

TextRel 217 0 0 "COMPANY: "
SetPosAbs 1287 -6656
TextRel 224 0 0 "BATTELLE MEMORIAL INSTITUTE"
SetPosAbs 1137 -6680
TextRel 218 0 0 "WELL:"
SetPosAbs 1287 -6626
TextRel 225 0 0 "AEPAEP"
SetPosAbs 1141 -6596
TextRel 219 0 0 "FIELD:"
SetPosAbs 1287 -6596
TextRel 226 0 0 "MOUNTAINEER"
SetPosAbs 1141 -6566
TextRel 220 0 0 "County: "
SetPosAbs 1287 -6566
TextRel 227 0 0 "MASON"
SetPosAbs 1141 -6536
TextRel 221 0 0 "State:"
SetPosAbs 1287 -6536
TextRel 228 0 0 "WEST VIRGINIA"
SetPosAbs 1141 -6506
TextRel 222 0 0 "COUNTRY:"
SetPosAbs 1287 -6506
TextRel 229 0 0 "USA"

I need to retrieve the string contained in quotes on the second line below the marker line. So for the above, I need to retrieve "AEPAEP" for WELL:, "MOUNTAINEER" for FIELD:, and "BATTELLE MEMORIAL INSTITUTE" for COMPANY:, etc, (minus the double quotes.)
The sed command returned the following:
AEPAEP
FIELD:
Thanks for everyone's help on this. While I can decipher most CShell code, sed and awk are from a different planetary system.
Thanks,
Paul H.
# 14  
Old 04-06-2009
Despite trying various modifications of the awk command I was not able to get it to work.

The sed command came very close though. It returned the desired string, but also a second undesired line. My input lines of interest are:

TextRel 217 0 0 "COMPANY: "
SetPosAbs 1287 -6656
TextRel 224 0 0 "BATTELLE MEMORIAL INSTITUTE"
SetPosAbs 1137 -6680
TextRel 218 0 0 "WELL:"
SetPosAbs 1287 -6626
TextRel 225 0 0 "AEPAEP"
SetPosAbs 1141 -6596
TextRel 219 0 0 "FIELD:"
SetPosAbs 1287 -6596
TextRel 226 0 0 "MOUNTAINEER"
SetPosAbs 1141 -6566
TextRel 220 0 0 "County: "
SetPosAbs 1287 -6566
TextRel 227 0 0 "MASON"
SetPosAbs 1141 -6536
TextRel 221 0 0 "State:"
SetPosAbs 1287 -6536
TextRel 228 0 0 "WEST VIRGINIA"
SetPosAbs 1141 -6506
TextRel 222 0 0 "COUNTRY:"
SetPosAbs 1287 -6506
TextRel 229 0 0 "USA"


I need to retrieve the string contained in quotes on the second line below the marker line. So for the above, I need to retrieve "AEPAEP" for WELL:, "MOUNTAINEER" for FIELD:, and "BATTELLE MEMORIAL INSTITUTE" for COMPANY:, etc, (minus the double quotes.)

The sed command returned the following:

AEPAEP
FIELD:

Thanks for everyone's help on this. While I can decipher most CShell code, sed and awk are from a different planetary system.

Thanks,
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