Page 2 of 2 FirstFirst 12
Results 11 to 13 of 13

Thread: Grepping multiple lines at once?

  1. #11
    Join Date
    May 2006
    Beans
    1,790

    Re: Grepping multiple lines at once?

    Quote Originally Posted by MadCow108 View Post
    edit doesn't work matches 1 2 2 3

    ugly but it uses only grep
    grep -A 3 -E "^1$" test.txt | grep -E "^2$" -B 1 -A 1 | grep -E "^3$" -B 2
    count by grepping again: grep "\-\-" | wc -l (+ 1)

    non-regex python:
    Code:
    import io
    
    file = io.open("test.txt")
    c = 0 
    for l in file:
      if l == "1\n" and file.next() == "2\n" and file.next() == "3\n":
        c += 1
    
    print(c)
    This doesn't match

    1
    1
    2
    3

  2. #12
    Join Date
    Sep 2007
    Location
    England
    Beans
    1,103

    Re: Grepping multiple lines at once?

    Quote Originally Posted by Arndt View Post
    I'm not sure that grep can handle a line millions of characters long. Maybe it can, though.

    Personally, I would write a small program for this purpose.
    Grep (or any of the standard text stream utilities) can handle any sized input
    Code:
    while [ true ]; do CY=$(date +%y); CM=$(date +%m); if [ -n "$PY" ] && [ -n "$PM" ]; then echo "Ubuntu ${CY}.${CM} is the worst release ever"; echo "I'm going back to ${PY}.${PM}"; fi; PY="$CY"; PM="$CM"; sleep 182d; done

  3. #13
    Join Date
    Nov 2005
    Location
    Leeds, UK
    Beans
    1,634
    Distro
    Ubuntu Development Release

    Re: Grepping multiple lines at once?

    Cheers for the python stuff. It would have worked for my problem when it matched 21 2 3 because what I didn't really tell you was that every number was a decimal between 0 and 1. But in any case, the reason I was doing that was because I was trying to find the periodicity of the rand() c function which further research tells me is 2^32 ~= 4 billion and I couldn't create a file with 9 billion values (to get the period twice) without my hard drive or grep complaining so either way, it was impractical.

    Python would have been even worse really but I guess if I left it long enough, it would have worked. Anyway, those snippets are quite useful for other things so I'll keep this thread in mind.

    Thanks everyone!

Page 2 of 2 FirstFirst 12

Bookmarks

Posting Permissions

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