简体   繁体   中英

Match from pattern to end of file in bash

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?

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 }'

idiomatic awk solution would be

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

prints from pattern (inclusive) to end of file.

sed -n '/^Case notes:/,$p' file >newfile

几年后你可以使用

more +"Case notes:" file

Pure bash can do it

declare -i tf=0
while read -r line
do
   case "$line" in
     *"Case notes"* ) tf=1;
   esac
   [[ $tf -eq 1 ]] && echo "$line"
done < file

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM