How to change string values to index value to use in Array?
Clash Royale CLAN TAG#URR8PPP
up vote
0
down vote
favorite
I tried to change an string to index to use for an array but I have been unable to get it to work.
This is my file
$ cat file1.txt
101,Harish,BAN
102,Srinu,HYD
And this code:
#!/bin/bash
IFS=','
while read line
do
DELIM_REMOVE=`echo $line|sed 's/,/ /g'`
V=($DEL_REMOVE)
echo $DELIM_REMOVE
for i in "$!V[@]"; do
printf 'V[%s] = %sn' "$i" "$V[i]"
echo "$V[i]"
done
done < /home/ec2-user/file1.txt
echo "$V[i]"
I also need to use the dynamically generated variables in loop to another loop.
linux shell-script
add a comment |
up vote
0
down vote
favorite
I tried to change an string to index to use for an array but I have been unable to get it to work.
This is my file
$ cat file1.txt
101,Harish,BAN
102,Srinu,HYD
And this code:
#!/bin/bash
IFS=','
while read line
do
DELIM_REMOVE=`echo $line|sed 's/,/ /g'`
V=($DEL_REMOVE)
echo $DELIM_REMOVE
for i in "$!V[@]"; do
printf 'V[%s] = %sn' "$i" "$V[i]"
echo "$V[i]"
done
done < /home/ec2-user/file1.txt
echo "$V[i]"
I also need to use the dynamically generated variables in loop to another loop.
linux shell-script
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I tried to change an string to index to use for an array but I have been unable to get it to work.
This is my file
$ cat file1.txt
101,Harish,BAN
102,Srinu,HYD
And this code:
#!/bin/bash
IFS=','
while read line
do
DELIM_REMOVE=`echo $line|sed 's/,/ /g'`
V=($DEL_REMOVE)
echo $DELIM_REMOVE
for i in "$!V[@]"; do
printf 'V[%s] = %sn' "$i" "$V[i]"
echo "$V[i]"
done
done < /home/ec2-user/file1.txt
echo "$V[i]"
I also need to use the dynamically generated variables in loop to another loop.
linux shell-script
I tried to change an string to index to use for an array but I have been unable to get it to work.
This is my file
$ cat file1.txt
101,Harish,BAN
102,Srinu,HYD
And this code:
#!/bin/bash
IFS=','
while read line
do
DELIM_REMOVE=`echo $line|sed 's/,/ /g'`
V=($DEL_REMOVE)
echo $DELIM_REMOVE
for i in "$!V[@]"; do
printf 'V[%s] = %sn' "$i" "$V[i]"
echo "$V[i]"
done
done < /home/ec2-user/file1.txt
echo "$V[i]"
I also need to use the dynamically generated variables in loop to another loop.
linux shell-script
linux shell-script
edited Dec 1 at 7:38
Rui F Ribeiro
38.5k1479128
38.5k1479128
asked Dec 1 at 6:13
Harish a
233
233
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
up vote
1
down vote
accepted
Don't use shell loops to process text, use a text processing utility.
awk -F, 'for (i = 0; i < NF; i++) printf "v[%d] = %sn", i, $(i+1)' < file1.txt
If you have to use a bash
loop, then it would make more sense to write it as:
while IFS=, read -ra v; do
for i in "$!v[@]"; do
printf 'v[%d] = %sn' "$i" "$v[i]"
done
done < file1.txt
With the caveat that if the last field is empty, it will be skipped.
It is working fine. Thank you. Need one more help, unable to use Variables outside of the loop.
– Harish a
Dec 1 at 9:59
@Harisha, it does not make sense to use variables outside of the loop, that variable is reset at each iteration, and we get out of the loop whenread
fails (on end-of-file) at which point$v
is set to an empty array. If you want to access the array for the last line of input, you'd need to make a copy within the loop (likelast_v=("$v[@]")
.
– Stéphane Chazelas
Dec 1 at 10:02
Can I take any other string like $DATA_V instead of $v. Because I have to use different Variables like DATA_V or DEFAULT_V to call different rows in different files, to store file1.txt rows in $DATA_V[@] and file2.txt in $DEFAULT_V[@], unable to complete my script. Please suggest me.
– Harish a
Dec 1 at 11:35
@Harisha, I don't understand your question. If you need a two-dimensional array (one for lines, one for fields in the lines), you'll need theksh93
shell,bash
only supports one-dimensional (sparse) arrays. Or again, use a proper text-processing tool likeawk
orperl
.
– Stéphane Chazelas
Dec 1 at 12:58
Actually, I am looking for Increment variables dynamically. Please refer the like.unix.stackexchange.com/posts/484689/revisions
– Harish a
Dec 1 at 16:21
|
show 1 more comment
up vote
0
down vote
There are several issues with your script.
- The name of the variable in
V=($DEL_REMOVE)
should beV=($DELIM_REMOVE)
- You are setting IFS to a comma (
,
) but you are removing the comma withsed
.
By doing those two changes your script starts to do something reasonable.
Making some other changes, your script becomes:
#!/bin/bash
IFS=' ' # Use the space to split
set -f # Avoid globing of values with *,? or
while read -r line # read the variable without removing backslash
do
v=( $line//,/ ) # Convert to an array by splitting with the shell.
for i in "$!v[@]"; do
printf 'v[%s] = %sn' "$i" "$v[i]"
done
done < file1.txt
Which will print:
v[0] = 101
v[1] = Harish
v[2] = BAN
v[0] = 102
v[1] = Srinu
v[2] = HYD
Is that what you expected it to do?
Thank you. It is working.
– Harish a
Dec 1 at 10:37
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
Don't use shell loops to process text, use a text processing utility.
awk -F, 'for (i = 0; i < NF; i++) printf "v[%d] = %sn", i, $(i+1)' < file1.txt
If you have to use a bash
loop, then it would make more sense to write it as:
while IFS=, read -ra v; do
for i in "$!v[@]"; do
printf 'v[%d] = %sn' "$i" "$v[i]"
done
done < file1.txt
With the caveat that if the last field is empty, it will be skipped.
It is working fine. Thank you. Need one more help, unable to use Variables outside of the loop.
– Harish a
Dec 1 at 9:59
@Harisha, it does not make sense to use variables outside of the loop, that variable is reset at each iteration, and we get out of the loop whenread
fails (on end-of-file) at which point$v
is set to an empty array. If you want to access the array for the last line of input, you'd need to make a copy within the loop (likelast_v=("$v[@]")
.
– Stéphane Chazelas
Dec 1 at 10:02
Can I take any other string like $DATA_V instead of $v. Because I have to use different Variables like DATA_V or DEFAULT_V to call different rows in different files, to store file1.txt rows in $DATA_V[@] and file2.txt in $DEFAULT_V[@], unable to complete my script. Please suggest me.
– Harish a
Dec 1 at 11:35
@Harisha, I don't understand your question. If you need a two-dimensional array (one for lines, one for fields in the lines), you'll need theksh93
shell,bash
only supports one-dimensional (sparse) arrays. Or again, use a proper text-processing tool likeawk
orperl
.
– Stéphane Chazelas
Dec 1 at 12:58
Actually, I am looking for Increment variables dynamically. Please refer the like.unix.stackexchange.com/posts/484689/revisions
– Harish a
Dec 1 at 16:21
|
show 1 more comment
up vote
1
down vote
accepted
Don't use shell loops to process text, use a text processing utility.
awk -F, 'for (i = 0; i < NF; i++) printf "v[%d] = %sn", i, $(i+1)' < file1.txt
If you have to use a bash
loop, then it would make more sense to write it as:
while IFS=, read -ra v; do
for i in "$!v[@]"; do
printf 'v[%d] = %sn' "$i" "$v[i]"
done
done < file1.txt
With the caveat that if the last field is empty, it will be skipped.
It is working fine. Thank you. Need one more help, unable to use Variables outside of the loop.
– Harish a
Dec 1 at 9:59
@Harisha, it does not make sense to use variables outside of the loop, that variable is reset at each iteration, and we get out of the loop whenread
fails (on end-of-file) at which point$v
is set to an empty array. If you want to access the array for the last line of input, you'd need to make a copy within the loop (likelast_v=("$v[@]")
.
– Stéphane Chazelas
Dec 1 at 10:02
Can I take any other string like $DATA_V instead of $v. Because I have to use different Variables like DATA_V or DEFAULT_V to call different rows in different files, to store file1.txt rows in $DATA_V[@] and file2.txt in $DEFAULT_V[@], unable to complete my script. Please suggest me.
– Harish a
Dec 1 at 11:35
@Harisha, I don't understand your question. If you need a two-dimensional array (one for lines, one for fields in the lines), you'll need theksh93
shell,bash
only supports one-dimensional (sparse) arrays. Or again, use a proper text-processing tool likeawk
orperl
.
– Stéphane Chazelas
Dec 1 at 12:58
Actually, I am looking for Increment variables dynamically. Please refer the like.unix.stackexchange.com/posts/484689/revisions
– Harish a
Dec 1 at 16:21
|
show 1 more comment
up vote
1
down vote
accepted
up vote
1
down vote
accepted
Don't use shell loops to process text, use a text processing utility.
awk -F, 'for (i = 0; i < NF; i++) printf "v[%d] = %sn", i, $(i+1)' < file1.txt
If you have to use a bash
loop, then it would make more sense to write it as:
while IFS=, read -ra v; do
for i in "$!v[@]"; do
printf 'v[%d] = %sn' "$i" "$v[i]"
done
done < file1.txt
With the caveat that if the last field is empty, it will be skipped.
Don't use shell loops to process text, use a text processing utility.
awk -F, 'for (i = 0; i < NF; i++) printf "v[%d] = %sn", i, $(i+1)' < file1.txt
If you have to use a bash
loop, then it would make more sense to write it as:
while IFS=, read -ra v; do
for i in "$!v[@]"; do
printf 'v[%d] = %sn' "$i" "$v[i]"
done
done < file1.txt
With the caveat that if the last field is empty, it will be skipped.
answered Dec 1 at 8:03
Stéphane Chazelas
296k54560905
296k54560905
It is working fine. Thank you. Need one more help, unable to use Variables outside of the loop.
– Harish a
Dec 1 at 9:59
@Harisha, it does not make sense to use variables outside of the loop, that variable is reset at each iteration, and we get out of the loop whenread
fails (on end-of-file) at which point$v
is set to an empty array. If you want to access the array for the last line of input, you'd need to make a copy within the loop (likelast_v=("$v[@]")
.
– Stéphane Chazelas
Dec 1 at 10:02
Can I take any other string like $DATA_V instead of $v. Because I have to use different Variables like DATA_V or DEFAULT_V to call different rows in different files, to store file1.txt rows in $DATA_V[@] and file2.txt in $DEFAULT_V[@], unable to complete my script. Please suggest me.
– Harish a
Dec 1 at 11:35
@Harisha, I don't understand your question. If you need a two-dimensional array (one for lines, one for fields in the lines), you'll need theksh93
shell,bash
only supports one-dimensional (sparse) arrays. Or again, use a proper text-processing tool likeawk
orperl
.
– Stéphane Chazelas
Dec 1 at 12:58
Actually, I am looking for Increment variables dynamically. Please refer the like.unix.stackexchange.com/posts/484689/revisions
– Harish a
Dec 1 at 16:21
|
show 1 more comment
It is working fine. Thank you. Need one more help, unable to use Variables outside of the loop.
– Harish a
Dec 1 at 9:59
@Harisha, it does not make sense to use variables outside of the loop, that variable is reset at each iteration, and we get out of the loop whenread
fails (on end-of-file) at which point$v
is set to an empty array. If you want to access the array for the last line of input, you'd need to make a copy within the loop (likelast_v=("$v[@]")
.
– Stéphane Chazelas
Dec 1 at 10:02
Can I take any other string like $DATA_V instead of $v. Because I have to use different Variables like DATA_V or DEFAULT_V to call different rows in different files, to store file1.txt rows in $DATA_V[@] and file2.txt in $DEFAULT_V[@], unable to complete my script. Please suggest me.
– Harish a
Dec 1 at 11:35
@Harisha, I don't understand your question. If you need a two-dimensional array (one for lines, one for fields in the lines), you'll need theksh93
shell,bash
only supports one-dimensional (sparse) arrays. Or again, use a proper text-processing tool likeawk
orperl
.
– Stéphane Chazelas
Dec 1 at 12:58
Actually, I am looking for Increment variables dynamically. Please refer the like.unix.stackexchange.com/posts/484689/revisions
– Harish a
Dec 1 at 16:21
It is working fine. Thank you. Need one more help, unable to use Variables outside of the loop.
– Harish a
Dec 1 at 9:59
It is working fine. Thank you. Need one more help, unable to use Variables outside of the loop.
– Harish a
Dec 1 at 9:59
@Harisha, it does not make sense to use variables outside of the loop, that variable is reset at each iteration, and we get out of the loop when
read
fails (on end-of-file) at which point $v
is set to an empty array. If you want to access the array for the last line of input, you'd need to make a copy within the loop (like last_v=("$v[@]")
.– Stéphane Chazelas
Dec 1 at 10:02
@Harisha, it does not make sense to use variables outside of the loop, that variable is reset at each iteration, and we get out of the loop when
read
fails (on end-of-file) at which point $v
is set to an empty array. If you want to access the array for the last line of input, you'd need to make a copy within the loop (like last_v=("$v[@]")
.– Stéphane Chazelas
Dec 1 at 10:02
Can I take any other string like $DATA_V instead of $v. Because I have to use different Variables like DATA_V or DEFAULT_V to call different rows in different files, to store file1.txt rows in $DATA_V[@] and file2.txt in $DEFAULT_V[@], unable to complete my script. Please suggest me.
– Harish a
Dec 1 at 11:35
Can I take any other string like $DATA_V instead of $v. Because I have to use different Variables like DATA_V or DEFAULT_V to call different rows in different files, to store file1.txt rows in $DATA_V[@] and file2.txt in $DEFAULT_V[@], unable to complete my script. Please suggest me.
– Harish a
Dec 1 at 11:35
@Harisha, I don't understand your question. If you need a two-dimensional array (one for lines, one for fields in the lines), you'll need the
ksh93
shell, bash
only supports one-dimensional (sparse) arrays. Or again, use a proper text-processing tool like awk
or perl
.– Stéphane Chazelas
Dec 1 at 12:58
@Harisha, I don't understand your question. If you need a two-dimensional array (one for lines, one for fields in the lines), you'll need the
ksh93
shell, bash
only supports one-dimensional (sparse) arrays. Or again, use a proper text-processing tool like awk
or perl
.– Stéphane Chazelas
Dec 1 at 12:58
Actually, I am looking for Increment variables dynamically. Please refer the like.unix.stackexchange.com/posts/484689/revisions
– Harish a
Dec 1 at 16:21
Actually, I am looking for Increment variables dynamically. Please refer the like.unix.stackexchange.com/posts/484689/revisions
– Harish a
Dec 1 at 16:21
|
show 1 more comment
up vote
0
down vote
There are several issues with your script.
- The name of the variable in
V=($DEL_REMOVE)
should beV=($DELIM_REMOVE)
- You are setting IFS to a comma (
,
) but you are removing the comma withsed
.
By doing those two changes your script starts to do something reasonable.
Making some other changes, your script becomes:
#!/bin/bash
IFS=' ' # Use the space to split
set -f # Avoid globing of values with *,? or
while read -r line # read the variable without removing backslash
do
v=( $line//,/ ) # Convert to an array by splitting with the shell.
for i in "$!v[@]"; do
printf 'v[%s] = %sn' "$i" "$v[i]"
done
done < file1.txt
Which will print:
v[0] = 101
v[1] = Harish
v[2] = BAN
v[0] = 102
v[1] = Srinu
v[2] = HYD
Is that what you expected it to do?
Thank you. It is working.
– Harish a
Dec 1 at 10:37
add a comment |
up vote
0
down vote
There are several issues with your script.
- The name of the variable in
V=($DEL_REMOVE)
should beV=($DELIM_REMOVE)
- You are setting IFS to a comma (
,
) but you are removing the comma withsed
.
By doing those two changes your script starts to do something reasonable.
Making some other changes, your script becomes:
#!/bin/bash
IFS=' ' # Use the space to split
set -f # Avoid globing of values with *,? or
while read -r line # read the variable without removing backslash
do
v=( $line//,/ ) # Convert to an array by splitting with the shell.
for i in "$!v[@]"; do
printf 'v[%s] = %sn' "$i" "$v[i]"
done
done < file1.txt
Which will print:
v[0] = 101
v[1] = Harish
v[2] = BAN
v[0] = 102
v[1] = Srinu
v[2] = HYD
Is that what you expected it to do?
Thank you. It is working.
– Harish a
Dec 1 at 10:37
add a comment |
up vote
0
down vote
up vote
0
down vote
There are several issues with your script.
- The name of the variable in
V=($DEL_REMOVE)
should beV=($DELIM_REMOVE)
- You are setting IFS to a comma (
,
) but you are removing the comma withsed
.
By doing those two changes your script starts to do something reasonable.
Making some other changes, your script becomes:
#!/bin/bash
IFS=' ' # Use the space to split
set -f # Avoid globing of values with *,? or
while read -r line # read the variable without removing backslash
do
v=( $line//,/ ) # Convert to an array by splitting with the shell.
for i in "$!v[@]"; do
printf 'v[%s] = %sn' "$i" "$v[i]"
done
done < file1.txt
Which will print:
v[0] = 101
v[1] = Harish
v[2] = BAN
v[0] = 102
v[1] = Srinu
v[2] = HYD
Is that what you expected it to do?
There are several issues with your script.
- The name of the variable in
V=($DEL_REMOVE)
should beV=($DELIM_REMOVE)
- You are setting IFS to a comma (
,
) but you are removing the comma withsed
.
By doing those two changes your script starts to do something reasonable.
Making some other changes, your script becomes:
#!/bin/bash
IFS=' ' # Use the space to split
set -f # Avoid globing of values with *,? or
while read -r line # read the variable without removing backslash
do
v=( $line//,/ ) # Convert to an array by splitting with the shell.
for i in "$!v[@]"; do
printf 'v[%s] = %sn' "$i" "$v[i]"
done
done < file1.txt
Which will print:
v[0] = 101
v[1] = Harish
v[2] = BAN
v[0] = 102
v[1] = Srinu
v[2] = HYD
Is that what you expected it to do?
answered Dec 1 at 6:42
Isaac
10.8k11447
10.8k11447
Thank you. It is working.
– Harish a
Dec 1 at 10:37
add a comment |
Thank you. It is working.
– Harish a
Dec 1 at 10:37
Thank you. It is working.
– Harish a
Dec 1 at 10:37
Thank you. It is working.
– Harish a
Dec 1 at 10:37
add a comment |
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.
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f485303%2fhow-to-change-string-values-to-index-value-to-use-in-array%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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