开发者

grep all lines from start of file to line containing a string

开发者 https://www.devze.com 2023-02-11 20:41 出处:网络
If I have input file开发者_运维问答 containing statementes asda rertte something nothing here I want to grep / extract (without using awk) every line from starting till I get the string \"someth

If I have input file开发者_运维问答 containing

statementes
asda
rertte 
something 
nothing here 

I want to grep / extract (without using awk) every line from starting till I get the string "something". How can I do this? grep -B does not work since it needs the exact number of lines.

Desired output:

statementes
asda
rertte 
something 


it's not completely robust, but sure -B works... just make the -B count huge:

grep -B `wc -l <filename>` -e 'something' <filename>


You could use a bash while loop and exit early when you hit the string:

$ cat file | while read line; do
> echo $line
> if echo $line | grep -q something; then
> exit 0
> fi
> done


head -n `grep -n -e 'something' <filename> | cut -d: -f1` <filename>
0

精彩评论

暂无评论...
验证码 换一张
取 消