24

I have a file named .ignore. In need to replace the projdir. For example:

ignore \..*
ignore README
projdir Snake

I need to replace Snake with, for example, "PacMan". I read the man page, but I have no idea what to do.

5 Answers 5

29

Search for a line that starts with projdir, and replace the whole line with a new one:

sed -i 's/^projdir .*$/projdir PacMan/' .ignore

^ and $ are beginning/end-of-line markers, so the pattern will match the whole line; .* matches anything. The -i tells sed to write the changes directly to .ignore, instead of just outputting them

3
  • 3
    If you use the long option for -i, which is --in-place then the example will be more self-documenting. Apr 22, 2020 at 8:45
  • this doesn't really address the question in a general term, only in the specific example. to be fair, the example was also not general enough... check my post for a more complete (and simpler) answer! and feel free to edit your answer accordingly. 😘
    – cregox
    Jul 19, 2020 at 14:43
  • @cregox, welcome to SE. Posting just links and trying to evoke curiosity is not the style used here.
    – Timo
    Oct 23, 2020 at 5:53
7

One approach is to rewrite the entire line, using backreferences for the parts you want to keep:

sed -e 's/^\( *projdir  *\)[^ ]*\(.*\)*$/\1PacMan\2/'

Another approach is to rewrite that part of the line, but only if some other part matches:

sed -e '/^ *projdir / s/ [^# ]/ PacMan/'

Both examples rewrite the second whitespace-delimited word on lines where the first word is projdir.

5

Although this is an old post and it seems to be solved for you by the accepted answer, the actual question has not been answered. So for completeness and to help others:

Here the answer that actually matches for "Snake" and not for lines startting with "projdir"...

sed -r 's/(^.*)Snake/\1PacMan' .ignore

This replaces everything from the start of the line including "Snake" with everything before "Snake" + "PacMan". \1 stands for what is matched inside (). Everything after "Snake" remains untouched.

0
2
sed -i 's:^projdir.*$:projdir PacMan:g' .ignore

^projdir is to find the line that starts with string projdir. The .*$ there stands for the string after projdir in the line the same line. The string projdir PacMan is the string with which we are replacing. g is for global - to replace all such lines starting with projdir. .ignore is the name of the file

3
  • 1
    Can you please extend your answer and clarify what is what? Jan 27, 2017 at 10:58
  • Answers are much more useful if they include explanations for how and why a command or piece of code works. Jan 27, 2017 at 11:12
  • I hope this is more useful after edit
    – Tinu
    Apr 1, 2017 at 7:22
-1

to answer the question in the title "how to replace text after a specific word using sed":

sed -i '/projdir$/!{s/Snake/PacMan/}' .ignore

makes wonders, as it doesn't expect the Snake will be in the same line as projdir! we all know 🐍 is never that easy to find.

-i will consolidate the change into the file

$ is to match Snake even in the same line

wished i knew or remembered what exactly ! and {} are for.

mostly learned from this amazing resource:

https://linuxhint.com/50_sed_command_examples/#s43

The basic uses of sed command are explained in this tutorial by using 50 unique examples.

5
  • anyone mind to explain why this was not a good answer? what have i missed?
    – cregox
    Jul 19, 2020 at 14:40
  • 1
    You missed the solution, does not work.
    – Timo
    Oct 23, 2020 at 5:57
  • @timo thanks! indeed, it needs to be fixed...
    – cregox
    Oct 25, 2020 at 19:37
  • 2
    Actually this solution has been very educative, and I'm using a variation of it in an actual issue. Mar 3, 2021 at 18:13
  • @David thanks for the feedback! i'm glad the fix worked. 🥰
    – cregox
    Mar 3, 2021 at 20:00

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .