Created
October 29, 2015 00:31
-
-
Save purplexa/7630af3d3734e1c3295d to your computer and use it in GitHub Desktop.
Use grep to print all lines up to a match
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function grep_from_start () { | |
| if [ -z $1 -o -z $2 ]; then | |
| echo "Usage:\ngrep_from_start 'regex' file1 [file2 file3 ...]" | |
| exit 2 | |
| fi | |
| for i in $(grep -n -m1 $1 "${@:2}"); do | |
| filename="$(cut -d':' -f1 <<< $i)" | |
| lnum="$(cut -d':' -f2 <<< $i)" | |
| omit_lines="$(( $(wc -l $filename | awk '{print $1}') - $lnum + 1 ))" | |
| echo "\n--\n$filename:" | |
| head -n -$omit_lines $filename | |
| done | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment