Find and replace all numbers after a string value and increment each one












0














I am trying to see if I can achieve a find/replace and increment an integer value in a couple of lines. To explain, I have the following text:



&sr=1240000000000&type=game&scoreA=x&scoreB=y&
&sr=1150000000000&type=game&scoreB=x&scoreB=y&
&sr=1270000000000&type=game&scoreC=x&scoreB=y&
&sr=1010000000000&type=game&scoreD=x&scoreB=y&


I want to replace ONLY the number after sr= till... & so for example as shown in bold:




&sr=1240000000000&type=game&scoreA=x&scoreB=y&




All numbers to be replaced and incremented by 10000 for each subsequent one found...



So ideally when I use a script as follows (Taking 2 args, first is value to replace to, and the input file):



./script.sh 1500000000000 file.txt



it should replace all as:



&sr=1500000010000&type=game&scoreA=x&scoreB=y&
&sr=1500000020000&type=game&scoreB=x&scoreB=y&
&sr=1500000030000&type=game&scoreC=x&scoreB=y&
&sr=1500000040000&type=game&scoreD=x&scoreB=y&


I put together a python script to achieve this but was wondering if anyone can advise me on doing this in a 1-3 liner script, maybe using AWK/SED. My python script is kinda long.










share|improve this question




















  • 1




    Can you show us your work so far?
    – ghoti
    Nov 12 '18 at 21:59
















0














I am trying to see if I can achieve a find/replace and increment an integer value in a couple of lines. To explain, I have the following text:



&sr=1240000000000&type=game&scoreA=x&scoreB=y&
&sr=1150000000000&type=game&scoreB=x&scoreB=y&
&sr=1270000000000&type=game&scoreC=x&scoreB=y&
&sr=1010000000000&type=game&scoreD=x&scoreB=y&


I want to replace ONLY the number after sr= till... & so for example as shown in bold:




&sr=1240000000000&type=game&scoreA=x&scoreB=y&




All numbers to be replaced and incremented by 10000 for each subsequent one found...



So ideally when I use a script as follows (Taking 2 args, first is value to replace to, and the input file):



./script.sh 1500000000000 file.txt



it should replace all as:



&sr=1500000010000&type=game&scoreA=x&scoreB=y&
&sr=1500000020000&type=game&scoreB=x&scoreB=y&
&sr=1500000030000&type=game&scoreC=x&scoreB=y&
&sr=1500000040000&type=game&scoreD=x&scoreB=y&


I put together a python script to achieve this but was wondering if anyone can advise me on doing this in a 1-3 liner script, maybe using AWK/SED. My python script is kinda long.










share|improve this question




















  • 1




    Can you show us your work so far?
    – ghoti
    Nov 12 '18 at 21:59














0












0








0







I am trying to see if I can achieve a find/replace and increment an integer value in a couple of lines. To explain, I have the following text:



&sr=1240000000000&type=game&scoreA=x&scoreB=y&
&sr=1150000000000&type=game&scoreB=x&scoreB=y&
&sr=1270000000000&type=game&scoreC=x&scoreB=y&
&sr=1010000000000&type=game&scoreD=x&scoreB=y&


I want to replace ONLY the number after sr= till... & so for example as shown in bold:




&sr=1240000000000&type=game&scoreA=x&scoreB=y&




All numbers to be replaced and incremented by 10000 for each subsequent one found...



So ideally when I use a script as follows (Taking 2 args, first is value to replace to, and the input file):



./script.sh 1500000000000 file.txt



it should replace all as:



&sr=1500000010000&type=game&scoreA=x&scoreB=y&
&sr=1500000020000&type=game&scoreB=x&scoreB=y&
&sr=1500000030000&type=game&scoreC=x&scoreB=y&
&sr=1500000040000&type=game&scoreD=x&scoreB=y&


I put together a python script to achieve this but was wondering if anyone can advise me on doing this in a 1-3 liner script, maybe using AWK/SED. My python script is kinda long.










share|improve this question















I am trying to see if I can achieve a find/replace and increment an integer value in a couple of lines. To explain, I have the following text:



&sr=1240000000000&type=game&scoreA=x&scoreB=y&
&sr=1150000000000&type=game&scoreB=x&scoreB=y&
&sr=1270000000000&type=game&scoreC=x&scoreB=y&
&sr=1010000000000&type=game&scoreD=x&scoreB=y&


I want to replace ONLY the number after sr= till... & so for example as shown in bold:




&sr=1240000000000&type=game&scoreA=x&scoreB=y&




All numbers to be replaced and incremented by 10000 for each subsequent one found...



So ideally when I use a script as follows (Taking 2 args, first is value to replace to, and the input file):



./script.sh 1500000000000 file.txt



it should replace all as:



&sr=1500000010000&type=game&scoreA=x&scoreB=y&
&sr=1500000020000&type=game&scoreB=x&scoreB=y&
&sr=1500000030000&type=game&scoreC=x&scoreB=y&
&sr=1500000040000&type=game&scoreD=x&scoreB=y&


I put together a python script to achieve this but was wondering if anyone can advise me on doing this in a 1-3 liner script, maybe using AWK/SED. My python script is kinda long.







regex awk sed replace






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 12 '18 at 21:44

























asked Nov 12 '18 at 21:31









Saffik

128112




128112








  • 1




    Can you show us your work so far?
    – ghoti
    Nov 12 '18 at 21:59














  • 1




    Can you show us your work so far?
    – ghoti
    Nov 12 '18 at 21:59








1




1




Can you show us your work so far?
– ghoti
Nov 12 '18 at 21:59




Can you show us your work so far?
– ghoti
Nov 12 '18 at 21:59












2 Answers
2






active

oldest

votes


















0














You may use this awk:



awk -v n='10000' -v s='1500000000000' 'sub("sr=[0-9]+&", "sr=" (s+i+n) "\&"){
i+=n} 1' file




&sr=1500000010000&type=game&scoreA=x&scoreB=y&
&sr=1500000020000&type=game&scoreB=x&scoreB=y&
&sr=1500000030000&type=game&scoreC=x&scoreB=y&
&sr=1500000040000&type=game&scoreD=x&scoreB=y&





share|improve this answer























  • But what happens if the value 1240000000000 is not known. It may be different values, I just gave an example. I edited the question to make it clear sorry.:)
    – Saffik
    Nov 12 '18 at 21:43












  • What i mean is: we will pass 1500000000000 to the script as input argument. But the value in the text file e.g. sr=1240000000000& can vary on each line so hardcoding it in script to find it and replace it might not work.
    – Saffik
    Nov 12 '18 at 21:51






  • 1




    In the file we will have random numbers after sr=xxx. The script will update the first one with a value we pass to script, for e.g. 1500000000000 and there-onwards it will increment each one after that with +10000.
    – Saffik
    Nov 12 '18 at 21:54










  • ok check my updated answer.
    – anubhava
    Nov 12 '18 at 21:56










  • Yerp, I think since you answered first but both answers are exactly what I was looking for! - thanks alot.
    – Saffik
    Nov 12 '18 at 22:05



















1














You don't need a bash script, awk can handle this on its own



awk -v num=1500000000000 '{num += 10000; sub(/^&sr=[0-9]+/, "\&sr=" num)}1' file > tmpfile
mv tmpfile file





share|improve this answer























  • Looks good. Any idea how I apply this change to the file? Currently it prints to console. :)
    – Saffik
    Nov 12 '18 at 21:55










  • @Saffik updated
    – oguzismail
    Nov 12 '18 at 21:56











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53270399%2ffind-and-replace-all-numbers-after-a-string-value-and-increment-each-one%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














You may use this awk:



awk -v n='10000' -v s='1500000000000' 'sub("sr=[0-9]+&", "sr=" (s+i+n) "\&"){
i+=n} 1' file




&sr=1500000010000&type=game&scoreA=x&scoreB=y&
&sr=1500000020000&type=game&scoreB=x&scoreB=y&
&sr=1500000030000&type=game&scoreC=x&scoreB=y&
&sr=1500000040000&type=game&scoreD=x&scoreB=y&





share|improve this answer























  • But what happens if the value 1240000000000 is not known. It may be different values, I just gave an example. I edited the question to make it clear sorry.:)
    – Saffik
    Nov 12 '18 at 21:43












  • What i mean is: we will pass 1500000000000 to the script as input argument. But the value in the text file e.g. sr=1240000000000& can vary on each line so hardcoding it in script to find it and replace it might not work.
    – Saffik
    Nov 12 '18 at 21:51






  • 1




    In the file we will have random numbers after sr=xxx. The script will update the first one with a value we pass to script, for e.g. 1500000000000 and there-onwards it will increment each one after that with +10000.
    – Saffik
    Nov 12 '18 at 21:54










  • ok check my updated answer.
    – anubhava
    Nov 12 '18 at 21:56










  • Yerp, I think since you answered first but both answers are exactly what I was looking for! - thanks alot.
    – Saffik
    Nov 12 '18 at 22:05
















0














You may use this awk:



awk -v n='10000' -v s='1500000000000' 'sub("sr=[0-9]+&", "sr=" (s+i+n) "\&"){
i+=n} 1' file




&sr=1500000010000&type=game&scoreA=x&scoreB=y&
&sr=1500000020000&type=game&scoreB=x&scoreB=y&
&sr=1500000030000&type=game&scoreC=x&scoreB=y&
&sr=1500000040000&type=game&scoreD=x&scoreB=y&





share|improve this answer























  • But what happens if the value 1240000000000 is not known. It may be different values, I just gave an example. I edited the question to make it clear sorry.:)
    – Saffik
    Nov 12 '18 at 21:43












  • What i mean is: we will pass 1500000000000 to the script as input argument. But the value in the text file e.g. sr=1240000000000& can vary on each line so hardcoding it in script to find it and replace it might not work.
    – Saffik
    Nov 12 '18 at 21:51






  • 1




    In the file we will have random numbers after sr=xxx. The script will update the first one with a value we pass to script, for e.g. 1500000000000 and there-onwards it will increment each one after that with +10000.
    – Saffik
    Nov 12 '18 at 21:54










  • ok check my updated answer.
    – anubhava
    Nov 12 '18 at 21:56










  • Yerp, I think since you answered first but both answers are exactly what I was looking for! - thanks alot.
    – Saffik
    Nov 12 '18 at 22:05














0












0








0






You may use this awk:



awk -v n='10000' -v s='1500000000000' 'sub("sr=[0-9]+&", "sr=" (s+i+n) "\&"){
i+=n} 1' file




&sr=1500000010000&type=game&scoreA=x&scoreB=y&
&sr=1500000020000&type=game&scoreB=x&scoreB=y&
&sr=1500000030000&type=game&scoreC=x&scoreB=y&
&sr=1500000040000&type=game&scoreD=x&scoreB=y&





share|improve this answer














You may use this awk:



awk -v n='10000' -v s='1500000000000' 'sub("sr=[0-9]+&", "sr=" (s+i+n) "\&"){
i+=n} 1' file




&sr=1500000010000&type=game&scoreA=x&scoreB=y&
&sr=1500000020000&type=game&scoreB=x&scoreB=y&
&sr=1500000030000&type=game&scoreC=x&scoreB=y&
&sr=1500000040000&type=game&scoreD=x&scoreB=y&






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 12 '18 at 21:56

























answered Nov 12 '18 at 21:39









anubhava

520k46316390




520k46316390












  • But what happens if the value 1240000000000 is not known. It may be different values, I just gave an example. I edited the question to make it clear sorry.:)
    – Saffik
    Nov 12 '18 at 21:43












  • What i mean is: we will pass 1500000000000 to the script as input argument. But the value in the text file e.g. sr=1240000000000& can vary on each line so hardcoding it in script to find it and replace it might not work.
    – Saffik
    Nov 12 '18 at 21:51






  • 1




    In the file we will have random numbers after sr=xxx. The script will update the first one with a value we pass to script, for e.g. 1500000000000 and there-onwards it will increment each one after that with +10000.
    – Saffik
    Nov 12 '18 at 21:54










  • ok check my updated answer.
    – anubhava
    Nov 12 '18 at 21:56










  • Yerp, I think since you answered first but both answers are exactly what I was looking for! - thanks alot.
    – Saffik
    Nov 12 '18 at 22:05


















  • But what happens if the value 1240000000000 is not known. It may be different values, I just gave an example. I edited the question to make it clear sorry.:)
    – Saffik
    Nov 12 '18 at 21:43












  • What i mean is: we will pass 1500000000000 to the script as input argument. But the value in the text file e.g. sr=1240000000000& can vary on each line so hardcoding it in script to find it and replace it might not work.
    – Saffik
    Nov 12 '18 at 21:51






  • 1




    In the file we will have random numbers after sr=xxx. The script will update the first one with a value we pass to script, for e.g. 1500000000000 and there-onwards it will increment each one after that with +10000.
    – Saffik
    Nov 12 '18 at 21:54










  • ok check my updated answer.
    – anubhava
    Nov 12 '18 at 21:56










  • Yerp, I think since you answered first but both answers are exactly what I was looking for! - thanks alot.
    – Saffik
    Nov 12 '18 at 22:05
















But what happens if the value 1240000000000 is not known. It may be different values, I just gave an example. I edited the question to make it clear sorry.:)
– Saffik
Nov 12 '18 at 21:43






But what happens if the value 1240000000000 is not known. It may be different values, I just gave an example. I edited the question to make it clear sorry.:)
– Saffik
Nov 12 '18 at 21:43














What i mean is: we will pass 1500000000000 to the script as input argument. But the value in the text file e.g. sr=1240000000000& can vary on each line so hardcoding it in script to find it and replace it might not work.
– Saffik
Nov 12 '18 at 21:51




What i mean is: we will pass 1500000000000 to the script as input argument. But the value in the text file e.g. sr=1240000000000& can vary on each line so hardcoding it in script to find it and replace it might not work.
– Saffik
Nov 12 '18 at 21:51




1




1




In the file we will have random numbers after sr=xxx. The script will update the first one with a value we pass to script, for e.g. 1500000000000 and there-onwards it will increment each one after that with +10000.
– Saffik
Nov 12 '18 at 21:54




In the file we will have random numbers after sr=xxx. The script will update the first one with a value we pass to script, for e.g. 1500000000000 and there-onwards it will increment each one after that with +10000.
– Saffik
Nov 12 '18 at 21:54












ok check my updated answer.
– anubhava
Nov 12 '18 at 21:56




ok check my updated answer.
– anubhava
Nov 12 '18 at 21:56












Yerp, I think since you answered first but both answers are exactly what I was looking for! - thanks alot.
– Saffik
Nov 12 '18 at 22:05




Yerp, I think since you answered first but both answers are exactly what I was looking for! - thanks alot.
– Saffik
Nov 12 '18 at 22:05













1














You don't need a bash script, awk can handle this on its own



awk -v num=1500000000000 '{num += 10000; sub(/^&sr=[0-9]+/, "\&sr=" num)}1' file > tmpfile
mv tmpfile file





share|improve this answer























  • Looks good. Any idea how I apply this change to the file? Currently it prints to console. :)
    – Saffik
    Nov 12 '18 at 21:55










  • @Saffik updated
    – oguzismail
    Nov 12 '18 at 21:56
















1














You don't need a bash script, awk can handle this on its own



awk -v num=1500000000000 '{num += 10000; sub(/^&sr=[0-9]+/, "\&sr=" num)}1' file > tmpfile
mv tmpfile file





share|improve this answer























  • Looks good. Any idea how I apply this change to the file? Currently it prints to console. :)
    – Saffik
    Nov 12 '18 at 21:55










  • @Saffik updated
    – oguzismail
    Nov 12 '18 at 21:56














1












1








1






You don't need a bash script, awk can handle this on its own



awk -v num=1500000000000 '{num += 10000; sub(/^&sr=[0-9]+/, "\&sr=" num)}1' file > tmpfile
mv tmpfile file





share|improve this answer














You don't need a bash script, awk can handle this on its own



awk -v num=1500000000000 '{num += 10000; sub(/^&sr=[0-9]+/, "\&sr=" num)}1' file > tmpfile
mv tmpfile file






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 12 '18 at 21:56

























answered Nov 12 '18 at 21:44









oguzismail

3,24131025




3,24131025












  • Looks good. Any idea how I apply this change to the file? Currently it prints to console. :)
    – Saffik
    Nov 12 '18 at 21:55










  • @Saffik updated
    – oguzismail
    Nov 12 '18 at 21:56


















  • Looks good. Any idea how I apply this change to the file? Currently it prints to console. :)
    – Saffik
    Nov 12 '18 at 21:55










  • @Saffik updated
    – oguzismail
    Nov 12 '18 at 21:56
















Looks good. Any idea how I apply this change to the file? Currently it prints to console. :)
– Saffik
Nov 12 '18 at 21:55




Looks good. Any idea how I apply this change to the file? Currently it prints to console. :)
– Saffik
Nov 12 '18 at 21:55












@Saffik updated
– oguzismail
Nov 12 '18 at 21:56




@Saffik updated
– oguzismail
Nov 12 '18 at 21:56


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53270399%2ffind-and-replace-all-numbers-after-a-string-value-and-increment-each-one%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Xamarin.iOS Cant Deploy on Iphone

Glorious Revolution

Dulmage-Mendelsohn matrix decomposition in Python