Search file and print line + next line? 
Author Message
 Search file and print line + next line?

Hello,

I have a file which has lines organized as such:

------------------
TITLE1 plus some other junk
This is some information about title 1 (one line)

TITLE2 plus some other junk
This is some information about title 2 (one line)

TITLE3 plus some other junk
This is some information about title 3 (one line)
------------------

I would like to be able to search for TITLE1 and have it print out the line
with TITLE1 in it (may not be alone on the line) , _plus_ the following line.

Is AWK the appropriate language to do this in, or can it be done with
GREP or SED?

As a generic extension, a secondary desire is to print out the next 'n' lines
instead of just the next one. But one is manditory.

Thanks,

        Steven

--
                                From the desk of:
                                        Steven Christensen   N9XJY



Tue, 22 Jun 1999 03:00:00 GMT  
 Search file and print line + next line?

Quote:

>Hello,

>I have a file which has lines organized as such:

>------------------
>TITLE1 plus some other junk
>This is some information about title 1 (one line)

>TITLE2 plus some other junk
>This is some information about title 2 (one line)

>TITLE3 plus some other junk
>This is some information about title 3 (one line)
>------------------

>I would like to be able to search for TITLE1 and have it print out the line
>with TITLE1 in it (may not be alone on the line) , _plus_ the following line.

>Is AWK the appropriate language to do this in, or can it be done with
>GREP or SED?

It could be done in sed.  In awk, you could simply write
awk '/TITLE1/{print;getline;print}' file

Quote:
>As a generic extension, a secondary desire is to print out the next 'n' lines
>instead of just the next one. But one is manditory.

>Thanks,

>    Steven

>--
>                            From the desk of:
>                                    Steven Christensen   N9XJY


--

If you crossed Tickle Me Elmo with that Cabbage Patch doll, would you get
a Bite Me Elmo?


Tue, 22 Jun 1999 03:00:00 GMT  
 Search file and print line + next line?


Quote:

>Hello,

>I have a file which has lines organized as such:

>------------------
>TITLE1 plus some other junk
>This is some information about title 1 (one line)

>TITLE2 plus some other junk
>This is some information about title 2 (one line)

>TITLE3 plus some other junk
>This is some information about title 3 (one line)
>------------------

>I would like to be able to search for TITLE1 and have it print out the line
>with TITLE1 in it (may not be alone on the line) , _plus_ the following line.

>Is AWK the appropriate language to do this in, or can it be done with
>GREP or SED?

>As a generic extension, a secondary desire is to print out the next 'n' lines
>instead of just the next one. But one is manditory.

>Thanks,

>        Steven

>--
>                                From the desk of:
>                                        Steven Christensen   N9XJY


Something like

BEGIN{
flag = 0
n = 1

Quote:
}

if( flag-- != 0 )
{
        print $0
{
else
{
        if( $1 ~/^TITLE/ )
        {
                flag = n
                print $0
        }

Quote:
}

That's a bit more explicit than some versions require, but I wanted to it to
be clear.  The n variable determines how many lines after the recognized one
are to be printed.

--



Wed, 23 Jun 1999 03:00:00 GMT  
 Search file and print line + next line?


Quote:
>I have a file which has lines organized as such:
>------------------
>TITLE1 plus some other junk
>This is some information about title 1 (one line)
>TITLE2 plus some other junk
>This is some information about title 2 (one line)
>------------------
>I would like to be able to search for TITLE1 and have it print out the line
>with TITLE1 in it (may not be alone on the line) , _plus_ the following line.
>Is AWK the appropriate language to do this in, or can it be done with
>GREP or SED?

Not the original AWK (which doesn't have getline), but this
  gawk '/pattern/ { print; getline; print }' filename
will work with gawk (GNU AWK) or nawk.

In sed, this
  sed -n '/pattern/{N;p;}' filename
works.

Quote:
>As a generic extension, a secondary desire is to print out the next 'n' lines
>instead of just the next one.

In gawk, wrap a for loop around the getline to print n lines.
In sed, this
  sed -n '/pattern/{N;N;N;p;}' filename
will for example print 4 lines, but makes n difficult to parameterize.


"A student is running ELIZA--'How are you?'  'Fine, thanks.'  'Let's talk
 about why you think you are fine.'--but thinks he's on a Teletype,
 conversing with the system operator.  He grows increasingly frustrated at
 the program's metagames.  Finally, he dials a voice-phone & gets the human
 he thinks he's been talking to.  'What the hell do you think you're doing?'
 'What do you mean, what the hell do I think I'm doing?'"
      --paraphrased from Richard Powers on p274 of /Galatea 2.2/



Wed, 23 Jun 1999 03:00:00 GMT  
 Search file and print line + next line?


Quote:
> I have a file which has lines organized as such:
> ------------------
> TITLE1 plus some other junk
> This is some information about title 1 (one line)

> TITLE2 plus some other junk
> This is some information about title 2 (one line)
> ------------------
> I would like to be able to search for TITLE1 and have it print out the line
> with TITLE1 in it (may not be alone on the line) , _plus_ the following line.
> Is AWK the appropriate language to do this in, or can it be done with
> GREP or SED?
> As a generic extension, a secondary desire is to print out the next 'n' lines
> instead of just the next one. But one is manditory.

here is a script i had written which does what u want and more:
(ppl, no flames for scripting in csh, plz :-))
----- 8< ----- cut here ----- 8< -----
#!/bin/csh -f
# see: print specified number of line above and/or below a specified pattern
# Usage: see [-i] [-s] [-l no-of-lines] [-a|-b] <pattern> <file-list>

# set default values
set silent_mode="off"
set grep_flags="-n"
set domain="both"
set no_of_lines=3

# check for proper usage: number of arguments
if ($#argv < 2) then
  if ($#argv == 1 && "$1" == "-h") then
    cat << --END-OF-HELP--
 ==============================================================================
                       Help screen for the 'see' command
 ------------------------------------------------------------------------------
        This command prints the context around a pattern in from a file.
        That is, it prints specified number of lines above and below the
        line that matched the given pattern. It uses the 'grep' command
        internally to do the processing.

        The command takes the following options:

        -h      prints this help screen.

        -l n    prints n number of lines around the line that matches
                the pattern (defaults to 3).

        -a      prints specified number of lines above the pattern matching
                line only (defaults to both - above and below).

        -b      prints specified number of lines below the pattern matching
                line only (defaults to both - above and below).

        -i      performs a case insensitive search (default off).

        -s      silent mode. does not complain about non-existant and non
                readable files (default off).
 ==============================================================================
--END-OF-HELP--
    exit
  else
    echo "see: usage: see [-i] [-s] [-l no-of-lines] [-a|-b] <pattern> <file-list>"
    exit
  endif
endif

# parse the input line
while ($#argv)
  if ("`echo $1 | cut -c1`" == "-") then
    set opt=`echo $1|cut -c2-`
    if ("$opt" == "a") then
      set domain="above"
      shift
    else if ("$opt" == "b") then
      set domain="below"
      shift
    else if ("$opt" == "s") then
      set silent_mode="on"
      shift
    else if ("$opt" == "i") then
      set grep_flags="-in"
      shift
    else if ("$opt" == "l") then
      set no_of_lines=$2
      shift
      shift
    else
      echo "see: usage: see [-i] [-s] [-l no-of-lines] [-a|-b] <pattern> <file-list> "
      exit
    endif
  else
    set pattern=$argv[1]
    set files="$argv[2-]"
    break
  endif
end

# if we still dont have $pattern and $files, then quit
if ($?pattern == 0 || $?files == 0) then
  echo "see: usage: see [-i] [-s] [-l no-of-lines] [-a|-b] <pattern> <file-list>"
  exit
endif

# ok, got all we need... now proceed with finding the pattern
# and printing the context
foreach file ($files)
if (!(-e $file)) then
  if ("$silent_mode" == "off") echo "see: Not found: $file"
  continue
else if (!(-r $file)) then
  if ("$silent_mode" == "off") echo "see: Not readable: $file"
  continue
else
  if ( { grep -l $pattern $file } ) then
  echo "===================================================================="
  echo "NEW FILE: $file:t"
  echo "--------------------------------------------------------------------"
  set lines=`grep $grep_flags $pattern $file | cut -f1 -d: | tr '\012' ' '`
    foreach found_line ($lines)
    if ("$domain" == "above") then


    else if ("$domain" == "below") then


    else if ("$domain" == "both") then


    else
        echo "see: Internal error occured!"
    endif
    sed -n "$start_line,$end_line p" $file
    echo "--------------------------------------------------------------------"
    end
  echo ""
  else
    if ("$silent_mode" == "off") echo "see: Pattern not found in file: $file"
endif
end
----- 8< ----- ends here ----- 8< -----



Wed, 23 Jun 1999 03:00:00 GMT  
 Search file and print line + next line?

Hello Steven,

this two lines awk script does the job:

/^TITLE1/        {i=2;}
i && i--

In the first line you can change the pattern TITLE1 and the number of
required lines, they are printed by the second line (default action).

Best wishes,
Toth Andras


: Hello,

: I have a file which has lines organized as such:

: ------------------
: TITLE1 plus some other junk
: This is some information about title 1 (one line)

: TITLE2 plus some other junk
: This is some information about title 2 (one line)

: TITLE3 plus some other junk
: This is some information about title 3 (one line)
: ------------------

: I would like to be able to search for TITLE1 and have it print out the line
: with TITLE1 in it (may not be alone on the line) , _plus_ the following line.

: Is AWK the appropriate language to do this in, or can it be done with
: GREP or SED?

: As a generic extension, a secondary desire is to print out the next 'n' lines
: instead of just the next one. But one is manditory.

: Thanks,

:       Steven

: --
:                               From the desk of:
:                                       Steven Christensen   N9XJY



Fri, 25 Jun 1999 03:00:00 GMT  
 Search file and print line + next line?

Quote:

>Maybe I am just making these too easy but wouldnt

>grep -A 1 TITLE1 file

>work?

Speak for yourself - I just tried it on HP-UX, SunOS, and OSF/1, and none
of them support the "-A" flag.

Andrew



Sat, 26 Jun 1999 03:00:00 GMT  
 Search file and print line + next line?

Maybe I am just making these too easy but wouldnt

grep -A 1 TITLE1 file

work? That is what I use.  Those elaborate scripts are long and
hard to intergreat into others I think. Under different OS's that
might change though. but it does exist.


: Hello,

: I have a file which has lines organized as such:

: ------------------
: TITLE1 plus some other junk
: This is some information about title 1 (one line)

: TITLE2 plus some other junk
: This is some information about title 2 (one line)

: TITLE3 plus some other junk
: This is some information about title 3 (one line)
: ------------------

: I would like to be able to search for TITLE1 and have it print out the line
: with TITLE1 in it (may not be alone on the line) , _plus_ the following line.

: Is AWK the appropriate language to do this in, or can it be done with
: GREP or SED?

: As a generic extension, a secondary desire is to print out the next 'n' lines
: instead of just the next one. But one is manditory.

: Thanks,

:       Steven

: --
:                               From the desk of:
:                                       Steven Christensen   N9XJY

--
                                        natambu.



Sat, 26 Jun 1999 03:00:00 GMT  
 Search file and print line + next line?

Quote:


> (Natambu Obleton) writes:
> >Maybe I am just making these too easy but wouldnt
> >   grep -A 1 TITLE1 file
> >work?

> Speak for yourself - I just tried it on HP-UX, SunOS, and OSF/1, and none
> of them support the "-A" flag.

That's because he's using GNU grep and you're not.  In any event, it has
nothing to do with awk.

roland
--
Roland Roberts, PhD                              Muller Data Corporation

                                                 New York, NY



Sat, 26 Jun 1999 03:00:00 GMT  
 Search file and print line + next line?

Quote:


>>I have a file which has lines organized as such:

>>------------------
>>TITLE1 plus some other junk
>>This is some information about title 1 (one line)

>>TITLE2 plus some other junk
>>This is some information about title 2 (one line)
>>------------------

>>I would like to be able to search for TITLE1 and have it print out the line
>>with TITLE1 in it (may not be alone on the line) , _plus_ the following line.

>>Is AWK the appropriate language to do this in, or can it be done with
>>GREP or SED?

Try GNU grep (really, GNU egrep): has the switches
   -A<n> and -B<n>, for "n" lines Before or After.

Does right thing when two ranges OVERLAP, no simple thing
to handle in awk, etc.

Like: (gnu)egrep -A1 TITLE1 myfile >! t.loc

Is EXACTLY what you need.



Wed, 30 Jun 1999 03:00:00 GMT  
 Search file and print line + next line?

Quote:

> I would like to be able to search for TITLE1 and have it print out the
> line with TITLE1 in it (may not be alone on the line) , _plus_ the
> following line.

this worked for me:

``
BEGIN { printing=0; }
/^TITLE/ { printing= ($1=="TITLE1") ; }
(printing)
''

summary:

  at the beginning, we aren't printing.

  when we see a TITLE line, we're printing IF AND ONLY IF it's TITLE1

  when we see a line, print it if need be.
--



Mon, 05 Jul 1999 03:00:00 GMT  
 
 [ 11 post ] 

 Relevant Pages 

1. printing next line ....

2. Printing line by line

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

4. Searching for an OS independing method for line printing

5. searching blocks of lines in a file ...

6. reversing lines char by char, but not the line order in a file

7. Search and replace text in a file based on a specific line

8. how to read text files line by line?

9. reading file line by line

10. new question in how can i read data from file line by line

11. How to compare two files line-by-line

12. line-by-line file read

 

 
Powered by phpBB® Forum Software