printing next line .... 
Author Message
 printing next line ....

Am a complete novice at using awk ...have a very basic question ...
Can someone help me figure out how to print the next line when a condition
is satisfied for a particular line
Example file ....

1   1   2  3
print this
1   2   2   3
no print
2  2   3  4
print this

I want to print all lines following lines which have same 1st and 2nd field
s illustrated in the program below:

awk ' $1 == $2 {print (how to print next line)}'



Mon, 12 Jan 2004 22:21:50 GMT  
 printing next line ....

Quote:

> ...
> I want to print all lines following lines which have same 1st and 2nd fields
> illustrated in the program below:

> awk ' $1 == $2 {print (how to print next line)}'

Think it the other way: to print nth line you need to know whether
the previous (n-1)th line satisfied the condition or not.

  itdid  # print current nth line if (n-1)th did satisfy the condition
  { itdid = ($1==$2) } # store condition for current nth line

--
  All true believers shall break their eggs at the convenient end.



Mon, 12 Jan 2004 22:49:21 GMT  
 printing next line ....

Quote:

>Am a complete novice at using awk ...have a very basic question ...
>Can someone help me figure out how to print the next line when a condition
>is satisfied for a particular line
>Example file ....

>1   1   2  3
>print this
>1   2   2   3
>no print
>2  2   3  4
>print this

>I want to print all lines following lines which have same 1st and 2nd field
>s illustrated in the program below:

>awk ' $1 == $2 {print (how to print next line)}'

The straightfoward (but less elegant - meaning, less flexible) solution is:

        $1 == $2 {getline;print}

Note that I'm not necessarily recommending this in the general case, but if
your problem is as you describe and your data is well behaved, it will work.



Mon, 12 Jan 2004 23:47:22 GMT  
 printing next line ....

Quote:

>Am a complete novice at using awk ...have a very basic question ...
>Can someone help me figure out how to print the next line when a condition
>is satisfied for a particular line
>Example file ....

>1   1   2  3
>print this
>1   2   2   3
>no print
>2  2   3  4
>print this

>I want to print all lines following lines which have same 1st and 2nd field
>s illustrated in the program below:

>awk ' $1 == $2 {print (how to print next line)}'

set a flag:

awk 'printnext == 1 {print; printnext = 0}
     $1 == $2 {printnext = 1}' infile

Chuck Demas

--
  Eat Healthy    |   _ _   | Nothing would be done at all,

  Die Anyway     |    v    | That no one could find fault with it.



Tue, 13 Jan 2004 03:43:59 GMT  
 printing next line ....

Quote:


> > ...
> > I want to print all lines following lines which have same 1st and 2nd fields
> > illustrated in the program below:

> > awk ' $1 == $2 {print (how to print next line)}'

> Think it the other way: to print nth line you need to know whether
> the previous (n-1)th line satisfied the condition or not.

>   itdid  # print current nth line if (n-1)th did satisfy the condition
>   { itdid = ($1==$2) } # store condition for current nth line

> --
>   All true believers shall break their eggs at the convenient end.


awk '
print_next {
        print $0
Quote:
}

{
        print_next = ($1 == $2)

- Show quoted text -

Quote:
}'



Tue, 13 Jan 2004 18:41:51 GMT  
 printing next line ....

Quote:


> > itdid  # print current nth line if (n-1)th did satisfy the condition
> > { itdid = ($1==$2) } # store condition for current nth line

> print_next { print $0 }
> { print_next = ($1 == $2) }'

So?  
  '{print $0}'  tantamounts to '{print}'
  and '{print}' tantamounts to ''

--
  All true believers shall break their eggs at the convenient end.



Tue, 13 Jan 2004 23:35:39 GMT  
 printing next line ....

Quote:



> > > itdid  # print current nth line if (n-1)th did satisfy the condition
> > > { itdid = ($1==$2) } # store condition for current nth line

> > print_next { print $0 }
> > { print_next = ($1 == $2) }'

> So?
>   '{print $0}'  tantamounts to '{print}'
>   and '{print}' tantamounts to ''

> --
>   All true believers shall break their eggs at the convenient end.


Sorry...


Wed, 14 Jan 2004 01:39:16 GMT  
 printing next line ....
It took me a long while to home in on what bothered me about the
solutions to . . .

Quote:

> Example file ....

> 1   1   2  3
> print this
> 1   2   2   3
> no print
> 2  2   3  4
> print this

> I want to print all lines following lines which have same
> 1st and 2nd fields illustrated in the program below:

> awk ' $1 == $2 {print (how to print next line)}'

> set a flag:

> awk 'printnext == 1 {print; printnext = 0}
>      $1 == $2 {printnext = 1}' infile

> The straightfoward (but less elegant - meaning, less
> flexible) solution is:

>         $1 == $2 {getline;print}

> Note that I'm not necessarily recommending this in the
> general case, but if your problem is as you describe and
> your data is well behaved, it will work.

               ^^^^^^^^^^^^
 . . . but if the data is slightly ill behaved, just a little
rude . . .    

C:> type example.file
1   1   2   3
print this
1   2   2   3
no print
2   2   3   4
print this, too
3   3   4   5
print print this this  <<<<< $1 == $2
6   6   7   8
print this also
6   7   7   8
no print either
6   7   7   8
no no don't print      <<<<< $1 == $2
7   7   6   9
print this as well

Five lines should be printed.  

 . . . Charles's does this . . .

C:> gawk310 'printnext == 1 {print; printnext = 0} $1==$2
{printnext = 1}' example.file
print this
print this, too
print print this this
6   6   7   8
print this also
7   7   6   9
print this as well

 . . . score: 5/5, 2 extra lines.  

Kenny's does this . . .

C:> gawk310 '$1 == $2 {getline; print}' example.file
print this
print this, too
print print this this
print this also
7   7   6   9

 . . . score: 4/5, 1 extra line.  

If the lines to be printed might have $1 == $2 one could simply
overlook spurious output lines or test that only even numbered
lines could be printed.  Changing the first condition in
Charles's to

     (printnext == 1) && (!(FNR%2))

and Kenny's to

     ($1 == $2) && (FNR%2)

makes them work.  However, any odd number of spurious
("non-data") lines in the data will botch them up between each
odd occurrence and the next, even one.  

This problem is just as hard as deciding, "what is a word?"  

Regards,
--

a Dancing Spirit Programmer in Asheville, nestled
in the mountains of Western North Carolina, U.S.A.



Tue, 27 Jan 2004 17:19:40 GMT  
 printing next line ....


% C:> type example.file
% 1   1   2   3
% print this
% 1   2   2   3
% no print
% 2   2   3   4
% print this, too
% 3   3   4   5
% print print this this  <<<<< $1 == $2
% 6   6   7   8
% print this also
% 6   7   7   8
% no print either
% 6   7   7   8
% no no don't print      <<<<< $1 == $2
% 7   7   6   9
% print this as well
%
% Five lines should be printed.  

I don't understand your rationale for saying five lines. It looks
like there are seven lines which should be printed. What in the
original request makes you want to throw lines out?

%  . . . Charles's does this . . .

[print all seven lines] -- sounds like a good idea to me. Why would
it bother you?

--

Patrick TJ McPhee
East York  Canada



Wed, 28 Jan 2004 10:25:30 GMT  
 printing next line ....

Quote:



> % C:> type example.file
> % 1   1   2   3
> % print this
> % 1   2   2   3
> % no print
> % 2   2   3   4
> % print this, too
> % 3   3   4   5
> % print print this this  <<<<< $1 == $2
> % 6   6   7   8
> % print this also
> % 6   7   7   8
> % no print either
> % 6   7   7   8
> % no no don't print      <<<<< $1 == $2
> % 7   7   6   9
> % print this as well
> %
> % Five lines should be printed.

> I don't understand your rationale for saying five lines.
> It looks like there are seven lines which should be
> printed. What in the original request makes you want to
> throw lines out?

> %  . . . Charles's does this . . .

> [print all seven lines] -- sounds like a good idea to me.
> Why would it bother you?


Venkat Pisipati said this:  

Quote:
> I want to print all lines following lines which have same
> 1st and 2nd fields illustrated in the program below:

and he gave this example:

Quote:
> 1   1   2   3
> print this
> 1   2   2   3
> no print
> 2   2   3   4
> print this

Although it was not stated, it is clear from this example that
the data consisted of a line of numbers followed by a line of
text, and that the problem was to print only each line of text
that immediately followed a line of numbers in which the first
two numbers were equal.  

What bothered me, I realized later, was the possibility that the
first two words (fields) in a line of *text* might be identical,
causing the two solutions given to print out the next line of
numbers.  The output should have been all text, but there were
foreseeable circumstances where it would not be.  

I provided a counter-example data that caused the two solutions
to "misbehave," and an unsatisfactory amendment to each solution,
with caveats.  

Regards,
--

a Dancing Spirit Programmer in Asheville, nestled
in the mountains of Western North Carolina, U.S.A.



Wed, 28 Jan 2004 17:36:13 GMT  
 printing next line ....


[I had written]

% > printed. What in the original request makes you want to
% > throw lines out?

% Although it was not stated, it is clear from this example that
% the data consisted of a line of numbers followed by a line of
% text, and that the problem was to print only each line of text

Exactly. There is nothing in the original post to say that
a record which starts with two duplicate fields should be handled
differently simply because it follows another record which starts
with two duplicate fields. How it should be handled depends on
the data, of which we know nothing (I wouldn't read too much into
the given example -- it's difficult to express `print this' as
a bunch of numbers, so obviously those lines have text in them).

It seems like we have three problems, and two solutions:

 1. the problem as stated by the original poster. Demas's solution
    is correct for this;
 2. a file consists of alternating records of type A and B. If the
    first two fields of an A are identical, print the following B.
    The other solution (I forget who posted it) might have been
    correct for this -- it should call getline on every A record,
    but only print if the test was true;
 3. a file consists of records of type A and B, with the types being
    determined by the data in the records. If the first two fields
    of an A are identical, print the following B. This could be
    solved by adding an additional test to either of the posted
    solutions (i.e., make the pattern typea($0) && $1 == $2 { ... },
    where typea is defined to match record type A (it could return
    $1 == $1+0 if the test is for a numeric field 1, for instance).

So, even if there are additional, unstated, requirements, it seems like
the posted solutions adapt reasonably well.
--

Patrick TJ McPhee
East York  Canada



Thu, 29 Jan 2004 01:31:22 GMT  
 printing next line ....

Quote:



> [I had written]

> % > printed. What in the original request makes you want
> % > to throw lines out?

> % Although it was not stated, it is clear from this
> % example that the data consisted of a line of numbers
> % followed by a line of text, and that the problem was
> % to print only each line of text

> Exactly. There is nothing in the original post to say
> that a record which starts with two duplicate fields
> should be handled differently simply because it follows
> another record which starts with two duplicate fields.

Patrick, are you a lawyer rather than a brewer of vinegar (and
cider?)?  ;-)>  

As I said, the first two solutions met "the letter of the law" in
the OP from Venkat Pisipati.  My comments were to my inference
that the "spirit" of the problem was your case 2.  That's all.
Only Venkat could say what was intended or which solution met his
needs.  

Regards,
--

a Dancing Spirit Programmer in Asheville, nestled
in the mountains of Western North Carolina, U.S.A.



Thu, 29 Jan 2004 07:02:30 GMT  
 
 [ 12 post ] 

 Relevant Pages 

1. Search file and print line + next line?

2. Printing line by line

3. The pretty-printer, *print-lines* and per-line prefixes

4. Print formatting - same line printing?

5. Match something then process next line

6. Overprint next line

7. split-next-line

8. comments and continuing strings on the next line

9. Need HELP on finishing pics on next line.

10. Draft of the next COBOL Standard - *not* Available on-line

11. write(*,*), jumps to the next line

12. control the write not to next line

 

 
Powered by phpBB® Forum Software