LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
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, 01: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, 01:35 AM   #2
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,842

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

Rep: Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120
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, 02: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, 03: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, 03:23 AM   #6
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,842

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

Rep: Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120
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, 08: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, 11:17 AM   #9
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,552

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
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 11:24 AM.
 
Old 02-19-2018, 08:18 PM   #10
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,792

Rep: Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201
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, 02:28 AM   #11
BudiKusasi
Member
 
Registered: Apr 2017
Distribution: Artix
Posts: 345

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


Reply

Tags
sed, sed bash, sed regex



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

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

All times are GMT -5. The time now is 08:02 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
Open Source Consulting | Domain Registration