printf “columns” and newline

In my program I'm trying to set up column headers for a file that I will be adding to throughout the script.

The line I use to create my headers is:

printf '%-10s' "csci" "csc" "Line #" "File Name" > "$1 3.txt"

This creates the headers just like I want, but when I go to add the next line it's added to the end of this line instead of the next one.

I've been trying to put a newline in the header creation line so the next line that gets added will be the empty line but all of my attempts to add a newline has gotten strange results (or ones I just don't want).

I've tried adding the \n like this: '%-10s\n', but this only makes my headers show up on separate lines (which I expected, but had to try this anyway).

I then tried: printf '%-10s' "csci" "csc" "Line #" "File Name" "\n"> "$1 3.txt", but this only printed out the \n as is without actually giving me a newline.

So, if my question isn't obvious by now, how can I get a newline in there?


either add an echo or printf statement after you print the columns, so that a new line is added:

(printf '%-10s' "csci" "csc" "Line #" "File Name"; echo) > "$1 3.txt"
# or
(printf '%-10s' "csci" "csc" "Line #" "File Name"; printf '\n') > "$1 3.txt"

or define the whole line for printf and add the \n on the end:

printf '%-10s %-10s %-10s %-10s\n' "csci" "csc" "Line #" "File Name" > "$1 3.txt"

on Bash this should work too, though it appends some whitespace on the end of the new line:

printf '%-10s' "csci" "csc" "Line #" "File Name" $'\n' > "$1 3.txt"

Need Your Help

right-align text inside an RTF table

.net table rtf alignment

I am trying to write a small formatted test in RTF using the C# RichTextEdit control, but I can't seem to get it right. This is how I want to to look:

Unable to call a template member function of a template class from another template class

c++ templates

I'm getting compiler errors when attempting to access a template member function of a template class from within another template class. A function call to getSubmatrix results in the compiler warn...