Report printing

Hi. i am trying to print the contents of my text file with delimiter : separating them.

6:15:ABC ABD:ABB ABE:
2:20:AEE ABH:ACC AAA:

i have completed my header for the report but i am having problem with my contents. i am trying to read in the data with the code below.

{ while read 'num1:num2:alpha1:alpha2:'; do
print "%-12s%12d%12d%12d\n" "$num1" "$num2 "$alpha1" "$alpha2"
done } < $file

it tells me my

'num1:num2:alpha1:alpha2:' 

not a valid identifier

Perhaps you need something more like:

while IFS=: read num1 num2 alpha1 alpha2 junk; do
  printf "%-12s%12d%12d%12d\n" "$num1" "$num2" "$alpha1" "$alpha2"
done < $file

Notes:

  • I missed where "$file" was set, but I presume it is
  • Changed "print" to "printf"
  • Added " after $num2
  • It's also useful to add "junk" after the last "while read" field, in case there are additional fields you don't care about, otherwise they become part of the last field read.

Thanks for your reply. Sorry for the mistake as i typed it out instead of copying. $file was set already in my program. i get an error syntax error for my while line when i tried your code. it says read: 6:15:ABC ABD:ABB ABE: syntax error in expression ( error token is "6")

Please tell me which shell you are using (please don't tell me it's C-Shell), and paste your exact script and input file (if possible!)

edit: seems to be Bash:

$ strings $(which bash) | grep -i token
%s%s%s: %s (error token is "%s")

edit 2: You need to change the formatting from "%d" to "%s" - these are strings, not numbers.

Slight change to Scott's solution:

printf "%-12d%12d%12s%12s\n" "$num1" "$num2" "$alpha1" "$alpha2"

This is assuming that your actual data is as per the sample shown.
It's always better to use %s instead of %d when you are unsure about the data.

Hi. its bash that im using. the error is still the same after changing my code. the error seems to happen when i start reading the 1st line.

edit: Hi scott. May i know what do i do with the code that u have provided?

$ strings $(which bash) | grep -i token
%s%s%s: %s (error token is "%s")

We're not going to second guess what you've done. Please post the code.

edit: You need do nothing with that code. It was only to find in which shell the string "error token" could be found.

Hi. This is what i did.

declare -rx file="alphanum.txt
declare -i alpha1
declare -i alpha2
declare -i num1
declare -i num2

while IFS=: read num1 num2 alpha1 alpha2 junk; do
printf "%-12d%12d%12s%12s\n" "$num1" "$num2" "$alpha1" "$alpha2" 
done < $file

just found out what went wrong. i declare -i alpha1 and alpha2.

Sorry, I should also have asked that you to say what "$file" is set to, and post the contents for that file.

And there's no point in using %d unless you plan to format it specially, i.e. with leading zeros, so change those to %s.

Hi. Sorry. i left out my declaration. i have edited my code posted already.

Yes, please include all relevant code. It could save people time :wink:

And where would the closing quote to this be:

declare -rx file="alphanum.txt