Lines Side by Side? 
Author Message
 Lines Side by Side?

Hi,

I need help to get some lines only if they are side by side and in order
of the RE I specified at the command lines(see "desired output" below).
 The RE to get must not be on the same lines...

$ cat input.data
1. 123 hello
2. 12345  hi
3. 123567 resting
4.
5. hello me
6. hi you
7. there is
8. nothing
9. there is
10.
11. hello there
12. hi
13. you can
14. there is
15. we are

$ scriptname RE1 RE2 RE3 inputfile

example:
$ scriptname hello hi there input.data

--Good output--
5. hello me
6. hi you
7. there is

--Bad output--
5. hello me
6. hi you
7. there is

11. hello there
12. hi
14. there is

Your help is greatly appreciated.

TIA,
jenny

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



Fri, 14 Feb 2003 03:00:00 GMT  
 Lines Side by Side?

writes

Quote:
>Hi,

>I need help to get some lines only if they are side by side and in order
>of the RE I specified at the command lines(see "desired output" below).
> The RE to get must not be on the same lines...

>$ cat input.data
>1. 123 hello
>2. 12345  hi
>3. 123567 resting
>4.
>5. hello me
>6. hi you
>7. there is
>8. nothing
>9. there is
>10.
>11. hello there
>12. hi
>13. you can
>14. there is
>15. we are

>$ scriptname RE1 RE2 RE3 inputfile

>example:
>$ scriptname hello hi there input.data

>--Good output--
>5. hello me
>6. hi you
>7. there is

>--Bad output--
>5. hello me
>6. hi you
>7. there is

>11. hello there
>12. hi
>14. there is

>Your help is greatly appreciated.

>TIA,
>jenny

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

Hello Jenny,

I had to reread your question several times before I understood it. I
think I have got it but let me know if I'm wrong.

#!gawk -f

BEGIN {
  if (ARGC<5) {
    print "too few arguments, 3 REs"
    print "+ 1 or more file names expected"
    exit
  }
  RE1=ARGV[1]
  RE2=ARGV[2]
  RE3=ARGV[3]
  for (i=1; i<ARGC-3; i++) ARGV[i]=ARGV[i+3]
  ARGC-=3

Quote:
}

{x[++n]=$0}

END {
  for (i=1; i<=n-2; i++) {
    if (x[i]~RE1 && x[i+1]~RE2 && x[i+2]~RE3) {
      print x[i]
      print x[i+1]
      print x[i+2]
    }
  }

Quote:
}

I have tested this with gawk 3.0.6 and awk95 and got good output.

sh-2.03$ ./scriptname hello hi there input.data
5. hello me
6. hi you
7. there is
sh-2.03$

This script is not suitable for multigigabyte input files because it
reads the whole input stream into an array before starting to process
it. I can streamline it if it does not suit your needs,

Hope this helps
--
Alan Linton



Sun, 16 Feb 2003 03:00:00 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. Class side vs Instance Side??

2. smalltalk - java side by side

3. smalltalk - java side by side

4. Printing group footers side-by-side

5. Report details side by side

6. New MVS REXX Utility: Compare partitioned datasets side-by-side - pdsmatch.zip (1/1)

7. client side or server side scripting- new web user

8. Two text widgets side by side?

9. Is it possible to concatenate two text files side by side?

10. How to put columns of two files into one file (side by side)?

11. append the two sets of data side by side at different times using"write to spreadsheet file vi"

12. Any issues with side-by-side Python installations on Windows?

 

 
Powered by phpBB® Forum Software