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
 
Search this Thread
Old 08-06-2008, 09:20 AM   #1
amit_pansuria
Member
 
Registered: Sep 2006
Posts: 73

Rep: Reputation: 15
how to replace value of 4.1.0.1.22 to 4.1.0.0.22 using shell script


helo ,

I have one requirement in my shell script. that
in my version.txt I have write down

4.1.0.1.22

Now I want to read this number and changes this number to 4.1.0.0.22 and
store it in seperate variable. original file number need not be changed.

Regards,
Amit
 
Old 08-06-2008, 09:49 AM   #2
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,508

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
If the original version number is stored in a variable, you can use substring replacement. For example
Code:
old_version=4.1.0.1.22
new_version=${old_version/1.22/0.22}
otherwise use sed, as in
Code:
new_version=$(echo 4.1.0.1.22 | sed s/4[.]1[.]0[.]1[.]22/4.1.0.0.22/}
the square brackets around the dots in the pattern are character lists: this is a way to make sed interpret a literal dot, otherwise it has the special meaning of any single character and the strings 4c1.0d1.22 or 4.1e0.1922 match as well.

Last edited by colucix; 08-06-2008 at 09:50 AM.
 
Old 08-06-2008, 08:04 PM   #3
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
I think escaping "." with "\" is preferable and less confusing. It does require the statement to be in either '' or "", though.
ta0kira
 
Old 08-06-2008, 08:30 PM   #4
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,696
Blog Entries: 5

Rep: Reputation: 241Reputation: 241Reputation: 241
if you always know where you want to change
Code:
awk -F"." ' $4=0;$1=$1' OFS="." file
 
  


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
Need a script to search and replace text in file using shell script unixlearner Programming 14 06-21-2007 10:37 PM
Shell script: Find "\n\t..." to replace a string in a file michael24h7d Programming 8 05-11-2007 03:07 AM
Shell script to find/replace build new TAB record ljungers Programming 6 01-19-2007 04:47 PM
How can I use a shell script to add and replace lines in a file? abefroman Programming 10 12-27-2005 05:05 PM
Search and replace text in file using shell script? matthurne Linux - Software 2 11-02-2004 10:11 AM


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