Announcement

Collapse
No announcement yet.

file operation searching and printing strings from file

Collapse
This topic is closed.
X
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    file operation searching and printing strings from file

    Dear friends

    I have a file say file.txt
    and contents are

    mdffaf klafjsa fa
    fka f ljafkljsaf
    akf safksafa
    AAAA ZZZZ afjla fsa kfjsaflaa fa fsa afa faf a
    kafksa f dsa fa afhsaf; akksa; a fsal fsa sa
    a hs;af 'safhjksa
    sa hjasakflfjl sakj sajklf sa
    a jfklajf sasa
    f jklafsa fsaf
    safjksaf safa
    BBBB XXXX fagsa gag aafa
    kljafs;aklfjf
    ajsafjlsafjdsafsa
    klafjsajfklfsa
    .
    .
    .so on


    now I want to make a script which prints the line starting with AAAA ZZZZ and ending with line BBBB XXXX
    there are two words with one tab space I tried the formula at http://www.unix.com/unix-dummies-que...text-file.html
    but it prints all the line except both two line but i really want that it should print the lines between these two lines including both only.

    anybody having idea please share....

    #2
    sed -n -e '/^AAAA ZZZZ/,/^BBBB XXXX/p' /file.txt

    Comment


      #3
      If that's just one space between AAAA and ZZZZ, and BBBB and XXXX:
      Code:
      awk '/AAAA ZZZZ/,/BBBB XXXX/' file.txt
      Simpler, IMO, than using sed. You say "one tab space"; here's "at least one tab or space, any mix accepted":
      Code:
      awk '/AAAA[\t ]+ZZZZ/,/BBBB[\t ]+XXXX/' file.txt
      Regards, John Little
      Regards, John Little

      Comment


        #4
        Originally posted by jlittle View Post
        If that's just one space between AAAA and ZZZZ, and BBBB and XXXX:
        Code:
        awk '/AAAA ZZZZ/,/BBBB XXXX/' file.txt
        Simpler, IMO, than using sed. You say "one tab space"; here's "at least one tab or space, any mix accepted":
        Code:
        awk '/AAAA[\t ]+ZZZZ/,/BBBB[\t ]+XXXX/' file.txt
        Regards, John Little
        Thanks to you all there is another method for others:
        I does not working for me But i found it some where else
        http://www.unix.com/shell-programmin...using-awk.html
        f=0
        while read line
        do
        case $line in
        OUTPUT*) f=1; continue ;;
        END* ) f=0
        esac
        if [ "$f" -eq 1 ]; then
        echo $line
        fi
        done < "file"

        Comment


          #5
          All of these solutions will work. Whichever one you like is the best.

          I don't know where the irrational fear of "sed" comes from. In addtion, I have talked to some kernel developer folks who have an irrational fear of "awk". Both are extremely useful tools. Neither is difficult to use. A few hours spent learning how to use sed or awk, is time well spent.

          Comment

          Working...
          X