How to use sed to return something from first line which matches and quit early?

My specific use case was to find out via 'git log' on a git-p4 imported tree which perforce branch was used for the last commit. git log (when called without -n will log every commit that every happened (hundreds of thousands for me)).

We can't know a-priori what value to give git for '-n.' After posting this, I found my solution which was:

git log | sed -n '0,/.*\[git-p4:.*\/\/depot\/blah\/\([^\/]*\)\/.*/s//\1/p; /\[git-p4/ q' 

I'd still like to know how to do this with a non '/' separator, and without having to specify the 'git-p4' part twice, once for the extraction and once for the quit. There has got to be a way to combine both on the same line...


Sed usually has an option to specify more than one pattern to execute (IIRC, it's the -e option). That way, you can specify a second pattern that quits after the first line.

Another approach is to use sed to extract the first line (sed '1q'), then pipe that to a second sed command (what you show above).


The "print and quit" trick works, if you also put the substitution command into the block (instead of only putting the print and quit there).

Try:

sed -n '/.*\(something\).*/{s//\1/p;q}'

Read this like: Match for the pattern given between the slashes. If this pattern is found, execute the actions specified in the block: Replace, print and exit. Typically, match- and substitution-patterns are equal, but this is not required, so the 's' command could also contain a different RE.

Actually, this is quite similar to what ghostdog74 answered for gawk.


use gawk

gawk '/MATCH/{
            print "do something with "$0
            exit 
}' file

Tags:

Sed