2

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

3 Answers 3

3
printf "%d\n" "$G" >>$out.txt

or

echo "$G" >>$out.txt

> will overwrite your whole file, while >> will append to the file

Usage:

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
Sign up to request clarification or add additional context in comments.

Comments

0

The easiest way to accomplish your task is to use the bash builtin printf command. Note: this is bash builtin printf, not /usr/bin/printf.

for ((GPCount=0; GPCount< $GPCnt; GPCount++))  
do
printf -v GPArr[$GPCount] "%d" $(sed -n '2p' $file.txt|cut -d" " -f7)
done
printf "%d\n" "${GPArr[@]}" >$out2.txt

Give it a try.

Comments

0

Your first loop fails because you seem to be trying to use G as an alias for ${GPArr[$GPCount]}. bash variables don't work that way (although in bash 4.3, you can declare a nameref which would work:

for ((GPCount=0; GPCount< $GPCnt; GPCount++))  
do
    declare -n G=${GPArr[$GPCount]}
    G="$(sed -n '2p' $file.txt|cut -d" " -f7)"
    printf "%d\n" "$G"
done > "$out.txt"

Also, make sure you aren't overwriting the file $out.txt each time through the loop. )

Your second loop just has a typo; you don't prefix a variable assignment with a dollar sign.

for ((GPCount=0; GPCount< $GPCnt; GPCount++))  
do
    # Not $GPArr[$GPCOUNT]=...
    GPArr[$GPCount]="$(sed -n '2p' $file.txt|cut -d" " -f7)"
done
printf "%d\n" "${GPArr[@]}" >$out2.txt

Incidentally, you don't need a loop counter at all; you can append to an array with the += operator.

for ((GPCount=0; GPCount< $GPCnt; GPCount++))  
do
    # Not $GPArr[$GPCOUNT]=...
    GPArr+=("$(sed -n '2p' $file.txt|cut -d" " -f7)")
done
printf "%d\n" "${GPArr[@]}" >$out2.txt

(In any case, it's not clear how the sed call is producing different values each time through the loop.)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.