LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   SED Question (https://www.linuxquestions.org/questions/linux-newbie-8/sed-question-766348/)

ktr944 11-02-2009 06:27 PM

SED Question
 
Hi,

I want to replace a string in a file. The string is:

$var 1>&2 >> /dev/null &
to
#$var 1>&2 >> /dev/null &.

Appreciate any help.

thanx
ktr944

TB0ne 11-02-2009 08:37 PM

Quote:

Originally Posted by ktr944 (Post 3741930)
Hi,

I want to replace a string in a file. The string is:

$var 1>&2 >> /dev/null &
to
#$var 1>&2 >> /dev/null &.

Appreciate any help.

thanx
ktr944

Normally, I'd suggest the man page for sed....but cripes, that's one heck of a man page. There are lots of good articles about sed via Google, and it's very powerful...try:
Code:

sed 's/\$var/\#\$var/g'
Note the '\'....for sed, you'll probably have to 'escape' the special characters (stuff like #!&, etc.), to make sed treat them as just 'regular' characters....the 's' is for substitute, and the 'g' tells sed to replace ALL occurrences. If you omit the 'g', it'll just do the first one.

ghostdog74 11-02-2009 08:39 PM

Code:

awk '$1=="$var"{print "#"$0;next}1' file

scottro11 11-02-2009 09:12 PM

Daniel Robbins has an excellent article on sed. (I've always thought he was one of the best doc writers around, and that that was one of the main reasons for Gentoo's popularity, but anyway...)


http://www.gentoo.org/doc/en/articles/l-sed1.xml

pixellany 11-02-2009 09:49 PM

Looking at the original question---How about opening a text editor and typing in the "#"?

The obvious question for ktr* is: what is the context?---eg how many lines do you want to change and what do they have in common?


All times are GMT -5. The time now is 08:37 PM.