Advanced grep and sed


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Advanced grep and sed
# 15  
Old 04-06-2009
I have just discovered that by piping the results of the sed command to head -1 returns the desired string, (and only the desired string). ie:

sed -n '/WELL:/{n;n;p;}' asciipds | awk -F\" '{ print $2 }' | head -1

Thanks,
Paul H.
# 16  
Old 04-06-2009
Have you tried the awk solution?

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

This is the output I get:

Code:
$ cat file
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"
$
$ awk -F "\"" '/COMPANY/{getline;getline;print $2}' file
BATTELLE MEMORIAL INSTITUTE
$ awk -F "\"" '/WELL/{getline;getline;print $2}' file
AEPAEP
~/scripts$ awk -F "\"" '/FIELD/{getline;getline;print $2}' file
MOUNTAINEER
$ awk -F "\"" '/COMPANY/{getline;getline;print $2}' file
BATTELLE MEMORIAL INSTITUTE
$

Have I missed something?

Regards
# 17  
Old 04-06-2009
When I run the following command from the command line prompt:

awk -F "\"" '/COMPANY/{getline;getline;print $2}' asciipds

I get:
Unmatched "

When I run:

awk -F \" '/COMPANY/{getline;getline;print $2}' asciipds

I get:

awk: can't open /COMPANY/{getline;getline;print $2}


Which shell are you running? I'm running in the csh which is what may be making the difference.

Thanks,
Paul H.
# 18  
Old 04-06-2009
Try it with nawk or /usr/xpg4/bin/awk on Solaris.

Regards
# 19  
Old 04-06-2009
The following command:

nawk -F \" '/COMPANY/{getline;getline;print $2}' asciipds | head -1

returned the following string:

BATTELLE MEMORIAL INSTITUTE

Without the "head -1" it was returning a second unwanted string as well.

Is nawk specifically for the c shell?

Thanks,
Paul H.
# 20  
Old 04-07-2009
Quote:
Originally Posted by phudgens
The following command:

nawk -F \" '/COMPANY/{getline;getline;print $2}' asciipds | head -1

returned the following string:

BATTELLE MEMORIAL INSTITUTE

Without the "head -1" it was returning a second unwanted string as well.

Is nawk specifically for the c shell?

Thanks,
Paul H.
I don't think it should not matters but the c shell is not recommended for scripting:

http://www.grymoire.com/Unix/CshTop10.txt

Regards
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