Bash – file, arrays and output

bashfilestext processing

I have a file that contains employees names, departments and the cities the employee is in, something like:

John
IT
Chicago
Joshua
accounting
New York
Marcy
CEO
Los Angeles
... 

I have to transform this file to this format:

John?IT?Chicago
Joshua?accounting?New York
Marcy?CEO?Los Angeles

using the question mark as a field delimiter

I have done this bash script so far:

fname="mix.txt"
exec<$fname

index=0
while read line ; do
        ARRAYLINES[$index]="$line"
        index=$(($index+1))
done < $fname

NUMBERLINES=$(grep -c "" $fname)
NUMBERLOOP=$(($NUMBERLINES/3))


count=0

for i in $(eval echo {1..$NUMBERLOOP})
do
  one=$(($count+1))
  two=$(($count+2))

  LINE0=${ARRAYLINES[$count]}
  LINE1=${ARRAYLINES[$one]}
  LINE2=${ARRAYLINES[$two]}
  ((count + 3))

  FINAL="$LINE0?$LINE1?$LINE2"
  echo $FINAL >> final.txt
done

The final file has the correct number of lines, but all lines are equal to the first employee data, like

John?IT?Chicago
John?IT?Chicago
John?IT?Chicago
John?IT?Chicago
John?IT?Chicago
John?IT?Chicago
John?IT?Chicago

any clues?

Best Answer

With

(( count + 3 ))

you just adding 3 to $count and discarding the result. Try with

(( count = count + 3 ))
Related Question