James
James

Reputation: 124

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?

Upvotes: 0

Views: 2477

Answers (2)

c00kiemon5ter
c00kiemon5ter

Reputation: 17604

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"

Upvotes: 5

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 798606

Append it afterwards.

echo >> "$1 3.txt"

Upvotes: 0

Related Questions