LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 02-19-2018, 02:19 AM   #1
gaurvrishi
Member
 
Registered: Apr 2012
Posts: 62

Rep: Reputation: Disabled
Smile change value from Sed command


Hi All,

Though i do understand how to use sed command and it's very easy but i am stuck in a situation where i need to change one value but it's changing all value.

Below i have component name with their version. Suppose if i want to change the component2 version says v2, then component5, component7 get also affected.
gauravrishi@administrator-ThinkPad-L470:/tmp$ cat 1.txt
component1: "v44"
component2: "v1"
component3: "v17"
component4: "v664"
component5: "v152"
component6: "v220"
component7: "v100"
component8: "v434"

sed -i "s/v1/v14/" 1.txt


component1: "v44"
component1: "v14"
component3: "v147"
component4: "v664"
component5: "v1451"
component6: "v1410"
component7: "v1400"
component8: "v434"

The value get change. I only want to change the value which i have mentioned in sed command.
 
Old 02-19-2018, 02:35 AM   #2
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 13,500

Rep: Reputation: 4314Reputation: 4314Reputation: 4314Reputation: 4314Reputation: 4314Reputation: 4314Reputation: 4314Reputation: 4314Reputation: 4314Reputation: 4314Reputation: 4314
there are several ways, probably:
Code:
sed 's/"v1"/"v14"/'
 
1 members found this post helpful.
Old 02-19-2018, 02:38 AM   #3
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 18,390

Rep: Reputation: 3046Reputation: 3046Reputation: 3046Reputation: 3046Reputation: 3046Reputation: 3046Reputation: 3046Reputation: 3046Reputation: 3046Reputation: 3046Reputation: 3046
Quote:
Originally Posted by gaurvrishi View Post
Though i do understand how to use sed command and it's very easy
The more you use sed (and regex) the more you will see the fallacy of this statement.
 
2 members found this post helpful.
Old 02-19-2018, 03:43 AM   #4
gaurvrishi
Member
 
Registered: Apr 2012
Posts: 62

Original Poster
Rep: Reputation: Disabled
Hi Thanks for your comment but the thing which i am looking it's not working.

new_version=14

current_version=$(cat 1.txt |grep component1 | cut -d: -f2 | sed 's/"//g' | sed 's/^..//' | tail -n1)

if [ "$current_version" == "$new_version" ]
then
echo "Version already present"
exit 1
else
echo "Changing the version"
sed -i "s/${current_version}/${new_version}/g" 1.txt
echo "current version: $current_version ----> new version: $new_version"

fi

Can you please help why the values are getting change. This is the code which i am using,
 
Old 02-19-2018, 04:12 AM   #5
gaurvrishi
Member
 
Registered: Apr 2012
Posts: 62

Original Poster
Rep: Reputation: Disabled
Thanks it's working for me. I have found the answer for this by using below mentioned command.

sed 's/"'${current_version}'"/"'${new_version}'"/' buildVersion.yaml
 
Old 02-19-2018, 04:23 AM   #6
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 13,500

Rep: Reputation: 4314Reputation: 4314Reputation: 4314Reputation: 4314Reputation: 4314Reputation: 4314Reputation: 4314Reputation: 4314Reputation: 4314Reputation: 4314Reputation: 4314
That is great!
(if you really want to say thanks just click on yes)
 
1 members found this post helpful.
Old 02-19-2018, 08:01 AM   #7
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 18,390

Rep: Reputation: 3046Reputation: 3046Reputation: 3046Reputation: 3046Reputation: 3046Reputation: 3046Reputation: 3046Reputation: 3046Reputation: 3046Reputation: 3046Reputation: 3046
You need to understand the effect of single versus double quotes - and the effect on the shell.
I prefer to use double quotes to allow natural variable substitution, and escape embedded double quotes (preferably avoid them altogether) - like this
Code:
sed "s/\"${current_version}\"/\"${new_version}\"/" buildVersion.yaml
I find consecutive quotes get confusing.
 
Old 02-19-2018, 09:04 AM   #8
gaurvrishi
Member
 
Registered: Apr 2012
Posts: 62

Original Poster
Rep: Reputation: Disabled
Thanks for your suggestions.

Could you please confirm what if I am using double quotes the code which I am using. Will this createa problem
 
Old 02-19-2018, 12:17 PM   #9
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,230

Rep: Reputation: 724Reputation: 724Reputation: 724Reputation: 724Reputation: 724Reputation: 724Reputation: 724
current_version=$(cat 1.txt |grep component1 | cut -d: -f2 | sed 's/"//g' | sed 's/^..//' | tail -n1)

Maybe could be shortened:
Code:
current_version=$(awk -F'"' '/component1/ {print substr($2, 2)}' cat 1.txt)

Last edited by keefaz; 02-19-2018 at 12:24 PM.
 
Old 02-19-2018, 09:18 PM   #10
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 1,267

Rep: Reputation: 580Reputation: 580Reputation: 580Reputation: 580Reputation: 580Reputation: 580
Normally the sed code should be in 'ticks'; your code correctly has the shell variable outside so the shell can substitute them.
But they must not contain special characters. If you want to allow special characters the variables must be in "quotes" for the shell to only substitute but not further expand them:
Code:
sed 's/"'"${current_version}"'"/"'"${new_version}"'"/'
For demonstration I put these quotes in red - in reality the chain "'" looks a bit confusing.
Therefore, in this case, putting the sed code in "quotes" is a good alternative.
 
Old 02-20-2018, 03:28 AM   #11
BudiKusasi
Member
 
Registered: Apr 2017
Posts: 54

Rep: Reputation: Disabled
sed -r 's/(component2:\s*"v)\w+/\12/' 1.txt
 
  


Reply

Tags
sed, sed bash, sed regex


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
Change ip using sed command glennbtn Linux - Software 7 08-02-2016 06:24 AM
sed command to global change if it match some pattern susanau Linux - Newbie 4 05-12-2015 03:57 AM
sed command to change variable names globally shridhar22 Linux - Newbie 21 02-06-2013 06:28 PM
sed: how to change MAC address string with sed cold Linux - Software 5 08-02-2010 08:43 AM
Sed Command - - -How to change part of the name of multiple files sahil.jammu Linux - Newbie 6 04-02-2009 11:57 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

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