iBug
iBug

Reputation: 37317

Delete from next line of matching to end of file with sed

I want to use sed to match a line, preserve the matching line and delete all lines under. Or in other words, I want to extract the text from beginning

Sample input:

1
2
3
4
5

Sample match RegEx: /^3$/

Sample output:

1
2
3

I tried this but it doesn't work

seq 5 | sed '/^3$/{n;,$d}'

I also tried this but it will delete the matching line, which I want to preserve.

seq 5 | sed '/^3$/,$d'

Any ideas with sed (I can't use AWK for miscellaneous reasons)?

Upvotes: 0

Views: 1369

Answers (2)

agc
agc

Reputation: 8446

Use the quit command:

 seq 5 | sed '/^3$/q'

Upvotes: 2

iBug
iBug

Reputation: 37317

Think the other way round: Print from beginning to matching line, which does the equivalent of deleting lines under.

seq 5 | sed -n '1,/^3$/p'

Output:

1
2
3

Upvotes: 1

Related Questions