How to remove all lines after a line that contains a specific string in Bash?

I want to remove all lines after a line which contains the string “Date:” in it. Specifically, I am writing the results of git show --name-only to a logging file, but I do not want to log the data after the string that starts with “Date:”

This is the output of git show --name-only:

commit <commit-hash> (HEAD -> <branch-name>, origin/<branch-name>)
Author: bitbucket-pipelines <[email protected]>
Date:   Mon Sep 18 18:26:04 2023 +0000

    [skip ci] Commit message

logs/log.txt

And this is what I want to output:

commit <commit-hash> (HEAD -> <branch-name>, origin/<branch-name>)
Author: bitbucket-pipelines <[email protected]>
Date:   Mon Sep 18 18:26:04 2023 +0000

I tried to remove lines using specific line numbers and writing to a file and then parsing it there, but I am looking for a solution where I can get the output directly as I want it without any extra steps

  • 2

    There’s lots of ways to do this – please edit your question to include what you tried so we can help you with that. Does a solution need to key on the string Date or could you just print the first 3 lines or the lines before the first empty line or something else?

    – 




  • 2

    Do you want to specifically only remove lines after a line contiaining Date: or do you want to print git show a commit without the commit message and file changes? Are you asking XY question? Did you read man git show? In particular there is git show --format=.

    – 




  • I think the question explains very clearly what is wanted, with example input and output.

    – 




You can do this in Awk. This will print all lines, and if the line starts with Date: the script will exit after printing.

git show --name-only | awk '{print} ; /^Date:/ { exit }'

Or Sed (quit when line matches):

git show --name-only | sed '/^Date:/q'

Or if you want to explicitly specify a number of lines:

git show --name-only | awk '{print} ; NR==3 { exit }'
git show --name-only | head -n 3

You can easily do this with only Git by providing the format with which the commit should be printed.

git show -s --pretty='commit %H%d%nAuthor: %aN <%aE>%nDate:   %ad'
  • %H the commit hash
  • %d ref names like --decorate
  • %aN author name (respecting .mailmap)
  • %aE author email (respecting .mailmap)
  • %ad author date

See the PRETTY FORMATS section of git help log for a detailed explanation.


If you don’t want to use Git to control the output format, head -3 is usually a pretty simple way to get only the first three lines of any output.

git show --name-only --no-patch | grep -v '^   .*' | grep -v '^$'

--no-patch to remove file-change lines (stat). Inverted greps to remove empty and indented lines, which is the message part.

A bit overkill for git show but can also be used for git log.

Don’t forget the good old grep! Here I use regex to match and print lines starting with commit, Author:, and Date:.

git show --name-only | grep '^\(commit\|Author:\|Date:\)'

Leave a Comment