LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-16-2016, 12:39 AM   #1
Sayan Acharjee
Member
 
Registered: Feb 2010
Location: Chennai, India
Distribution: Manjaro
Posts: 624

Rep: Reputation: 64
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?
 
Old 06-16-2016, 02:15 AM   #2
HMW
Member
 
Registered: Aug 2013
Location: Sweden
Distribution: Debian, Arch, Red Hat, CentOS
Posts: 773
Blog Entries: 3

Rep: Reputation: 369Reputation: 369Reputation: 369Reputation: 369
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
 
1 members found this post helpful.
Old 06-16-2016, 04:09 AM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,008

Rep: Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193
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?
 
Old 06-16-2016, 05:55 AM   #4
Sayan Acharjee
Member
 
Registered: Feb 2010
Location: Chennai, India
Distribution: Manjaro
Posts: 624

Original Poster
Rep: Reputation: 64
Quote:
Originally Posted by HMW View Post
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 View Post
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.
 
Old 06-16-2016, 06:01 AM   #5
Sayan Acharjee
Member
 
Registered: Feb 2010
Location: Chennai, India
Distribution: Manjaro
Posts: 624

Original Poster
Rep: Reputation: 64
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.

Last edited by Sayan Acharjee; 06-16-2016 at 06:05 AM.
 
Old 06-16-2016, 06:15 AM   #6
HMW
Member
 
Registered: Aug 2013
Location: Sweden
Distribution: Debian, Arch, Red Hat, CentOS
Posts: 773
Blog Entries: 3

Rep: Reputation: 369Reputation: 369Reputation: 369Reputation: 369
Quote:
Originally Posted by Sayan Acharjee View Post
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
 
Old 06-16-2016, 06:33 AM   #7
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,784

Rep: Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083
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
 
1 members found this post helpful.
Old 06-16-2016, 06:38 AM   #8
Sayan Acharjee
Member
 
Registered: Feb 2010
Location: Chennai, India
Distribution: Manjaro
Posts: 624

Original Poster
Rep: Reputation: 64
Quote:
Originally Posted by HMW View Post
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 View Post
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!!

Last edited by Sayan Acharjee; 06-16-2016 at 06:39 AM.
 
Old 06-16-2016, 09:54 AM   #9
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,008

Rep: Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193
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
 
  


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] C: Using an array element as index to another array? stf92 Programming 1 08-23-2015 11:26 PM
Bash script issue (for loop inside a loop) Mperonen Programming 3 08-08-2013 02:14 AM
Sleep inside for loop inside while loop causing issues. TheOnlyQ Programming 13 12-19-2012 12:59 PM
[SOLVED] A question about for loop (increasing two parameters) yaron.kadem Linux - Server 5 12-28-2011 02:25 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

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