1

I remembered how to do this and even learned it from Stack Exchange, but forgot how I googled it so now I can't find it. The answers I am finding now are not what I'm looking for. Here is an example text:

The quick
brown fox
jumps over
the lazy
dog

I want sed to remove everything before and when it says "jum" so the output would be something like this:

ps over
the lazy
dog

How do I do this?

3
  • 1
    What if there are more than one jum in the file? Should we keep the first? The last? Commented Mar 22, 2023 at 12:26
  • @terdon That would be too bad, in that case I would delete more than just the "jum". Commented Mar 23, 2023 at 0:39
  • But how do you want to handle cases with more than one? Commented Mar 23, 2023 at 9:16

3 Answers 3

2
sed -ne '1,/^jum/{s/^jum//p;d;}' -e p

As @Sundeep states in comment, if you have GNU sed, use preferably sed -ne '0,/^jum/{s/^jum//p;d;}' -e p. This would work even if jum on the first line. which is not the case with 1,/^jum/.

Here is what it does:

  • sed -n: don't print anything unless told to
  • 1,/^jum/: for lines between the 1st one and the one that starts with jum inclusive:
    • s/^jum//p: if you can remove "jum" at start of line, print the result
    • d: and proceed with next line
  • -e p: all other lines, just print them.
3
  • 1
    Won't work if the first line starts with jum. With GNU sed, you can use 0,/pattern/ to match the first line as well. Also, you can use s///p to avoid repeating the pattern. Commented Mar 22, 2023 at 9:17
  • 1
    @Sundeep you're right twice. I'll edit the answer. I just tend not to use s///. sed script are cryptic enough without this additional magic. I like your awk answer and was tempted to use awk too (although I probably would not have written a solution as clever and clear as yours), but OP asked for sed. Anyway, you're a master at both. Chapeau ! Commented Mar 22, 2023 at 10:57
  • Why are we assuming that jump will be the first word of a line? Commented Mar 22, 2023 at 12:25
1

Using GNU sed, and assuming your file is small enough to fit in memory, you can do this:

$ sed -z 's/.*jum//' file 
ps over
the lazy
dog

The -z tells sed to treat its input as NUL-separated, meaning that it expects a NUL (\0) to signidfy the end of each line. Since your file will have no NUL characters, this has the effect of sed treating the entire file as a single line so we can do the whole thing in a single s/// operation.

Note that this will find the last jum in the file and delete everything before that. If there are other occurrences of jum earlier, those will be removed.

0

With awk:

$ awk 'sub(/^jum/, ""){f=1} f' ip.txt
ps over
the lazy
dog

If the substitution succeeds, set a flag to print the lines.

2
  • 1
    Why are we assuming that jump will be the first word of a line? Commented Mar 22, 2023 at 12:25
  • Hmm, good point. Perhaps the OP can clarify, along with multiple match case. Commented Mar 22, 2023 at 12:56

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.