I have an array of element in one variable. I have to print all the values of the array into a file. Initially, I read the values of an array from many files using sed and cut. I have print those values(array) into a file. When I use print statement within loop, the values are overwritten. Is there any other way to print all the elements into a file using commands like awk or cat???
Here is the code which I tried
for ((GPCount=0; GPCount< $GPCnt; GPCount++))
do
G=${GPArr[$GPCount]}
G="$(sed -n '2p' $file.txt|cut -d" " -f7)"
printf "%d\n" "$G" >$out.txt
done
I know if I use this in loop it will overwrite, so I tried another block of code
for ((GPCount=0; GPCount< $GPCnt; GPCount++))
do
$GPArr[$GPCount]="$(sed -n '2p' $file.txt|cut -d" " -f7)"
done
printf "%d\n" "${GPArr[@]}" >$out2.txt
there is no output and I am getting error( its giving the value but it couldnt direct the sed o/p to array variable
./test.sh: line 33: =33: command not found
./test.sh: line 33: =39: command not found
./test.sh: line 33: =45: command not found
If I modify the code like this
for ((GPCount=0; GPCount< $GPCnt; GPCount++))
do
G="$(sed -n '2p' $file.txt|cut -d" " -f7)"
done
printf "%d\n" "${GPArr[@]}" >$out2.txt
I am getting 0 as a output in the text file. I need the o/p like this in my text file
33
39
45
Kindly help me in this regard. Thanks in advance