LinuxQuestions.org
Visit Jeremy's Blog.
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 10-01-2018, 12:56 PM   #1
docker_it_is
LQ Newbie
 
Registered: Oct 2018
Posts: 3

Rep: Reputation: Disabled
sed replace first occurrence but do subsequent


Hello, I am a newbie to Shell scripting so please forgive me for any nonsense things

My requirement:

Input file (Docker compose - yml format) has following:
version: "3.2"
services:
eurekaA:
image: dtr.abc.com/11658-us-eos/registry-service-qa:develop-4
eurekaB:
image: dtr.abc.com/11658-us-eos/registry-service-qa:develop-4
configserver:
image: dtr.abc.com/11658-us-eos/config-service-qa:develop-69
gatewayservice:
image: dtr.abc.com/11658-us-eos/gateway-service-qa:develop-26
groupcustomerservice:
image: dtr.abc.com/11658-us-eos/grpcust-service-qa:develop-149


Need to Retag all images with appending "-promote" keyword and I have following script working well until encountering a use case of repeated image name in the input file "registry-service"

Code:
========================================================================

Code:
images_to_promote=(`grep image docker-compose.yml | cut -d":" -f2,3`)

#Loop through the array and execute SED to search and replace

for i in "${images_to_promote[@]}"
        do
                new_Img_name="$i-promote"
                echo "Updating $i ---> $new_Img_name"
                eval sed -i 's#$i#$new_Img_name#g' docker-compose.yml
        done
========================================================================

The Output expected is :
dtr.abc.com/11658-us-eos/registry-service-qa:develop-4-promote
dtr.abc.com/11658-us-eos/registry-service-qa:develop-4-promote
dtr.abc.com/11658-us-eos/config-service-qa:develop-69-promote
dtr.abc.com/11658-us-eos/gateway-service-qa:develop-26-promote
dtr.abc.com/11658-us-eos/groupcustomer-service-qa:develop-149-promote


But instead I get:
dtr.abc.com/11658-us-eos/registry-service-qa:develop-4-promote-promote
dtr.abc.com/11658-us-eos/registry-service-qa:develop-4-promote-promote
dtr.abc.com/11658-us-eos/config-service-qa:develop-69-promote
dtr.abc.com/11658-us-eos/gateway-service-qa:develop-26-promote
dtr.abc.com/11658-us-eos/groupcustomer-service-qa:develop-149-promote



I have tried exact search using \< \>
I have tried getting rid of \g to limit search for only first occurrence

But none of these are helping.

Could someone please provide me direction to achieve this ?

Last edited by docker_it_is; 10-01-2018 at 02:11 PM.
 
Old 10-01-2018, 02:05 PM   #2
scasey
LQ Veteran
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.9.2009
Posts: 5,727

Rep: Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211
Please use code tags when posting commands, scripts or outputs.

I suspect the problem is with value in the images-to-promote variable, not with sed.
Put a set -x at the beginning of your script to see the actual values being processed.

Because you have duplicate file names in your input, those two names are both processed each time, resulting in the duplicated update.

Modify images_to_promote to remove duplicates:
Code:
images_to_promote=(`grep image lqtest | sort -u | cut -d":" -f2,3`)

Last edited by scasey; 10-01-2018 at 02:17 PM.
 
1 members found this post helpful.
Old 10-01-2018, 02:25 PM   #3
docker_it_is
LQ Newbie
 
Registered: Oct 2018
Posts: 3

Original Poster
Rep: Reputation: Disabled
Thumbs up

Quote:
Originally Posted by scasey View Post
Please use code tags when posting commands, scripts or outputs.

I suspect the problem is with value in the images-to-promote variable, not with sed.
Put a set -x at the beginning of your script to see the actual values being processed.

Because you have duplicate file names in your input, those two names are both processed each time, resulting in the duplicated update.

Modify images_to_promote to remove duplicates:
Code:
images_to_promote=(`grep image lqtest | sort -u | cut -d":" -f2,3`)


Just what I needed (A different perspective to look at the problem)
THANKS Sean for this and yes this has resolved the problem !
 
Old 10-02-2018, 03:14 AM   #4
l0f4r0
Member
 
Registered: Jul 2018
Location: Paris
Distribution: Debian
Posts: 900

Rep: Reputation: 290Reputation: 290Reputation: 290
Out of curiosity, are you doing other things with your var images_to_promote or inside your for loop?
Because IMHO your script, as it is, seems pretty complicated for what it does. Maybe I do not have all the context, but why not use the following?
Code:
sed -n -r '/image/ s/develop-[0-9]+/&-promote/p' docker-compose.yml
 
Old 10-02-2018, 12:47 PM   #5
docker_it_is
LQ Newbie
 
Registered: Oct 2018
Posts: 3

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by l0f4r0 View Post
Out of curiosity, are you doing other things with your var images_to_promote or inside your for loop?
Because IMHO your script, as it is, seems pretty complicated for what it does. Maybe I do not have all the context, but why not use the following?
Code:
sed -n -r '/image/ s/develop-[0-9]+/&-promote/p' docker-compose.yml
Yes, I have multiple other things to do in that loop but this is great too and in fact with your help I have modified and separated the loop to do rest of the stuff and achieved the desired result using following one line sed:
Code:
sed -i '/image:/s/$/-Promote/' docker-compose.yml
Yet another perspective A Big thanks to you too!
 
Old 10-03-2018, 02:30 AM   #6
l0f4r0
Member
 
Registered: Jul 2018
Location: Paris
Distribution: Debian
Posts: 900

Rep: Reputation: 290Reputation: 290Reputation: 290
Perfect then, everybody is happy
Maybe you can mark your thread as resolved?
 
Old 10-03-2018, 07:21 AM   #7
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,790

Rep: Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201
With shell builtins it's efficient and flexible
Code:
fn=docker-compose.yml
#loop thru the lines
while IFS=":" read w1 i
do
  # branch on keyword
  case $w1 in
  (image)
    new_Img_name="$i-promote"
    echo "Updating $i ---> $new_Img_name"
    echo "$new_Img_name" >&3
  esac
done < $fn 3> $fn.new
 
  


Reply



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
[SOLVED] Replace 2nd occurrence of a string in a file - sed or awk? kushalkoolwal Programming 26 09-26-2021 04:10 PM
Get nth occurrence of two strings and replace it Mann_engg Linux - Newbie 2 05-30-2013 05:56 PM
how to replace occurrence of a string in many files in one command tkmsr Programming 1 10-30-2010 06:29 AM
SED replace string by occurrence uttam_h Programming 5 03-05-2008 10:02 PM
Replace every other occurrence of pattern Wynd Linux - General 8 12-14-2005 03:43 PM

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

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