Computing.Net > Forums > Unix > grep/awk/conditional print

grep/awk/conditional print

Reply to Message Icon

Original Message
Name: daelomin
Date: August 2, 2006 at 09:42:44 Pacific
Subject: grep/awk/conditional print
OS: Linux
CPU/Ram: P4/1gig
Comment:

Hi,

I wish to print a certain area of some code based on two delimiters. The first one I achieve by grepping the file and then I use "-A 30" to print the file after that occurence.
My problem is I want to print only up to the "endif" part

Here is the file:
1 else if (cdretr == 'GP_GET_SSMI') then
2
3 if (LLECMA) then
4 CALL next_context(cdretr, version=0, nviews=2)
5 endif
6
7 else if (cdretr == 'GP_PUT_SSMI') then
8 blabla


So what I would like is some AWK script that will print lines 1 to 5, disregarding where on the line "endif" is.

any clue?

Thanks! :)



Report Offensive Message For Removal

Response Number 1
Name: lchi2000g
Date: August 2, 2006 at 11:53:28 Pacific
Subject: grep/awk/conditional print
Reply: (edit)

Use:

grep -A 30 GP_GET_SSMI file8.txt | sed -n '1,/^endif/p'

Example:

/home/oracle/luke/tmp$ cat file8.txt
...
else if (cdretr == 'GP_GET_SSMI') then

if (LLECMA) then
CALL next_context(cdretr, version=0, nviews=2)
endif

else if (cdretr == 'GP_PUT_SSMI') then
blabla

/home/oracle/luke/tmp$ grep -A 30 GP_GET_SSMI file8.txt | sed -n '1,/^endif/p'
else if (cdretr == 'GP_GET_SSMI') then

if (LLECMA) then
CALL next_context(cdretr, version=0, nviews=2)
endif

Luke Chi


Report Offensive Follow Up For Removal

Response Number 2
Name: daelomin
Date: August 3, 2006 at 01:06:19 Pacific
Subject: grep/awk/conditional print
Reply: (edit)

Almost! ;)

Problem is, with indentation that can vary, endif can be anywhere on the line...

cat file | sed -n -e '1,/^.*endif/p'

seems to do the trick however!

Thanks for that Luke.

I still wonder if one can do temporary buffering of many lines until reaching some pattern before printing stuff out...



Report Offensive Follow Up For Removal

Response Number 3
Name: lchi2000g
Date: August 3, 2006 at 20:23:01 Pacific
Subject: grep/awk/conditional print
Reply: (edit)

Please explain a little more about "one can do temporary buffering of many lines until reaching some pattern before printing stuff out...
"

Luke Chi


Report Offensive Follow Up For Removal

Response Number 4
Name: James Boothe
Date: August 7, 2006 at 11:56:49 Pacific
Subject: grep/awk/conditional print
Reply: (edit)

Sure, you can accumulate/buffer your lines, and it comes in very handy for some trickier problems.

With awk, you can store the lines in an array, or even append multiple lines to a single variable. But if you append, you also need to append your own newline separation. sed has a hold buffer that will accumulate lines.

With basic shell commands, I would store them in an array (maximum 1023 entries I think).


Report Offensive Follow Up For Removal

Response Number 5
Name: daelomin
Date: August 8, 2006 at 08:06:49 Pacific
Subject: grep/awk/conditional print
Reply: (edit)

That sounds like what I need :)

So say you have this somewhere in a file:

IF(TEST_BOOLEAN); THEN
## some comment
print "Begin"
call mysubroutine()
print "Value=" value
ELSE
blabla
ENDIF

How could you return only the main skeleton of the program by storing the main lines into the array in AWK?

Namely, you'd have:

IF(TEST_BOOLEAN); THEN
call mysubroutine()
ELSE
ENDIF

& even noticing that the ELSE holds no interesting command, just return this:

IF(TEST_BOOLEAN); THEN
call mysubroutine()
ENDIF

Thanks


Report Offensive Follow Up For Removal


Response Number 6
Name: daelomin
Date: August 8, 2006 at 08:32:33 Pacific
Subject: grep/awk/conditional print
Reply: (edit)

I have posted a new topic, more explanatory on the issue...


Report Offensive Follow Up For Removal






Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: grep/awk/conditional print

Comments:

 


  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 
Data Recovery Software