LinuxQuestions.org
Help answer threads with 0 replies.
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 01-05-2011, 12:14 AM   #1
balakodoth
Member
 
Registered: Dec 2010
Posts: 40

Rep: Reputation: 0
replacing a line with spaces


I have a line like

port = 2566

I want to replace it as

port = 8080

does anyone knows how to do this in shell script. when i tried to do this with sed
sed -i 's/port=.*/port='$3'/' /root/$2
it is not recognizing spaces , it works only if line is port=2566 .

any idea on how to do this?
 
Old 01-05-2011, 12:31 AM   #2
crts
Senior Member
 
Registered: Jan 2010
Posts: 1,608

Rep: Reputation: 449Reputation: 449Reputation: 449Reputation: 449Reputation: 449
Quote:
Originally Posted by balakodoth View Post
I have a line like

port = 2566

I want to replace it as

port = 8080

does anyone knows how to do this in shell script. when i tried to do this with sed
sed -i 's/port=.*/port='$3'/' /root/$2
it is not recognizing spaces , it works only if line is port=2566 .

any idea on how to do this?
If your line has spaces then your regex needs spaces, too.
Code:
sed -i 's/port = .*/port = '$3'/' file
 
Old 01-05-2011, 12:46 AM   #3
balakodoth
Member
 
Registered: Dec 2010
Posts: 40

Original Poster
Rep: Reputation: 0
thanks... I tried giving space but it dosent work.. :-(
 
Old 01-05-2011, 12:49 AM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,499

Rep: Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867
Maybe it is a tab or a mix, try using character list instead:
Code:
sed -i "s/port[[:space:]]*=[[:space:]]*.*/port = $3/" file
The use of double quotes will help you use the variable you are passing in.
 
1 members found this post helpful.
Old 01-05-2011, 01:04 AM   #5
munavar
LQ Newbie
 
Registered: Dec 2010
Posts: 7

Rep: Reputation: 0
You can use this

sed -i 's/2566/8080/' FilePath
 
Old 01-05-2011, 01:43 AM   #6
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,499

Rep: Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867
@munavar - only if we assume that it exists nowhere else in the file
 
Old 01-05-2011, 07:58 AM   #7
rhoekstra
Member
 
Registered: Aug 2004
Location: The Netherlands
Distribution: RedHat 2, 3, 4, 5, Fedora, SuSE, Gentoo
Posts: 372

Rep: Reputation: 42
Quote:
sed -i '/^port/s/2566/8080/' FilePath
using the '/^port/' in front of the 's/' statement only applies the change to lines that start with 'port'.
 
  


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
Replacing Tabs (^T) with spaces.... visitnag Linux - Newbie 3 05-04-2008 03:30 AM
Replacing spaces with a : xjlittle Programming 1 02-12-2007 08:42 AM
replacing all spaces in filenames of a directory merlin23 Linux - Newbie 1 01-11-2005 07:52 AM
Spaces on command line odd Linux - Software 2 05-22-2004 08:17 AM
mp3 files - replacing blank spaces with _ script? mymojo Linux - Newbie 4 12-08-2003 03:33 AM


All times are GMT -5. The time now is 11:45 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