Chris
Chris

Reputation: 1249

Remove everything up to but not including pattern

I am doing this and it deletes everything including the pattern. I would like it to not delete the pattern.

sed -i '' 's/.*Pattern//g' file

I would like it to do this

Hello this is a Pattern
Pattern

Thanks

Upvotes: 0

Views: 981

Answers (1)

shellter
shellter

Reputation: 37318

The typical solution is

sed -i '' 's/.*\(Pattern\)/\1/' file
#--------------^^-------^^-^^

where the \(..\) pair is a capture group, and the \1 indicates "replace with what was found in capture group (1)". You can have up to 9 capture groups (maye more in ultra-modern seds).

Note, you can also do the lazymans approach and replace the complete match with your target Pattern, i.e.

sed -i '' 's/.*Pattern/Pattern/' file

edit And thanks to @Beta, removed the redundant g from s/../../g.

IHTH

Upvotes: 3

Related Questions