[SOLVED] Increasing the array index inside while loop
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
There must be a simple solution but I am finding it elusive,
below is the code:
Code:
declare -a arr;
arr=("$@");
count=0;
while read q
do
cc=$arr[1]
sed -i "s/$cc/$q/g" test
(( count ++ ))
done < bcd.txt
I want the second argument to be used for replace in the sed section when the loop runs for the second time, 3rd argument when loop runs for the 3rd time, etc. Can I increase the array index directly inside sed? e.g., sed -i "s/$arr[1++]/$q/g" or something similar?
How do I do this?
You can also increase your counter inside the call to the array, ie. ++ will work inside []
I am also curious why the previous question was marked as solved but there does not appear to be any solution posted?? Or were you not going to share your new found knowledge?
Secondly, you need to update this index, use your counter!
Best regards,
HMW
This question is not related to that one in the other thread, that's why a new thread.
Quote:
Originally Posted by grail
You can also increase your counter inside the call to the array, ie. ++ will work inside []
I am also curious why the previous question was marked as solved but there does not appear to be any solution posted?? Or were you not going to share your new found knowledge?
I marked it as solved as I didn't require that anymore. Anyway, I tried using ++ inside [] but getting error,
Code:
declare -a arr;
arr=("$@");
count=0;
while read q
do
#cc=${arr[1]};
sed -i "s/${arr[0 ++]}/$q/g" test
(( count ++ ))
done < bcd
./lo.sh: line 19: 0 ++: syntax error: operand expected (error token is "+")
Last edited by Sayan Acharjee; 06-16-2016 at 06:05 AM.
I marked it as solved as I didn't require that anymore.
And that is of course your choice, but the site is supposed to also work by helping future members so when they search and find your question as it is somehow related to what they are doing that
they get an idea from your solution on how their own might be solved.
Quote:
Anyway, I tried using ++ inside [] but getting error,
Which makes a lot of sense when you place spaces between the variable name and the ++ operator along with the fact that you then tried to use this operator on a number, ie. zero and not on a variable.
So the working option would have been:
Code:
sed -i "s/${arr[count++]}/$q/g" test
Of course, the use of the positional parameters would seem the most straight forward approach
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.