bash array substitute the value of nth element
this is the string i want to change :
random|L|N|0|0|1|0|R|0100|T|N|0|0|100|0|1|0|0|0|1|casper cap|N||N|C|0|0000994500|S|0000115500 I named the script foobaz. It greps out a stock called random, and breaks down the pipe delimited string. The only value that i need replaced is the 27th value, or third to last element of this string with value of $2. the stock name is going to be $1 and the price will be $2. I need this price replaced and a new string printed out. Code:
$ /tmp/foobaz random 999999999 |
I'm not exactly sure what you are trying to represent here. But you can just assign a value to an array element directly.
|
To say I am confused is an understatement.
Firstly, I think you have been using the forum long enough to know that you use code tags when displaying data or code. Is the script supposed to be in foobaz? Is the word 'random' a string? What does the original data set look like? From your current output that does not contain '999999999', is this good or bad? Did the substitution work? Lastly, in the code, what are you running sed against? I see no file no string??? |
sorry guys - I am at work and had to do this on the sly
this is the data set : random|L|N|0|0|1|0|R|0100|T|N|0|0|100|0|1|0|0|0|1|casper cap|N||N|C|0|0000994500|S|0000115500 this is the only part of the string that i need to replace : 0000994500 - all the other values need to stay the same. if I just echo out the array it lines up nicely. Code:
#!/bin/bash |
Replace with what and how many times? Can't you simply do it like this:
Code:
~$ sed -e 's/0000994500/<string>/g' Code:
~$ awk -v val=${ADDR[27]} 'BEGIN{FS="|"}; {gsub(/val/,"<string>",$27); print $0}' infile.txt |
Hers' one using cut
Code:
t='random|L|N|0|0|1|0|R|0100|T|N|0|0|100|0|1|0|0|0|1|casper cap|N||N|C|0|0000994500|S|0000115500' |
hmmm ... whilst all the posts above would seem ample, I am curious why you do not simply replace the array item as you need?
If we assume your code works as it stands and each item of the pipe separated string is now an individual part of the ADDR array, can't you simply: Code:
ADDR[26]='new info' |
this is the sting i am working with :
Code:
random|L|N|0|0|1|0|R|0100|T|N|0|0|100|0|1|0|0|0|1|casper cap|N||N|C|0|0000994500|S|0000115500 and yeah - Grail the substitution works !!!! Code:
#!/bin/bash Code:
random|L|N|0|0|1|0|R|0100|T|N|0|0|100|0|1|0|0|0|1|casper|cap|N||N|C|0|9999999999|S|0000115500 Keep in mind - i know that i could use awk fields - but want to understand bash arrays. "If it doesn't challenge you, it doesn't change you!" |
Ok ... I get you now. So first off, you do know that your for loop is pointless?? Firstly you never use the 'i' variable and second if you know which element to replace you do not need to loop
over anything. So the easy answer is, use IFS more: Code:
#!/bin/bash |
this worked great Grail
I wanted so bad to use the arrays in bash, but alas, it was not to be true. |
Quote:
The statement ADDR=( $stocks ) loads the array ADDR with the values in ${stocks}, delimited by the pipe symbol. |
If you hadn't noticed, the secret is that the '*' array expansion element uses the first character of IFS as its delimiter when printing out the list (be sure to quote it).
So setting IFS to the pipe globally means that it both splits the string into the array and prints the updated array in the same way. arrays BTW, grail's code simply used an unquoted variable inside an "arr=()" pattern to set the array. The expanded parameter is word-split according to the current IFS setting, which is then used to set the array. However I would point out that there's a minor risk to this. Since shell globbing is also applied on unquoted parameters, some character in the input could potentially expand into a list of files existing in the PWD, which would also become part of the array. This can be avoided by also using set +f to turn off glob expansion temporarily. Or you can continue to use read instead, which I personally recommend as the cleaner option. |
All times are GMT -5. The time now is 07:23 AM. |