LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 11-02-2009, 07:27 PM   #1
ktr944
LQ Newbie
 
Registered: Nov 2009
Posts: 2

Rep: Reputation: 0
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
 
Old 11-02-2009, 09:37 PM   #2
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 17,919

Rep: Reputation: 3689Reputation: 3689Reputation: 3689Reputation: 3689Reputation: 3689Reputation: 3689Reputation: 3689Reputation: 3689Reputation: 3689Reputation: 3689Reputation: 3689
Quote:
Originally Posted by ktr944 View Post
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.
 
Old 11-02-2009, 09:39 PM   #3
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Code:
awk '$1=="$var"{print "#"$0;next}1' file
 
Old 11-02-2009, 10:12 PM   #4
scottro11
Member
 
Registered: Jun 2009
Location: NYC
Posts: 260

Rep: Reputation: 58
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
 
Old 11-02-2009, 10:49 PM   #5
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738
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?
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
sed question? slack66 Linux - Newbie 7 07-26-2006 07:47 AM
[sed] "Advanced" sed question(s) G00fy Programming 2 03-20-2006 01:34 AM
sed question ckoniecny Linux - General 3 11-11-2005 10:31 AM
sed question from Ch 6.12 na5m Linux From Scratch 1 02-02-2005 04:36 PM
sed question sphynx Programming 6 04-28-2004 07:25 PM


All times are GMT -5. The time now is 02:03 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration