Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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.

share|improve this question
add comment

2 Answers

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

share|improve this answer
add comment

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.