linux - SED: How can I print every line after first instance of string using Sed?

06
2013-09
  • barrrista

    I have a file with a similar format...

    16:28 asdfasdf
    16:29 4398upte
    16:30 34liuthr
    16:31 34tertio
    

    How can I use SED to print out every line including and after the line with "16:30"?

    The result would be...

    16:30 34liuthr
    16:31 34tertio
    

    Right now, I am using sed as follows, but I have to manually find the first line's line number e.g. "562697":

    sed -n '562697,$p'
    
  • Answers
  • Nicole Hamilton

    Addresses in sed can be either line numbers or patterns. Try this:

    sed -n '/16:30/,$p'
    

    If the pattern contains a /, you can escape it with a \. For example, to search for 16/30 instead of 16:30, try this:

    sed -n '/16\/30/,$p'
    
  • choroba

    Use a regular expression in the address:

    sed -n '/^16:30/,$p'
    

    or

    sed '/^16:30/,$!d'
    

  • Related Question

    SED: how to pull out version string
  • WilliamKF

    I would like to use sed to pull out the version number from the command:

    svnversion --version
    

    Which gives output like:

    svnversion, version 1.6.2 (r37639)
       compiled May 10 2009, 12:41:21
    
    Copyright (C) 2000-2009 CollabNet.
    Subversion is open source software, see http://subversion.tigris.org/
    This product includes software developed by CollabNet (http://www.Collab.Net/).
    

    And after processing with sed, I'd like just:

    1.6.2
    

    So far, I have this monstrosity (due to my ignorance):

    svnversion --version | sed s/[\wa-zA-Z//\(\):,]*//g | sed 's/[ ]//' | sed 's/[ ]//' | sed 's/[ ][0-9 ./n/-]*//'
    

    I'm sure there is a simple elegant solution that an expert can provide easily.


  • Related Answers
  • Darren Hall

    I know this isn't using sed, but based upon your output this is easier.

    svnversion --version | head -1 | awk '{print $3}'
    

    If you have perl available...

    svnversion --version | perl -lne 'print $1 if /version (\d+.\d+.\d+)/'
    
  • Dennis Williamson

    Try this:

    svnversion --version | sed -n '/version/ s/.*version \([0-9]\+\.[0-9]\+\.[0-9]\+\) .*/\1/p'
    

    It says:

    • -n --- Don't automatically print output.
    • /version/ --- On lines that include the string "version",
    • s/ - substitute for what's
    • .*version and .* --- between [a sequence of zero or more of any character, the string "version", a space] and [a space, a sequence of zero or more of any character],
    • \(...\) --- capturing
    • [0-9]\+ --- a sequence of one or more digits
    • \. --- followed by a literal period
    • then more digits and periods,
    • /\1/ --- what was captured in the first (and only, in this case) set of parentheses
    • p --- and explicitly print it.

    Because automatic printing is suppressed and only the line that includes "version" is selected, the other lines are ignored.

  • yanchenko

    One more way to do it w/o sed: svnversion --version | head -1 | cut -d" " -f3