SkypeMeSM
SkypeMeSM

Reputation: 3287

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

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 

Upvotes: 1

Views: 1637

Answers (3)

jmilloy
jmilloy

Reputation: 8365

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

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

Upvotes: 2

jmilloy
jmilloy

Reputation: 8365

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

Upvotes: 0

Jared Casper
Jared Casper

Reputation: 168

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

Upvotes: 0

Related Questions