Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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 am a newbie in Bash, but 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? thanks

share|improve this question

2 Answers 2

up vote 2 down vote accepted

With

(( count + 3 ))

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

(( count = count + 3 ))
share|improve this answer
    
duuuuh... I knew I was missing something! Thanks!!!!!! –  Digital Robot Nov 29 '11 at 18:24

Normally I'm not a fan of posting "actually you should do it this way" answers, but somebody already addressed the error in your script and I can't stop myself -- you're making this way too hard. This is much easier with a tool like sed or awk. For example, in sed:

$ sed 'N; N; s/\n/?/g' /path/to/your/file

This reads three lines (one by default, the other two because of N; N;), and replaces newlines with question marks (s/\n/?/g). It will output the converted version of the file; if you want to replace the contents in-place use the -i flag

share|improve this answer
    
amazing. As I said I am a newbie in Bash. I know a lot of stuff but I am far from having a really deep knowledge. Thanks for this amazing command! I knew sed, but I was not aware it could do stuff like that in one line!!!!!!!!!!!!!!!! –  Digital Robot Nov 29 '11 at 18:36
1  
Why the address? Works for me without too. –  manatwork Nov 29 '11 at 18:36
    
@manatwork Oh, I suppose it would, since I'm consuming the lines immediately. Edited, thanks –  Michael Mrozek Nov 29 '11 at 18:39
    
anyway it is giving me an error on the tilde, saying ": invalid command code ~ –  Digital Robot Nov 29 '11 at 18:43
1  
@HananN. Global; it replaces all occurrences instead of just the first. echo aaa | sed 's/a/b/' would output "baa"; echo aaa | sed 's/a/b/g' outputs "bbb" –  Michael Mrozek Nov 29 '11 at 21:33

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.