Skip to content

Instantly share code, notes, and snippets.

@kholis
Created January 26, 2026 13:25
Show Gist options
  • Select an option

  • Save kholis/9e27a50c6816a7f8fae7b952df6969f6 to your computer and use it in GitHub Desktop.

Select an option

Save kholis/9e27a50c6816a7f8fae7b952df6969f6 to your computer and use it in GitHub Desktop.
Grep java log multiline started with timestamp "YYYY-MM-DD HH:MM:SS"
#!/usr/bin/bash
# by chatgpt
if [ "$1" == "" ]; then
echo 'Example: TS="2026-01-25 10:42:" grep-multiline.sh /path/to/file.log'
exit 1
fi
awk -v ts="$TS" '
/^[0-9]{4}-[0-9]{2}-[0-9]{2}/ {
if (buf ~ "^" ts) print buf ""
buf = $0
next
}
{ buf = buf "\n" $0 }
END { if (buf ~ "^" ts) print buf }
' $1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment