Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Match from pattern to end of file in bash

Tags:

regex

bash

eof

I have been trying to figure out how to use grep in a bash script to match from a pattern to the end of the file. The file is not always the same number of lines each time and is not always [A-Za-z0-9]. I'm trying to migrate from a flat-file based catalog to a database.

File excerpt:

First, Last: Doe, John
ID: xxxxxxxx
...

Case Notes:

This "person" does not exist!  
Please do not add him. 
Thanks.

I need to grab everything from Case Notes: to the end of file. I can't seem to find anything to help out there as there isn't an actual EOF character.

Ideas?

like image 375
Colin Avatar asked Sep 19 '11 18:09

Colin


People also ask

What does [- Z $1 mean in bash?

$1 means an input argument and -z means non-defined or empty. You're testing whether an input argument to the script was defined when running the script. Follow this answer to receive notifications.

What does %% mean in bash?

So as far as I can tell, %% doesn't have any special meaning in a bash function name. It would be just like using XX instead. This is despite the definition of a name in the manpage: name A word consisting only of alphanumeric characters and under- scores, and beginning with an alphabetic character or an under- score.

What =~ in bash?

The bash documentation just calls it the =~ operator. Is it only used to compare the right side against the left side? The right side is considered an extended regular expression. If the left side matches, the operator returns 0 , and 1 otherwise.


3 Answers

An awk script might be easier:

awk '/^Case Notes:$/ { matched = 1 } matched'

Or if you don't want to see the Case notes: string itself, reverse it:

awk 'matched; /^Case Notes:$/ { matched = 1 }'
like image 134
ajk Avatar answered Sep 30 '22 20:09

ajk


idiomatic awk solution would be

awk '/^Case Notes:$/,0'

prints from pattern (inclusive) to end of file.

like image 25
karakfa Avatar answered Sep 30 '22 21:09

karakfa


sed -n '/^Case notes:/,$p' file >newfile
like image 22
tripleee Avatar answered Sep 30 '22 22:09

tripleee