Skip to content

Instantly share code, notes, and snippets.

@tsleyson
Created August 10, 2016 22:35
Show Gist options
  • Save tsleyson/f1961129d850272175f898db48e5db09 to your computer and use it in GitHub Desktop.
Save tsleyson/f1961129d850272175f898db48e5db09 to your computer and use it in GitHub Desktop.
How to get all the lines of a file starting from the first one that matches a regex, up to the end
tail -n $(grep -n -m 1 "regex to match" "source file" | cut -d : -f1) "source file" > "dest file"
# The above throws away the first line that matches the regex. This will keep that line:
tail -n $(( $(grep -n -m 1 "regex to match" "source file" | cut -d : -f1) - 1)) "source file" > "dest file"
@tsleyson
Copy link
Author

Say we have this file:

line one
line two
yay, line three!
line four
yay, line five!
line six

Run this command:

tail -n $(grep -n -m 1 "yay" "source file" | cut -d : -f1) "source file"

to get this output:

line four
yay, line five!
line six

Run the second version to get this output:

yay, line three!
line four
yay, line five!
line six

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment