Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jun 17th, 2011, 7:17 AM   #1
jjgalan
Newbie
 
Join Date: Jun 2011
Posts: 6
Rep Power: 0 jjgalan is on a distinguished road
awk print interval defined by two regexp

Hi all,

I have a file like this:

*FIELD TI*
XXXXXXXXXXXXXX
*FIELD CS*
YYYYYYYYYYY
*FIELD CD*
ZZZZZZZZZZZZZZZZZ
*FIELD TI*
11111111111111111111111
*FIELD TI*
2222222222222222222222222
*FIELD CA*
33333333333333333333333333
*FIELD CS*
4444444444444444444444444
*FIELD PO*
ABAVDIHEK

How can I get all the register among the regexp *FIELD CS* and the next "*"?. I mean:
YYYYYYYYYYY
4444444444444444444444444

I have tried:

awk '/\*FIELD CS\*/ , /\*/' infile > outfile

but it only prints registers with the regexp *FIELD CS*. Can someone help me?

Thanks in advance
jjgalan is offline   Reply With Quote
Old Jun 17th, 2011, 9:19 AM   #2
Patsie
Professional Programmer
 
Patsie's Avatar
 
Join Date: Jun 2010
Posts: 450
Rep Power: 5 Patsie is on a distinguished road
Re: awk print interval defined by two regexp

I would say:
awk '/\*FIELD CS\*/ { getline; print; }'
This would work with your example data.
Patsie is offline   Reply With Quote
Old Jun 18th, 2011, 5:45 AM   #3
jjgalan
Newbie
 
Join Date: Jun 2011
Posts: 6
Rep Power: 0 jjgalan is on a distinguished road
Re: awk print interval defined by two regexp

Hi Patsie,

Yes, it works with one line, but what about with this file?

*FIELD TI*
XXXXXXXXXXXXXX
*FIELD CS*
YYYYYYYYYYY
abababababababa
*FIELD CD*
ZZZZZZZZZZZZZZZZZ
*FIELD TI*
11111111111111111111111
*FIELD TI*
2222222222222222222222222
*FIELD CA*
33333333333333333333333333
*FIELD CS*
4444444444444444444444444
ccvcvcvcvcvcvcvcvcvcvcvcvcvcvcvc
ggsgsgsgsgsgsgsgsgsgsgsgsgsgs
*FIELD PO*
ABAVDIHEK

I would like to get:

*FIELD CS*
YYYYYYYYYYY
abababababababa
*FIELD CS*
4444444444444444444444444
ccvcvcvcvcvcvcvcvcvcvcvcvcvcvcvc
ggsgsgsgsgsgsgsgsgsgsgsgsgsgs

By the way, do you know any book, review or so where the command 'getline' is very well explained (for dummies like me)

Thanks a lot
jjgalan is offline   Reply With Quote
Old Jun 18th, 2011, 6:02 AM   #4
scottn
Hobbyist Programmer
 
scottn's Avatar
 
Join Date: Sep 2010
Location: der Schweiz
Posts: 146
Rep Power: 4 scottn is on a distinguished road
Re: awk print interval defined by two regexp

$ awk '/\*/ {p=0} /\*FIELD CS\*/ {p=1}p' file1
*FIELD CS*
YYYYYYYYYYY
abababababababa
*FIELD CS*
4444444444444444444444444
ccvcvcvcvcvcvcvcvcvcvcvcvcvcvcvc
ggsgsgsgsgsgsgsgsgsgsgsgsgsgs

There's not really much to getline. It's well enough explained in the awk man page:
The ``function'' getline sets $0 to the next input record from the current
input file; getline <file sets $0 to the  next record from file.  getline
x sets variable x instead.  Finally, cmd | getline pipes the output
of cmd into getline; each call of getline returns the next line of
output from cmd.   In all cases, getline  returns  1 for a successful
input, 0 for end of file, and -1 for an error.
__________________
Solaris - AIX - OS X
UNIX not GNU
SPAM Hater
scottn is offline   Reply With Quote
Old Jun 20th, 2011, 3:16 AM   #5
jjgalan
Newbie
 
Join Date: Jun 2011
Posts: 6
Rep Power: 0 jjgalan is on a distinguished road
Re: awk print interval defined by two regexp

Thanks a lot scottn!!!
jjgalan is offline   Reply With Quote
Reply

Bookmarks

Tags
awk, regexp, register interval

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
AWK Syntax Unexpected } porphyry5 Sed and Awk 4 May 12th, 2011 12:31 AM
more awk problems. fisk4 Sed and Awk 0 Feb 6th, 2011 3:30 PM
Awk 2D arrays Zo... Sed and Awk 4 Oct 1st, 2010 10:52 AM
ftplib help please! disAbled Python 1 Jun 30th, 2005 8:05 PM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 6:58 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC