LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
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 10-25-2007, 11:35 AM   #1
vo1pwf
LQ Newbie
 
Registered: Oct 2007
Posts: 6

Rep: Reputation: 0
Post using sed to replace text on one line in a text file


the end result of what I am trying to get is :::
sed '243s/something/somethingelse/' -i hamlog.txt

this works from the console but tryign to get a bash script working
where by the 243 is entered by a user as well as something and somethngelse
that being $id $whattochange and $changeto

what I have tried so far is...seperated by dashes:::

------------------------------------------------------------
sed '$(echo $id)s/$(echo $whattochange)/$(echo $changeto)/' -i $logfile
------------------------------------------------------------
cat $logfile | sed $id's/"$whattochange"/"$changeto"/g' > test.txt
mv test.txt $logfile
------------------------------------------------------------
cat $logfile | sed '$(echo $id)s/$(echo $whattochange)/$(echo $changeto)/g' > test.txt
------------------------------------------------------------
sed '$(echo "$id/s/$whattochange/$changeto/")' -i $logfile
------------------------------------------------------------
xx="`echo -e "$id \bs"`"
sed '$xx/$whattochange/$changeto/' -i $logfile
------------------------------------------------------------

nothing seems to work, it works perfectly from the command line so I know I cant be quoting or something properly but I just cant seem to hit the nail on the head so to speak

been a week trying to research and learn what I am doing wrong but only ending up in a dead end. Was hopign to learn but doing it the hard way but alas teeth has been grinded hair has been pulled tiem to ask the "uber" geeks for help

many thanks
vo1pwf - geek in training -
 
Old 10-25-2007, 12:13 PM   #2
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 22,965
Blog Entries: 11

Rep: Reputation: 865Reputation: 865Reputation: 865Reputation: 865Reputation: 865Reputation: 865Reputation: 865
Hi,

And welcome to LQ!


What happens if you replace the ' with " in
Code:
sed '$(echo $id)s/$(echo $whattochange)/$(echo $changeto)/' -i $logfile
?


Cheers,
Tink
 
Old 10-25-2007, 12:22 PM   #3
spirit receiver
Member
 
Registered: May 2006
Location: Frankfurt, Germany
Distribution: SUSE 10.2
Posts: 424

Rep: Reputation: 33
No need for "echo", the following should work fine:
Code:
sed "$id s/$whattochange/$changeto/" -i $logfile
Have a look at this.
 
1 members found this post helpful.
Old 10-25-2007, 06:50 PM   #4
vo1pwf
LQ Newbie
 
Registered: Oct 2007
Posts: 6

Original Poster
Rep: Reputation: 0
Smile thanks

thank you thank you thank you thank you thank you thank you thank you thank you thank you thank you thank you thank you thank you thank you

I knew it had to be somethign asoo stupid and simple

the sed "$id s/$whattochange/$changeto/" -i $logfile

worked like a charm

when I used ' it complaint about spaceing tried to do a echo -e \b to remove spacing and still didnt work..... a weeks frustration solved....thank you soooOOOOOOOOoooOOOoooOO much

eventually like to learn more and give back to the community like you just did but until then so continues the learning curve
 
Old 06-24-2009, 07:46 AM   #5
muhsayd
LQ Newbie
 
Registered: Jun 2009
Posts: 1

Rep: Reputation: 0
Question

Hi All,

I'm Using This Command :

sed -e -i "s/${Rule}/${NewRule}/g" excludefile.conf

And it's working fine with me, but sometimes any of my variables contains this character "/" and this causes trouble executing the command, could anyone help skipping this issue ?

thanks in advance.
 
Old 06-24-2009, 07:54 AM   #6
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,458

Rep: Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941
Please, don't resurrect old threads, unrelated to your question. Start your own and you will get a faster response.

You can use any other character as separator. The rule is: the first character after the 's' command is the separator, e.g.
Code:
sed -e -i "s%${Rule}%${NewRule}%g" excludefile.conf
 
1 members found this post helpful.
  


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
Python: find defined text string in a file, and replace the whole line Dark Carnival Programming 6 05-22-2007 06:02 AM
command line edit -- global find/replace on text file w/o going into vi car182 Linux - Newbie 4 05-25-2006 05:42 PM
SED - replace text in file on specific line 3saul Linux - Software 1 03-04-2006 07:01 PM
SED - display text on specific line of text file 3saul Linux - Software 3 12-29-2005 04:32 PM
Replace text of unknown content with other text in file brian0918 Programming 15 07-14-2005 09:22 PM


All times are GMT -5. The time now is 01:28 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration