LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 10-05-2012, 03:33 PM   #1
tonypal12
LQ Newbie
 
Registered: Oct 2012
Posts: 1

Rep: Reputation: Disabled
Question replacing situation with 'sed'


I'm trying to do a string replace and want to do the following

A file (test.fil) containing the following amongst other lines

myparam = 777777777

and to replace the pattern "myparam" in the above file and anything after (as the blanks before the = may vary and the numbers after the = sign may vary)

so something like

sed -i s/myparam <and everything after that> /myparam = 1234567/g test.fil

note: the search pattern always begins with myparam

so the result would be

myparam = 1234567

P.S: Remember we cannot do sed -i s/myparam = [0-9]*/myparam = 1234567/g test.fil. The reason being there may or maynot be a blank before or after the '=' character.

Last edited by tonypal12; 10-05-2012 at 03:35 PM.
 
Old 10-05-2012, 04:20 PM   #2
custangro
Senior Member
 
Registered: Nov 2006
Location: California
Distribution: Fedora , CentOS , RHEL
Posts: 1,979
Blog Entries: 1

Rep: Reputation: 209Reputation: 209Reputation: 209
This should work...


EXAMPLE...

Code:
[root@ /tmp]# cat ,mypa 
myparam = 1234567
myparam= 1234567
myparam      = 1234567
myparam	= 1234567
myparam				= 1234567

[root@ /tmp]# sed -e 's/^myparam/other/g' ,mypa 
other = 1234567
other= 1234567
other      = 1234567
other	= 1234567
other				= 1234567
 
Old 10-07-2012, 04:15 PM   #3
frob23
Senior Member
 
Registered: Jan 2004
Location: Roughly 29.467N / 81.206W
Distribution: OpenBSD, Debian, FreeBSD
Posts: 1,450

Rep: Reputation: 48
This sounds an awfully lot like a homework question.

If it's not, I still need clarification: we're not replacing the "key" (myparam in your example) but we are replacing everything after it.

So:
Code:
junk     = 435323245
foo = 34
junk = dsd32324
bar    = 5643224
Running the command with "junk 23skidoo" would change the file to:
Code:
junk = 23skiddo
foo = 34
junk = 23skiddo
bar    = 5643224
Am I right?
 
Old 10-07-2012, 04:57 PM   #4
frob23
Senior Member
 
Registered: Jan 2004
Location: Roughly 29.467N / 81.206W
Distribution: OpenBSD, Debian, FreeBSD
Posts: 1,450

Rep: Reputation: 48
I am going to go against my gut feeling and assume this isn't homework. Besides, it's a pretty simple thing.

I did this by putting the sed command in a simple script (called switch.sh on my system). You could do it as a command by replacing the variables with the values you want in the sed command from the script. But I assume you'll run this command more than once so having a simplified interface is beneficial.

switch.sh
Code:
#!/bin/sh
PARAM="$1"
VALUE="$2"

sed "s/${PARAM}[        ]*=.*/${PARAM} = ${VALUE}/"
Note: The white-space between the []s is a single space and a tab.

We would use this like follows:
Code:
cat test.fil | switch.sh foo 149234
It outputs the new file to standard out where it can be captured. This handles the following file correctly:
Code:
foo   = bar
foo=454355645
food=454355645
foo df=454355645
bash = 12343
foo =    dssadfdsa
bash      	= nice
foo		=	not
becomes

Code:
foo = 149234
foo = 149234
food=454355645
foo df=454355645
bash = 12343
foo = 149234
bash      	= nice
foo = 149234
This correctly ignores the key "food" and "foo df". You can change the value of "foo df" by putting the argument to the script in quotes (switch "foo df" 67) but let's hope that isn't a problem you really have to face.
 
  


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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
sed not replacing properly zizou86 Programming 1 01-18-2010 12:39 PM
Replacing Characters with sed zokken Programming 9 12-02-2009 07:34 PM
replacing with sed dipuasks Linux - General 13 12-31-2008 11:24 AM
replacing sed -s option i.you Linux - Software 3 10-28-2007 12:08 PM
Help with sed - replacing strings thulley Linux - Software 4 08-22-2006 10:07 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 07:35 AM.

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
Open Source Consulting | Domain Registration