LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Increasing the array index inside while loop (https://www.linuxquestions.org/questions/programming-9/increasing-the-array-index-inside-while-loop-4175582397/)

Sayan Acharjee 06-16-2016 12:39 AM

Increasing the array index inside while loop
 
Hi

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?

HMW 06-16-2016 02:15 AM

Hi!

It would be better if you kept this in the original thread for context:
http://www.linuxquestions.org/questi...4/#post5560608

But, you are on the right track!

First of all, this is not correct syntax:
Code:

cc=$arr[1]
To get index 1 in a Bash array, do:
Code:

cc=${arr[1]}
Secondly, you need to update this index, use your counter!

Best regards,
HMW

grail 06-16-2016 04:09 AM

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?

Sayan Acharjee 06-16-2016 05:55 AM

Quote:

Originally Posted by HMW (Post 5561697)
Hi!

It would be better if you kept this in the original thread for context:
http://www.linuxquestions.org/questi...4/#post5560608

But, you are on the right track!

First of all, this is not correct syntax:
Code:

cc=$arr[1]
To get index 1 in a Bash array, do:
Code:

cc=${arr[1]}
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 (Post 5561726)
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 "+")

Sayan Acharjee 06-16-2016 06:01 AM

Okay I made it work following what HMW suggested,

Code:

declare -a arr;
arr=("$@");

count=0;
while read q
do
cc=${arr[1]};
sed -i "s/${arr[$count]}/$q/g" test
(( count ++ ))
done < bcd

Thank you both.

HMW 06-16-2016 06:15 AM

Quote:

Originally Posted by Sayan Acharjee (Post 5561759)
Okay I made it work following what HMW suggested,

Code:

declare -a arr;
arr=("$@");

count=0;
while read q
do
cc=${arr[1]};
sed -i "s/${arr[$count]}/$q/g" test
(( count ++ ))
done < bcd

Thank you both.

Awesome, you solved it!

Here are a few suggestions:
You don't need this variable at all since you don't use it:
Code:

cc=${arr[1]};
I would also suggest that you ALWAYS use read with the -r option, like so:
Code:

while read -r line; do
Finally, why do you end your lines with ';' (a semicolon). In Bash, you don't need to.

Best regards,
HMW

ntubski 06-16-2016 06:33 AM

You could also do away with the counter by using the positional args, "$@", directly:

Code:

while read -r q
do
    sed -i "s/$1/$q/g" test
    shift
done < bcd


Sayan Acharjee 06-16-2016 06:38 AM

Quote:

Originally Posted by HMW (Post 5561763)
Awesome, you solved it!

Here are a few suggestions:
You don't need this variable at all since you don't use it:
Code:

cc=${arr[1]};
I would also suggest that you ALWAYS use read with the -r option, like so:
Code:

while read -r line; do
Finally, why do you end your lines with ';' (a semicolon). In Bash, you don't need to.

Best regards,
HMW

Yes, I know. I just forgot to delete that variable before posting here.


Quote:

Originally Posted by ntubski (Post 5561771)
You could also do away with the counter by using the positional args, "$@", directly:

Code:

while read -r q
do
    sed -i "s/$1/$q/g" test
    shift
done < bcd



Great tip. Thanks!!

grail 06-16-2016 09:54 AM

Quote:

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


All times are GMT -5. The time now is 10:22 AM.