sed: return last occurrence match until end of file

前端 未结 6 1435
名媛妹妹
名媛妹妹 2020-12-10 15:29

Using sed, how do I return the last occurance of a match until the End Of File? (FYI this has been simplified)

So far I\'ve tried:

sed -n \'/ Statist         


        
相关标签:
6条回答
  • 2020-12-10 16:05

    This might also work, slightly more simple version of the sed solution given by the others above:

    sed -n 'H; /^Statistics |/h; ${g;p;}' logfile.log
    

    Output:

    Statistics |
       Stuff
    Error: error type one
    Error: error type two
    
    0 讨论(0)
  • 2020-12-10 16:12

    If you're happy with an awk solution, this kinda works (apart from getting an extra blank line):

    awk '/^Statistics/ { buf = "" } { buf = buf "\n" $0 } END { print buf }' input.txt
    
    0 讨论(0)
  • 2020-12-10 16:18

    If you have tac available:

    tac INPUTFILE | sed '/^Statistics |/q' | tac
    
    0 讨论(0)
  • 2020-12-10 16:19

    Your example script has a space before Statistics but your sample data doesn't seem to. This has a regex which assumes Statistics is at beginning of line; tweak if that's incorrect.

    sed -n '/^Statistics |/h;/^Statistics |/!H;$!b;x;p'
    

    When you see Statistics, replace the hold space with the current line (h). Otherwise, append to the hold space (H). If we are not at the end of file, stop here (b). At end of file, print out the hold space (x retrieve contents of hold space; p print).

    In a sed script, commands are optionally prefixed by an "address". Most commonly this is a regex, but it can also be a line number. The address /^Statistics |/ selects all lines matching the regular expression; /^Statistics |/! selects lines not matching the regular expression; and $! matches all lines except the last line in the file. Commands with no explicit address are executed for all input lines.

    Edit Explain the script in some more detail, and add the following.

    Note that if you need to pass this to a remote host using ssh, you will need additional levels of quoting. One possible workaround if it gets too complex is to store this script on the remote host, and just ssh remotehost path/to/script. Another possible workaround is to change the addressing expressions so that they don't contain any exclamation marks (these are problematic on the command line e.g. in Bash).

    sed -n '/^Statistics |/{h;b};H;${x;p}'
    

    This is somewhat simpler, too!

    A third possible workaround, if your ssh pipeline's stdin is not tied up for other things, is to pipe in the script from your local host.

    echo '/^Statistics |/h;/^Statistics |/!H;$!b;x;p' |
    ssh remotehost sed -n -f - file
    
    0 讨论(0)
  • 2020-12-10 16:20

    This might work for you:

    sed '/Statistics/h;//!H;$!d;x' file
    Statistics |
       Stuff
    Error: error type one
    Error: error type two
    
    0 讨论(0)
  • 2020-12-10 16:23
    sed ':a;N;$!ba;s/.*Statistics/Statistics/g' INPUTFILE
    

    should work (GNU sed 4.2.1).

    It reads the whole file to one string, then replaces everything from the start to the last Statistics (word included) with Statistics, and prints what's remaining.

    HTH

    0 讨论(0)
提交回复
热议问题