LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 07-14-2008, 01:44 AM   #1
paragkalra
Member
 
Registered: Sep 2006
Location: Nagpur, Maharashtra, India
Distribution: Debian, Ubuntu, Redhat, Fedora, SLES, OpenSUSE, FreeBSD, Mac OS X
Posts: 221

Rep: Reputation: 31
Simple "sed" query


Hi Folks,

I have just started learning "sed" and I am a complete newbie. I have simple task to be accomplished.

I have a VMware configuration file in which there is following line:

ide1:0.fileName = "D:\isos\test\test1234-abcd.iso"

In my scenario the name of the iso keeps on changing like test1234-abcd.iso, test12346789-abcd.iso, test1234-abcdefg.iso, test123xyz.iso bla bla bla. But in all cases "test" will remain as it is and only the contents between "test" & ".iso" will change

So I want to write a generalized sed script to replace the previous iso name with the new iso name. For example to change
Quote:
ide1:0.fileName = "D:\isos\test\test1234-abcd.iso"
to
Quote:
ide1:0.fileName = "D:\isos\test\test12ab.iso"
I used following command:
Quote:
sed -e 's/test1234-abcd.iso/test12ab.iso/' test.vmx
It works and the changed contents are displayed on the output screen. But they are not getting written on the file itself.

Why so?

Second question: How can I use wild charchters to make it generalized ?for eg:
Quote:
sed -e 's/test*iso/test-abc-xyz.iso/' test.vmx
The above command is not working :-(
 
Old 07-14-2008, 02:08 AM   #2
gaynut
LQ Newbie
 
Registered: Jan 2008
Posts: 27

Rep: Reputation: 15
For ur first question,

sed doesn't modify the contents of original file.
it needs to be redirected to another file.
say,
u can achieve modifying the contents of ur test.vmx file thro' 2 steps

(i) sed -e 's/test*iso/test-abc-xyz.iso/' test.vmx > test.vmx.swp
(ii) mv test.vmx.swp test.vmx


for ur second question,
The regular expression is not used properly.

sed 's/test[0-9]*\-[a-z]*\.iso/linux/g' < filename >filename1

would now replace ur filename[eg:test1234-abc.iso] with "linux" everywhere.
U can try with ur filename instead.
 
Old 07-14-2008, 02:36 AM   #3
Mr. C.
Senior Member
 
Registered: Jun 2008
Posts: 2,529

Rep: Reputation: 59
1) Actually, newer sed can edit-in-place. See the -i[SUFFIX] option.

2) * in regular expressions means 0 or more of the preceeding character. So you need to indicate what character you want to quantify. In your case, you want any character, and this is dot (.). So, you could use:

sed -i.bak 's/test.*\.iso/test.NEW.iso/' test.vmx

Last edited by Mr. C.; 07-14-2008 at 02:58 AM.
 
Old 07-14-2008, 02:43 AM   #4
paragkalra
Member
 
Registered: Sep 2006
Location: Nagpur, Maharashtra, India
Distribution: Debian, Ubuntu, Redhat, Fedora, SLES, OpenSUSE, FreeBSD, Mac OS X
Posts: 221

Original Poster
Rep: Reputation: 31
Thanks gaynut .....your command worked like a charm....also thanks to Mr. C. for your suggestion....
 
Old 07-14-2008, 02:52 AM   #5
Disillusionist
Senior Member
 
Registered: Aug 2004
Location: England
Distribution: Ubuntu
Posts: 1,013

Rep: Reputation: 83
You could have included all valid characters that you want to test for, which is what gaynut was trying to do, however in this case.

Assuming valid entries are:

test123-abc.iso
test12abc.iso
test12a4c.iso
test.iso

gaynut's suggestion would only have found the first entry.

You could use:
sed -i 's/test[-0-9a-z].iso/test0123-new.iso/' test.vmx

Or to create a backup copy:
sed -i.bak 's/test[-0-9a-z].iso/test0123-new.iso/' test.vmx

Or test for any number of any character as per Mr. C.

The problem with Mr. C.'s suggestion is that it is too inclusive, it would also match:

testfile=alternative.iso
test with new iso

Last edited by Disillusionist; 07-14-2008 at 03:04 AM.
 
Old 07-14-2008, 04:02 AM   #6
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,695
Blog Entries: 5

Rep: Reputation: 239Reputation: 239Reputation: 239
sometimes, the simplest things can be done without too much regular expressions
Code:
awk 'BEGIN{OFS=FS="\\"}/^ide/{ $NF="test12ab.iso"}1' file
 
Old 07-14-2008, 12:04 PM   #7
Mr. C.
Senior Member
 
Registered: Jun 2008
Posts: 2,529

Rep: Reputation: 59
FS is a regular expression, degenerate or not! :-)
 
Old 07-14-2008, 12:11 PM   #8
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,360

Rep: Reputation: 1909Reputation: 1909Reputation: 1909Reputation: 1909Reputation: 1909Reputation: 1909Reputation: 1909Reputation: 1909Reputation: 1909Reputation: 1909Reputation: 1909
Quote:
Originally Posted by Mr. C. View Post
FS is a regular expression, degenerate or not! :-)
Who is the degenerate? The regular expression or ghostdog74?

I like the awk solution. I'd just add the double quotes at the end of the line, as in the original file:
Code:
awk 'BEGIN{OFS=FS="\\"}/^ide/{ $NF="test12ab.iso\""}1' file
 
Old 07-14-2008, 12:35 PM   #9
Mr. C.
Senior Member
 
Registered: Jun 2008
Posts: 2,529

Rep: Reputation: 59
Well, me of course! Bah hah ha hah!
 
Old 07-14-2008, 01:26 PM   #10
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 22,950
Blog Entries: 11

Rep: Reputation: 860Reputation: 860Reputation: 860Reputation: 860Reputation: 860Reputation: 860Reputation: 860
Quote:
Originally Posted by Mr. C. View Post
Well, me of course! Bah hah ha hah!
Gaynuts spelling?
 
Old 07-14-2008, 08:56 PM   #11
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,695
Blog Entries: 5

Rep: Reputation: 239Reputation: 239Reputation: 239
Quote:
Originally Posted by Mr. C. View Post
FS is a regular expression, degenerate or not! :-)
lol. well, i did mention "without too much regular expressions"
 
Old 07-19-2008, 09:23 AM   #12
paragkalra
Member
 
Registered: Sep 2006
Location: Nagpur, Maharashtra, India
Distribution: Debian, Ubuntu, Redhat, Fedora, SLES, OpenSUSE, FreeBSD, Mac OS X
Posts: 221

Original Poster
Rep: Reputation: 31
Guys....Stop fighting.....All it maters is that it resolved my problem within seconds....Special thanks to "Disillusionist"...for making the command more easy & generalized ........All cheers to LQ...LQ rocks man...
 
Old 07-19-2008, 11:43 AM   #13
Mr. C.
Senior Member
 
Registered: Jun 2008
Posts: 2,529

Rep: Reputation: 59
Paragkalra - we're all having a good time. You might be misunderstanding the difference between "ribbing" and "fighting".

I made a self-deprecating joke about myself (being a degenerate); this invites the others to have a good time too.

There's no hostility here, at least I don't feel any.
 
Old 07-19-2008, 04:38 PM   #14
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,360

Rep: Reputation: 1909Reputation: 1909Reputation: 1909Reputation: 1909Reputation: 1909Reputation: 1909Reputation: 1909Reputation: 1909Reputation: 1909Reputation: 1909Reputation: 1909
Yeah, I was ribbing too... I know ghostdog for a long time, Mr.C. for a while, I felt I could do a little joke. ghostdog himself once told me to not be too serious... this is the good spirit at LQ. Enjoy it
 
Old 07-19-2008, 09:03 PM   #15
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,695
Blog Entries: 5

Rep: Reputation: 239Reputation: 239Reputation: 239
I have known colucix and Mr.C (from another forum) for a long time too and we are not fighting, so no worries. by the way, i sure hope that your path do not coincidentally have "test" and "iso" together like in
ide1:0.fileName = "D:\isos\test12blahiso\test1234-abcd.iso".
 
  


Reply

Tags
charachters, sed


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
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Replacing "function(x)" with "x" using sed/awk/smth Griffon26 Linux - General 3 11-22-2006 10:47 AM
Using "sed" - a simple explanation needed midiox Linux - Newbie 2 04-03-2006 06:47 AM
[sed] "Advanced" sed question(s) G00fy Programming 2 03-20-2006 12:34 AM
KPPP "Query Modem" returns empty fields ananya Linux - Hardware 2 02-21-2005 10:50 PM
KPPP "Query Modem" returns empty fields ananya Linux - Software 1 02-21-2005 10:39 PM


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