Doug
Doug

Reputation: 1316

Unix C Shell Scripting printf help

Attempting to print out a list of values from 2 different variables that are aligned correctly.

foreach finalList ($correctList $wrongList)
printf "%20s%s\n" $finalList
end

This prints them out an they are aligned, but it's one after another. How would I have it go through each item in each list and THEN go to a new line?

I want them to eventually appear like this:

Correct    Incorrect
Good1      Bad1
Good2      Bad2
Good3      Bad3

Good comes from correctList Bad comes from wrongList

Getting rid of \n makes it Like this:

Good1     Bad1    Good2    Bad2

I just want 2 columns.

Upvotes: 2

Views: 18346

Answers (3)

mpez0
mpez0

Reputation: 2883

I believe the pr(1) command with the -m option will help do what you want. Look at its man page to eliminate the header/trailer options and set the column widths.

Also, I recommend you not use the C-Shell for scripting; you'll find the sh-syntax shells (sh, bash, ksh, etc) are more consistent and much easier to debug.

Upvotes: 0

Robert Gamble
Robert Gamble

Reputation: 109022

You can iterate over both lists at the same time like this:

# Get the max index of the smallest list
set maxIndex = $#correctList
if ( $#wrongList < $#correctList ) then
  set maxIndex = $#wrongList
endif

set index = 1
while ($index <= $maxIndex)
  printf "%-20s %s\n" "$correctList[$index]" "$wrongList[$index]"
  @ index++
end

Upvotes: 5

Ray Tayek
Ray Tayek

Reputation: 10003

try getting rid of the \n

Upvotes: 0

Related Questions