0

is probably a stupid question, but can't see the reason for while abort.

i have a file like this:

"id": "00000000000000000",
"visibilitystate": 1,
"profilestate": 8
"somethingelse": "abc",
"id": "99999999999999999",
"againsomethingelse": "cba"
"visibilitystate": 0,
"profilestate": 9

one million or more repetitions, number,designation and value can be different between id and id, but id is always different. my first thought was read in a loop, store the value in a array and later insert into mysql-db.

i try this:

set -x
#Data extract array
array=(0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0)

#Control array
array2=(0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0)

while read a; do
    if [[ ${array2[0]} == 1 ]]; then
        awk '/"id":/ { exit 42 }'
        if [[ $? -eq 42 ]]; then
            echo mysql
            array2[0]=0
        fi
    fi
    if [[ ${array2[0]} == 0 ]]; then
        awk '/"id":/ { exit 42 }'
        if [ $? -eq 42 ]; then
            array[0]=`sed -n 's/.*"id":."\(.*\)",.*/\1/p'`
            array2[0]=1
        fi
    fi
done <testf

set +x

after read one line, the loop exit, don't understand why.

output:

+ array=(0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0)
+ array2=(0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0)
+ read a
+ [[ 0 == 1 ]]
+ [[ 0 == 0 ]]
+ awk '/"id":/ { exit 42 }'
+ '[' 42 -eq 42 ']'
++ sed -n 's/.*"id":."\(.*\)",.*/\1/p'
+ array[0]=
+ array2[0]=1
+ read a
+ set +x

can someone help me?

andy
  • 11
  • 3
  • possibly related: http://stackoverflow.com/questions/346445/bash-while-read-loop-breaking-early – pje Oct 15 '12 at 00:11
  • 1
    There is missing information: what are the initial values of array and array2 before entering the loop? Also, what do you want to accomplish? (the while is an implementation detail) – gpoo Oct 15 '12 at 00:52
  • At script begin i create the arrays with 21 values `array=(0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0)` same for array2 – andy Oct 15 '12 at 01:06
  • @andy, comments do terrible things with formatting, and this is important enough that you should [add it to your question](http://stackoverflow.com/posts/12887685/edit) rather than just relying on comments. – ghoti Oct 15 '12 at 01:13
  • And per @gpoo, please do tell us what you are trying to accomplish. This sounds very much like an [XY problem](http://mywiki.wooledge.org/XyProblem). – ghoti Oct 15 '12 at 01:15
  • my target is: get all values from id to id (next id not included) into array, sometimes that are 21 values sometimes fewer, the array have 21 values because its have a 0 for not found values and i can insert it into mysql – andy Oct 15 '12 at 01:23
  • @gpoo,@ghoti i'm sorry, you are right its not a finished question. i edit the question, add the two arrays and add counter reset after mysql. in the script i have more such if's for this post i have reduced it to the basic problem. maybe i should better use case? – andy Oct 15 '12 at 01:41
  • Still you are explaining how do you want to solve your problem instead of explaining the problem. My guess is that you want to parse a file to insert the content into a database, but each column value is stored in a row, but those lines are variable. Is the problem something like what I just explained? – gpoo Oct 15 '12 at 01:44
  • @gpoo i know while read as read line by line, begin at first stop after last. the first line in this test file is id so i get the value for id in array, then according to my understanding the array2 is 0, so i search what for a designation is in this line, on first line its id, i get the value of id in array, running through again, line 2,3,4. line 5 contains next id, so array2 contain 1, search is id contain in the actual line, when line 5 yes, then echo mysql and set array2 to 0. the problem is that according to debug output and test with echo $a the while loop don't read line 2,3,4... – andy Oct 15 '12 at 02:06
  • @gpoo answer on last question, yes exactly. – andy Oct 15 '12 at 02:08
  • @andy: read my answer. In the first `awk` executed (after `[[ ${array2[0]} == 0 ]];`, `awk` read the content of the whole file. So, in the next `while` there is nothing else to read. – gpoo Oct 15 '12 at 02:08
  • @gpoo yes i see it to late, exactly what my problem was. thank you very much! i think awk and sed use only the actual line from loop read, but when they read the whole file i see the problem. sorry i was a little beside me, now I go to sleep :) many thanks again! – andy Oct 15 '12 at 02:18

1 Answers1

2

There are 3 problems in the script. You are using 3 different commands inside the loop that are reading from stdin (awk twice and sed).

When you use awk '/"id":/ { exit 42 }' it reads everything else (the input file). You probably want to apply that command in $a. That would be something like:

echo $a | awk '/"id":/ { exit 42 }'

The second error is the same, but in the next if.

The third error is similar, but in this case when you use sed. You probably want to do something like:

array[0]=`echo $a | sed -n 's/.*"id":."\(.*\)",.*/\1/p'`

By the way, test is a reserved word in bash, you should avoid using it as file name (not related with your problem, though).

gpoo
  • 8,408
  • 3
  • 38
  • 53