Results 1 to 6 of 6
  1. #1
    Join Date
    Sep 2008
    Posts
    4

    Unanswered: print lines between two patterns in unix

    sample :

    Detroit
    Chicago
    Newyork
    Battlecreek
    Jackson
    Brooklyn


    How would I print only lines match between Detroit and Brooklyn ?
    I don't want print Detroit and Brooklyn

    output should be :
    Chicago
    Newyork
    Battlecreek
    Jackson

    Thanks

    Jhonny

  2. #2
    Join Date
    Sep 2009
    Location
    Ontario
    Posts
    1,057
    Provided Answers: 1
    #!/bin/ksh
    print=no
    while read line
    do
    if [ $line = Brooklyn ]
    then
    print=no
    fi
    if [ $print = yes ]
    then
    echo $line
    fi
    if [ $line = Detroit ]
    then
    print=yes
    fi
    done

  3. #3
    Join Date
    Sep 2008
    Posts
    4
    thanks for reply.

    can it be done using sed or awk command ?

  4. #4
    Join Date
    Feb 2004
    Location
    In front of the computer
    Posts
    15,579
    Provided Answers: 54
    Quote Originally Posted by NZDF
    #!/bin/awk
    {if (/Chicago/) {p=1}; if (/Brooklin/) {p=0}; if (p) {print}}
    Note that this code works fine, but would likely get you failed if you turn it in as a classroom assignment.

    -PatP
    In theory, theory and practice are identical. In practice, theory and practice are unrelated.

  5. #5
    Join Date
    Sep 2008
    Posts
    4
    did not work
    awk {if (/Chicago/) {p=1}; if (/Brooklin/) {p=0}; if (p) {print}} q1
    -bash: syntax error near unexpected token `('
    thanks

  6. #6
    Join Date
    Sep 2009
    Location
    Ontario
    Posts
    1,057
    Provided Answers: 1
    Why do you suppose you are getting a bash error in an awk script.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •