bash 在bash中从模式匹配到文件结尾

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7475309/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 00:49:02  来源:igfitidea点击:

Match from pattern to end of file in bash

regexbasheof

提问by Colin

I have been trying to figure out how to use grepin 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.

我一直在试图弄清楚如何grep在 bash 脚本中使用从模式到文件末尾的匹配。该文件每次的行数并不总是相同,也不总是[A-Za-z0-9]. 我正在尝试从基于平面文件的目录迁移到数据库。

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.

我需要抓取从Case Notes:文件末尾的所有内容。我似乎无法找到任何帮助,因为没有实际的 EOF 字符。

Ideas?

想法?

回答by ajk

An awkscript might be easier:

一个awk脚本可能会更容易:

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

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

或者,如果您不想看到Case notes:字符串本身,请将其反转:

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

回答by karakfa

idiomatic awksolution would be

惯用的awk解决方案是

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

prints from pattern (inclusive) to end of file.

从模式(包括)打印到文件末尾。

回答by tripleee

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

回答by tripleee

Few years late but you could use

晚了几年,但你可以使用

more +"Case notes:" file

回答by bash-o-logist

Pure bashcan do it

bash能做到

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