How do I replace words with increasing numbers?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP












2















For example, the only thing in file.txt looks like this:



xxxxxxxxxHAHAxxxxxxHOHOxxxxxxx 


I hope to replace HAHA with seq 1 3 and replace HOHO with seq 5 7, so the output should be:



xxxxxxxxx1xxxxxx5xxxxxxx 
xxxxxxxxx2xxxxxx6xxxxxxx
xxxxxxxxx3xxxxxx7xxxxxxx


What I did:



for i in $(seq 1 3) 
do sed "s/HAHA/$i/g" file.txt
for i in $(seq 5 7)
do sed "s/HOHO/$i/g" file.txt
done
done > new.txt


But new.txt doesn't show what I expected. How should I change the code?










share|improve this question
























  • By the way, I added four spaces to each line of my code, but it doesn't format like what it should be like

    – LittleG
    Mar 4 at 1:58















2















For example, the only thing in file.txt looks like this:



xxxxxxxxxHAHAxxxxxxHOHOxxxxxxx 


I hope to replace HAHA with seq 1 3 and replace HOHO with seq 5 7, so the output should be:



xxxxxxxxx1xxxxxx5xxxxxxx 
xxxxxxxxx2xxxxxx6xxxxxxx
xxxxxxxxx3xxxxxx7xxxxxxx


What I did:



for i in $(seq 1 3) 
do sed "s/HAHA/$i/g" file.txt
for i in $(seq 5 7)
do sed "s/HOHO/$i/g" file.txt
done
done > new.txt


But new.txt doesn't show what I expected. How should I change the code?










share|improve this question
























  • By the way, I added four spaces to each line of my code, but it doesn't format like what it should be like

    – LittleG
    Mar 4 at 1:58













2












2








2








For example, the only thing in file.txt looks like this:



xxxxxxxxxHAHAxxxxxxHOHOxxxxxxx 


I hope to replace HAHA with seq 1 3 and replace HOHO with seq 5 7, so the output should be:



xxxxxxxxx1xxxxxx5xxxxxxx 
xxxxxxxxx2xxxxxx6xxxxxxx
xxxxxxxxx3xxxxxx7xxxxxxx


What I did:



for i in $(seq 1 3) 
do sed "s/HAHA/$i/g" file.txt
for i in $(seq 5 7)
do sed "s/HOHO/$i/g" file.txt
done
done > new.txt


But new.txt doesn't show what I expected. How should I change the code?










share|improve this question
















For example, the only thing in file.txt looks like this:



xxxxxxxxxHAHAxxxxxxHOHOxxxxxxx 


I hope to replace HAHA with seq 1 3 and replace HOHO with seq 5 7, so the output should be:



xxxxxxxxx1xxxxxx5xxxxxxx 
xxxxxxxxx2xxxxxx6xxxxxxx
xxxxxxxxx3xxxxxx7xxxxxxx


What I did:



for i in $(seq 1 3) 
do sed "s/HAHA/$i/g" file.txt
for i in $(seq 5 7)
do sed "s/HOHO/$i/g" file.txt
done
done > new.txt


But new.txt doesn't show what I expected. How should I change the code?







bash shell-script text-processing






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 4 at 2:02







LittleG

















asked Mar 4 at 1:57









LittleGLittleG

206




206












  • By the way, I added four spaces to each line of my code, but it doesn't format like what it should be like

    – LittleG
    Mar 4 at 1:58

















  • By the way, I added four spaces to each line of my code, but it doesn't format like what it should be like

    – LittleG
    Mar 4 at 1:58
















By the way, I added four spaces to each line of my code, but it doesn't format like what it should be like

– LittleG
Mar 4 at 1:58





By the way, I added four spaces to each line of my code, but it doesn't format like what it should be like

– LittleG
Mar 4 at 1:58










2 Answers
2






active

oldest

votes


















2














Here's one way you could do it, using the bash built-in read command's ability to read from different file descriptors:



while read -u3 i && read -u4 j; do 
sed -e "s/HAHA/$i/" -e "s/HOHO/$j/" file.txt
done > new.txt 3< <(seq 1 3) 4< <(seq 5 7)



Since your numbers have a simple arithmetic relationship, it would be simpler to use a single seq process + some shell arithmetic:



for i in $(seq 1 3); do
sed -e "s/HAHA/$i/" -e "s/HOHO/$((i+4))/" file.txt
done > new.txt


In either case, see Why is using a shell loop to process text considered bad practice? - the inefficiencies may not matter for the minimal example you have provided, but if you are doing anything more serious you should consider using a different approach.






share|improve this answer




















  • 1





    I wonder why you use 3 and 4 after the -u. Did you just choose these numbers randomly?

    – LittleG
    Mar 4 at 2:27






  • 1





    @LittleG they are the first two numbers after the terminal's standard input / output / error streams (fd 0, 1, 2) that are available as file descriptors

    – steeldriver
    Mar 4 at 2:32






  • 1





    In the first solution, why do you add a space between the two <?

    – LittleG
    Mar 4 at 2:46






  • 1





    @LittleG iirc it's necessary in order for the shell parser to distinguish < <(command) from the here-document syntax << word, but I may be wrong about that: see for example What are the shell's control and redirection operators?

    – steeldriver
    Mar 4 at 2:58







  • 1





    @LittleG you may not be able to avoid a loop, but you can avoid a shell loop, as shown in Kusalananda's answer

    – steeldriver
    Mar 5 at 0:08


















2














Without calling an external utility for each line produced:



read template <file

paste <(seq 1 3) <(seq 5 7) |
while read x y; do
t=$template/HAHA/$x
t=$t/HOHO/$y
printf '%sn' "$t"
done >new.txt


This first reads the line from the file called file into $template. It then constructs a two-column input for a while read loop. The input to the loop is the two columns of numbers from seq.



In the loop, some bash-specific substitutions are performed on the value of $template to replace the HAHA and HOHO string with the numbers read from paste. This creates $t which is then outputted.




Using only awk, and assuming that the input is a single line only:



awk '
for (i = 1; i <= 3; ++i)
t = $0;
sub("HAHA", i, t)
sub("HOHO", 4+i, t)
print t

' file



A shell loop for bash, mimicking the awk code above:



read template <file

for (( i = 1; i <= 3; ++i )); do
t=$template/HAHA/$i
t=$t/HOHO/$((i+4))
printf '%sn' "$t"
done





share|improve this answer

























  • You said 'Without calling an external utility' - does this mean it runs faster than calling sed?

    – LittleG
    Mar 4 at 21:11






  • 1





    @LittleG Calling an external utility, like sed, in a loop is slow in comparison to using only built-in utilities in a loop, yes.

    – Kusalananda
    Mar 4 at 21:45











Your Answer








StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "106"
;
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: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
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%2funix.stackexchange.com%2fquestions%2f504187%2fhow-do-i-replace-words-with-increasing-numbers%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









2














Here's one way you could do it, using the bash built-in read command's ability to read from different file descriptors:



while read -u3 i && read -u4 j; do 
sed -e "s/HAHA/$i/" -e "s/HOHO/$j/" file.txt
done > new.txt 3< <(seq 1 3) 4< <(seq 5 7)



Since your numbers have a simple arithmetic relationship, it would be simpler to use a single seq process + some shell arithmetic:



for i in $(seq 1 3); do
sed -e "s/HAHA/$i/" -e "s/HOHO/$((i+4))/" file.txt
done > new.txt


In either case, see Why is using a shell loop to process text considered bad practice? - the inefficiencies may not matter for the minimal example you have provided, but if you are doing anything more serious you should consider using a different approach.






share|improve this answer




















  • 1





    I wonder why you use 3 and 4 after the -u. Did you just choose these numbers randomly?

    – LittleG
    Mar 4 at 2:27






  • 1





    @LittleG they are the first two numbers after the terminal's standard input / output / error streams (fd 0, 1, 2) that are available as file descriptors

    – steeldriver
    Mar 4 at 2:32






  • 1





    In the first solution, why do you add a space between the two <?

    – LittleG
    Mar 4 at 2:46






  • 1





    @LittleG iirc it's necessary in order for the shell parser to distinguish < <(command) from the here-document syntax << word, but I may be wrong about that: see for example What are the shell's control and redirection operators?

    – steeldriver
    Mar 4 at 2:58







  • 1





    @LittleG you may not be able to avoid a loop, but you can avoid a shell loop, as shown in Kusalananda's answer

    – steeldriver
    Mar 5 at 0:08















2














Here's one way you could do it, using the bash built-in read command's ability to read from different file descriptors:



while read -u3 i && read -u4 j; do 
sed -e "s/HAHA/$i/" -e "s/HOHO/$j/" file.txt
done > new.txt 3< <(seq 1 3) 4< <(seq 5 7)



Since your numbers have a simple arithmetic relationship, it would be simpler to use a single seq process + some shell arithmetic:



for i in $(seq 1 3); do
sed -e "s/HAHA/$i/" -e "s/HOHO/$((i+4))/" file.txt
done > new.txt


In either case, see Why is using a shell loop to process text considered bad practice? - the inefficiencies may not matter for the minimal example you have provided, but if you are doing anything more serious you should consider using a different approach.






share|improve this answer




















  • 1





    I wonder why you use 3 and 4 after the -u. Did you just choose these numbers randomly?

    – LittleG
    Mar 4 at 2:27






  • 1





    @LittleG they are the first two numbers after the terminal's standard input / output / error streams (fd 0, 1, 2) that are available as file descriptors

    – steeldriver
    Mar 4 at 2:32






  • 1





    In the first solution, why do you add a space between the two <?

    – LittleG
    Mar 4 at 2:46






  • 1





    @LittleG iirc it's necessary in order for the shell parser to distinguish < <(command) from the here-document syntax << word, but I may be wrong about that: see for example What are the shell's control and redirection operators?

    – steeldriver
    Mar 4 at 2:58







  • 1





    @LittleG you may not be able to avoid a loop, but you can avoid a shell loop, as shown in Kusalananda's answer

    – steeldriver
    Mar 5 at 0:08













2












2








2







Here's one way you could do it, using the bash built-in read command's ability to read from different file descriptors:



while read -u3 i && read -u4 j; do 
sed -e "s/HAHA/$i/" -e "s/HOHO/$j/" file.txt
done > new.txt 3< <(seq 1 3) 4< <(seq 5 7)



Since your numbers have a simple arithmetic relationship, it would be simpler to use a single seq process + some shell arithmetic:



for i in $(seq 1 3); do
sed -e "s/HAHA/$i/" -e "s/HOHO/$((i+4))/" file.txt
done > new.txt


In either case, see Why is using a shell loop to process text considered bad practice? - the inefficiencies may not matter for the minimal example you have provided, but if you are doing anything more serious you should consider using a different approach.






share|improve this answer















Here's one way you could do it, using the bash built-in read command's ability to read from different file descriptors:



while read -u3 i && read -u4 j; do 
sed -e "s/HAHA/$i/" -e "s/HOHO/$j/" file.txt
done > new.txt 3< <(seq 1 3) 4< <(seq 5 7)



Since your numbers have a simple arithmetic relationship, it would be simpler to use a single seq process + some shell arithmetic:



for i in $(seq 1 3); do
sed -e "s/HAHA/$i/" -e "s/HOHO/$((i+4))/" file.txt
done > new.txt


In either case, see Why is using a shell loop to process text considered bad practice? - the inefficiencies may not matter for the minimal example you have provided, but if you are doing anything more serious you should consider using a different approach.







share|improve this answer














share|improve this answer



share|improve this answer








edited Mar 4 at 15:12

























answered Mar 4 at 2:15









steeldriversteeldriver

37.6k45389




37.6k45389







  • 1





    I wonder why you use 3 and 4 after the -u. Did you just choose these numbers randomly?

    – LittleG
    Mar 4 at 2:27






  • 1





    @LittleG they are the first two numbers after the terminal's standard input / output / error streams (fd 0, 1, 2) that are available as file descriptors

    – steeldriver
    Mar 4 at 2:32






  • 1





    In the first solution, why do you add a space between the two <?

    – LittleG
    Mar 4 at 2:46






  • 1





    @LittleG iirc it's necessary in order for the shell parser to distinguish < <(command) from the here-document syntax << word, but I may be wrong about that: see for example What are the shell's control and redirection operators?

    – steeldriver
    Mar 4 at 2:58







  • 1





    @LittleG you may not be able to avoid a loop, but you can avoid a shell loop, as shown in Kusalananda's answer

    – steeldriver
    Mar 5 at 0:08












  • 1





    I wonder why you use 3 and 4 after the -u. Did you just choose these numbers randomly?

    – LittleG
    Mar 4 at 2:27






  • 1





    @LittleG they are the first two numbers after the terminal's standard input / output / error streams (fd 0, 1, 2) that are available as file descriptors

    – steeldriver
    Mar 4 at 2:32






  • 1





    In the first solution, why do you add a space between the two <?

    – LittleG
    Mar 4 at 2:46






  • 1





    @LittleG iirc it's necessary in order for the shell parser to distinguish < <(command) from the here-document syntax << word, but I may be wrong about that: see for example What are the shell's control and redirection operators?

    – steeldriver
    Mar 4 at 2:58







  • 1





    @LittleG you may not be able to avoid a loop, but you can avoid a shell loop, as shown in Kusalananda's answer

    – steeldriver
    Mar 5 at 0:08







1




1





I wonder why you use 3 and 4 after the -u. Did you just choose these numbers randomly?

– LittleG
Mar 4 at 2:27





I wonder why you use 3 and 4 after the -u. Did you just choose these numbers randomly?

– LittleG
Mar 4 at 2:27




1




1





@LittleG they are the first two numbers after the terminal's standard input / output / error streams (fd 0, 1, 2) that are available as file descriptors

– steeldriver
Mar 4 at 2:32





@LittleG they are the first two numbers after the terminal's standard input / output / error streams (fd 0, 1, 2) that are available as file descriptors

– steeldriver
Mar 4 at 2:32




1




1





In the first solution, why do you add a space between the two <?

– LittleG
Mar 4 at 2:46





In the first solution, why do you add a space between the two <?

– LittleG
Mar 4 at 2:46




1




1





@LittleG iirc it's necessary in order for the shell parser to distinguish < <(command) from the here-document syntax << word, but I may be wrong about that: see for example What are the shell's control and redirection operators?

– steeldriver
Mar 4 at 2:58






@LittleG iirc it's necessary in order for the shell parser to distinguish < <(command) from the here-document syntax << word, but I may be wrong about that: see for example What are the shell's control and redirection operators?

– steeldriver
Mar 4 at 2:58





1




1





@LittleG you may not be able to avoid a loop, but you can avoid a shell loop, as shown in Kusalananda's answer

– steeldriver
Mar 5 at 0:08





@LittleG you may not be able to avoid a loop, but you can avoid a shell loop, as shown in Kusalananda's answer

– steeldriver
Mar 5 at 0:08













2














Without calling an external utility for each line produced:



read template <file

paste <(seq 1 3) <(seq 5 7) |
while read x y; do
t=$template/HAHA/$x
t=$t/HOHO/$y
printf '%sn' "$t"
done >new.txt


This first reads the line from the file called file into $template. It then constructs a two-column input for a while read loop. The input to the loop is the two columns of numbers from seq.



In the loop, some bash-specific substitutions are performed on the value of $template to replace the HAHA and HOHO string with the numbers read from paste. This creates $t which is then outputted.




Using only awk, and assuming that the input is a single line only:



awk '
for (i = 1; i <= 3; ++i)
t = $0;
sub("HAHA", i, t)
sub("HOHO", 4+i, t)
print t

' file



A shell loop for bash, mimicking the awk code above:



read template <file

for (( i = 1; i <= 3; ++i )); do
t=$template/HAHA/$i
t=$t/HOHO/$((i+4))
printf '%sn' "$t"
done





share|improve this answer

























  • You said 'Without calling an external utility' - does this mean it runs faster than calling sed?

    – LittleG
    Mar 4 at 21:11






  • 1





    @LittleG Calling an external utility, like sed, in a loop is slow in comparison to using only built-in utilities in a loop, yes.

    – Kusalananda
    Mar 4 at 21:45















2














Without calling an external utility for each line produced:



read template <file

paste <(seq 1 3) <(seq 5 7) |
while read x y; do
t=$template/HAHA/$x
t=$t/HOHO/$y
printf '%sn' "$t"
done >new.txt


This first reads the line from the file called file into $template. It then constructs a two-column input for a while read loop. The input to the loop is the two columns of numbers from seq.



In the loop, some bash-specific substitutions are performed on the value of $template to replace the HAHA and HOHO string with the numbers read from paste. This creates $t which is then outputted.




Using only awk, and assuming that the input is a single line only:



awk '
for (i = 1; i <= 3; ++i)
t = $0;
sub("HAHA", i, t)
sub("HOHO", 4+i, t)
print t

' file



A shell loop for bash, mimicking the awk code above:



read template <file

for (( i = 1; i <= 3; ++i )); do
t=$template/HAHA/$i
t=$t/HOHO/$((i+4))
printf '%sn' "$t"
done





share|improve this answer

























  • You said 'Without calling an external utility' - does this mean it runs faster than calling sed?

    – LittleG
    Mar 4 at 21:11






  • 1





    @LittleG Calling an external utility, like sed, in a loop is slow in comparison to using only built-in utilities in a loop, yes.

    – Kusalananda
    Mar 4 at 21:45













2












2








2







Without calling an external utility for each line produced:



read template <file

paste <(seq 1 3) <(seq 5 7) |
while read x y; do
t=$template/HAHA/$x
t=$t/HOHO/$y
printf '%sn' "$t"
done >new.txt


This first reads the line from the file called file into $template. It then constructs a two-column input for a while read loop. The input to the loop is the two columns of numbers from seq.



In the loop, some bash-specific substitutions are performed on the value of $template to replace the HAHA and HOHO string with the numbers read from paste. This creates $t which is then outputted.




Using only awk, and assuming that the input is a single line only:



awk '
for (i = 1; i <= 3; ++i)
t = $0;
sub("HAHA", i, t)
sub("HOHO", 4+i, t)
print t

' file



A shell loop for bash, mimicking the awk code above:



read template <file

for (( i = 1; i <= 3; ++i )); do
t=$template/HAHA/$i
t=$t/HOHO/$((i+4))
printf '%sn' "$t"
done





share|improve this answer















Without calling an external utility for each line produced:



read template <file

paste <(seq 1 3) <(seq 5 7) |
while read x y; do
t=$template/HAHA/$x
t=$t/HOHO/$y
printf '%sn' "$t"
done >new.txt


This first reads the line from the file called file into $template. It then constructs a two-column input for a while read loop. The input to the loop is the two columns of numbers from seq.



In the loop, some bash-specific substitutions are performed on the value of $template to replace the HAHA and HOHO string with the numbers read from paste. This creates $t which is then outputted.




Using only awk, and assuming that the input is a single line only:



awk '
for (i = 1; i <= 3; ++i)
t = $0;
sub("HAHA", i, t)
sub("HOHO", 4+i, t)
print t

' file



A shell loop for bash, mimicking the awk code above:



read template <file

for (( i = 1; i <= 3; ++i )); do
t=$template/HAHA/$i
t=$t/HOHO/$((i+4))
printf '%sn' "$t"
done






share|improve this answer














share|improve this answer



share|improve this answer








edited Mar 4 at 15:35

























answered Mar 4 at 8:36









KusalanandaKusalananda

139k17259429




139k17259429












  • You said 'Without calling an external utility' - does this mean it runs faster than calling sed?

    – LittleG
    Mar 4 at 21:11






  • 1





    @LittleG Calling an external utility, like sed, in a loop is slow in comparison to using only built-in utilities in a loop, yes.

    – Kusalananda
    Mar 4 at 21:45

















  • You said 'Without calling an external utility' - does this mean it runs faster than calling sed?

    – LittleG
    Mar 4 at 21:11






  • 1





    @LittleG Calling an external utility, like sed, in a loop is slow in comparison to using only built-in utilities in a loop, yes.

    – Kusalananda
    Mar 4 at 21:45
















You said 'Without calling an external utility' - does this mean it runs faster than calling sed?

– LittleG
Mar 4 at 21:11





You said 'Without calling an external utility' - does this mean it runs faster than calling sed?

– LittleG
Mar 4 at 21:11




1




1





@LittleG Calling an external utility, like sed, in a loop is slow in comparison to using only built-in utilities in a loop, yes.

– Kusalananda
Mar 4 at 21:45





@LittleG Calling an external utility, like sed, in a loop is slow in comparison to using only built-in utilities in a loop, yes.

– Kusalananda
Mar 4 at 21:45

















draft saved

draft discarded
















































Thanks for contributing an answer to Unix & Linux Stack Exchange!


  • 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%2funix.stackexchange.com%2fquestions%2f504187%2fhow-do-i-replace-words-with-increasing-numbers%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

How to check contact read email or not when send email to Individual?

Displaying single band from multi-band raster using QGIS

How many registers does an x86_64 CPU actually have?