script question - Unix

This is a discussion on script question - Unix ; >From a script, I want to print all the lines of a file up to a line containing the word "END". Something like: # grep -avw -m 1 "END" -f bla.txt or # cat bla.txt | while read line; do ...

+ Reply to Thread
Results 1 to 5 of 5

Thread: script question

  1. script question

    >From a script, I want to print all the lines of a file up to a line
    containing the word "END".
    Something like:

    # grep -avw -m 1 "END" -f bla.txt

    or

    # cat bla.txt | while read line; do
    echo $line
    if [ "$line" == "END" ]; then break; fi
    done


    ....except a script that actually works. The second one above does not
    work because
    the "read line" corrupts the lines by removing whitespace. I don't
    know why the
    first one doesn't work.

    -Mike


  2. Re: script question

    In <1174683868.851106.205880@e65g2000hsc.googlegroups. com> "Mike" writes:

    > >From a script, I want to print all the lines of a file up to a line

    > containing the word "END".


    Did you really mean "containing" the word END? From your examples below,
    I don't think you did.

    > Something like:


    > # grep -avw -m 1 "END" -f bla.txt


    > or


    > # cat bla.txt | while read line; do
    > echo $line
    > if [ "$line" == "END" ]; then break; fi
    > done


    > ...except a script that actually works. The second one above does not
    > work because the "read line" corrupts the lines by removing whitespace.
    > I don't know why the first one doesn't work.


    The first one doesn't work for two reasons.

    First, you're misusing the -f flag. It is not used to specify the file
    to be searched.

    Second, by using the -m 1 flag, you're telling grep to stop after finding
    one match. But you're also using -v, which reverses all matches. So you're
    actually telling grep to stop after finding one line that DOES NOT match
    "END" -- which, presumably, would be the very first line in the file.

    Anyway, here's a sed script that should do what you want:

    sed -n '1,/^END$/p' bla.txt

    This script will also print the line "END", so if you don't want that,
    add this:

    | grep -v '^END$'

    --
    John Gordon A is for Amy, who fell down the stairs
    gordon@panix.com B is for Basil, assaulted by bears
    -- Edward Gorey, "The Gashlycrumb Tinies"


  3. Re: script question

    Mike wrote:
    >>From a script, I want to print all the lines of a file up to a line

    > containing the word "END".


    awk '1;/END/{exit}' file

    or

    awk '/END/{exit}1' file

    depending on whether or not you want the matching line printed.

    Ed.

  4. Re: script question

    On 2007-03-23, Mike wrote:
    >>From a script, I want to print all the lines of a file up to a line

    > containing the word "END".
    > Something like:
    >
    > # grep -avw -m 1 "END" -f bla.txt


    That will stop at the first line that does NOT contain "END" (the -v
    option reverses the test).

    > or
    >
    > # cat bla.txt | while read line; do
    > echo $line
    > if [ "$line" == "END" ]; then break; fi
    > done
    >
    > ...except a script that actually works. The second one above does
    > not work because the "read line" corrupts the lines by removing
    > whitespace.


    Unless it's a small file, using a while loop to read it is very
    inefficient.

    > cat bla.txt | while read line; do
    > echo $line
    > if [ "$line" == "END" ]; then break; fi
    > done


    First of all, you don't need cat.

    Secondly, you are stripping off leading and trailing spaces
    because you did not set IFS, and also because you did not quote
    $line when you echoed it.

    while IFS= read -r line; do
    printf "%s\n" "$line"
    if [ "$line" = "END" ]; then break; fi
    done < bla.txt

    If the line containing END has other characters on it, use case
    instead of if:

    case $line in
    *END*) break ;;
    esac

    > I don't know why the first one doesn't work.


    See above.

    --
    Chris F.A. Johnson, author |
    Shell Scripting Recipes: | My code in this post, if any,
    A Problem-Solution Approach | is released under the
    2005, Apress | GNU General Public Licence

  5. Re: script question

    2007-03-23, 14:04(-07), Mike:
    >>From a script, I want to print all the lines of a file up to a line

    > containing the word "END".
    > Something like:
    >
    > # grep -avw -m 1 "END" -f bla.txt
    >
    > or
    >
    > # cat bla.txt | while read line; do
    > echo $line
    > if [ "$line" == "END" ]; then break; fi
    > done
    >
    >
    > ...except a script that actually works. The second one above does not
    > work because
    > the "read line" corrupts the lines by removing whitespace. I don't
    > know why the
    > first one doesn't work.

    [...]

    sed /END/q < bla.txt

    Remember a shell is a command line interpreter, pick up the
    command most suited for the job. Using a while read loop is the
    most alien way to do shell programming.

    --
    Stéphane

+ Reply to Thread