To check more records at once (?) 
Author Message
 To check more records at once (?)

I guess it's possible in AWK...

I need to detect empty lines _before_ and _after_ the current
record, how can I do this?

Input file:

<empty line>
<Tab>   valami
<empty line>

Desired:

To do something to 'valami', IF

1. the line starts with a <Tab> char.
2. before and after this record there are empty lines

PS. of course I searched my local AWK doc, but found nothing
about the 2. condition... :(

--
Mihaly Gyulai

http://www.*-*-*.com/

Sent via Deja.com http://www.*-*-*.com/
Before you buy.



Tue, 18 Feb 2003 16:49:55 GMT  
 To check more records at once (?)

deja.com> writes

Quote:
>I guess it's possible in AWK...

>I need to detect empty lines _before_ and _after_ the current
>record, how can I do this?

>Input file:

><empty line>
><Tab>   valami
><empty line>

>Desired:

>To do something to 'valami', IF

>1. the line starts with a <Tab> char.
>2. before and after this record there are empty lines

>PS. of course I searched my local AWK doc, but found nothing
>about the 2. condition... :(

>--
>Mihaly Gyulai

>http://www.freeyellow.com/members5/gyulai/

>Sent via Deja.com http://www.deja.com/
>Before you buy.

Hi Michaly,

I had an idea that I could write a general purpose program to be
prepended to an awk script which would provide variables PREVLINE and
NEXTLINE. This would make it very easy to do what you asked.

Writing such a program turned out to be a little more complicated than I
expected and takes about 30 lines of awk but at least, if you use igawk,
it can be easily included in your programs.

This is file prevnext.awk

#provide PREVLINE and NEXTLINE variables
#for use in an awk script
#note :-
#1) test FILENAME==ENDFILE to see if you are at the end
#2) note that NR and FNR correspond to NEXTLINE not to $0

BEGIN {# append a blank line to the input stream
  if (ARGC==1) ARGV[ARGC++]="-"
  ENDFILE="/tmp/endfile"
  print "" > ENDFILE
  close(ENDFILE)
  ARGV[ARGC++]=ENDFILE

Quote:
}

#set up a buffer : previous, current and next lines
{
  PREVLINE=CURRLINE
  TMP=$0
  $0=NEXTLINE
  NEXTLINE=TMP
  if (NR==1) next
  CURRLINE=$0

Quote:
}

#tidy up
END {
#following 2 lines needed to let rm ENDFILE work with awk95
  close(ENDFILE)
  while (getline==1)
  system("rm "ENDFILE)

Quote:
}

This is how to use prevnext.awk for your task :-

#!igawk -f

FILENAME==ENDFILE {exit}

NR<3 {next}

PREVLINE~/^$/ && /\tvalami/ && NEXTLINE~/^$/ {
  print "do something to valami"

Quote:
}

I have tested this using gawk 3.0.6, Windows 95, MSDOS and sh using some
DJGPP Unix-like utilities. It should work with Unix gawk/igawk.

In DOS the command is :-

igawk -f script.awk datafile

In a unix shell your options include:-

igawk -f ./script.awk datafile

./script.awk datafile

cat datafile | igawk -f ./script.awk

cat datafile | ./script.awk

If you don't use igawk you can manually include prevnext.awk instead. In
that case I know that it works with both gawk 3.0.6 and awk95.

I can't guarantee that this will work with other versions of awk.

Hope this helps
--
Alan Linton



Wed, 19 Feb 2003 02:03:45 GMT  
 To check more records at once (?)

deja.com> writes

Quote:
>I guess it's possible in AWK...

>I need to detect empty lines _before_ and _after_ the current
>record, how can I do this?

>Input file:

><empty line>
><Tab>   valami
><empty line>

>Desired:

>To do something to 'valami', IF

>1. the line starts with a <Tab> char.
>2. before and after this record there are empty lines

>PS. of course I searched my local AWK doc, but found nothing
>about the 2. condition... :(

>--
>Mihaly Gyulai

>http://www.freeyellow.com/members5/gyulai/

>Sent via Deja.com http://www.deja.com/
>Before you buy.

Here's another alternative.

#!gawk -f

{
  prev2=prev1
  prev1=curr
  curr=$0

Quote:
}

prev2~/^$/ && prev1~/\tvalami/ && $0~/^$/ {
  print "do something to valami"

Quote:
}

Hope this helps
--
Alan Linton


Wed, 19 Feb 2003 16:28:28 GMT  
 To check more records at once (?)
Hi  Mihaly

You have to take care if the _first_ and _last_ line in the file fulfil the
requirements of (<Tab>   valami). Also, if there can be several empty line in a
run, your script should handle this.

Here is my shot at it:

awk '
        /^$/ { before = NR; next }      # remember empty line
        /^[\t]valami/  &&  (before = NR-1)  {
                ThisLine = $0
                eof = getline
                if ( ($0 ~ /^$/)  ||  (eof<=0) ) {
                        before = NR     # this is a empty line, remember it
                        # do your something stuff on ThisLine
                        .... something stuff ....
                }
        }
'  filename

Hope it is useful.

Greetings

Pierre-Yves
Best Regards




Fri, 21 Feb 2003 02:12:20 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. Once Again Please: Mark Records

2. Records appearing more than once in CDX file

3. {caps,num}lock - once more for the record

4. VAST : ResultTable check Departing of record

5. VAST : Check for departing of record

6. Checking record structure of a data file

7. Duplicate Record Checking - What is best way

8. How to check exisitence of record?

9. Import Checking for Dupe Records

10. Checking for locked record

11. Checking for Change Record

12. Which embed to check record count in browse?

 

 
Powered by phpBB® Forum Software