0

I need to add a header to an existing file in unix as a part of my requirement. For that I used ed text editor got from this discussion

How can I add a line to a file in a shell script?

This is the code I used

printf '0a\n%s\n.\nw\n',$header, | ed "./runtime/"$outputFileName

I obtained the value of $header from another file which is

"header 1","header 2","header 3"

After executing the script, When I check the output file the very first line of the file, contain only this line

1","header

How to make the whole line to get added in the output file? please help. Thanks in advance.

PS:I am using Sun Solaris

1 Answer 1

4

be sure to use printf correctly. (1) quote header (2) separate arguments by spaces

echo '"header 1","header 2","header 3"' > test.txt
header="$(cat header.txt)"
printf '0a\n%s\n.\nw\n' "$header"

results in

0a
"header 1","header 2","header 3"
.
w
Sign up to request clarification or add additional context in comments.

1 Comment

By changing from "printf '0a\n%s\n.\nw\n',$header," to "printf '0a\n%s\n.\nw\n' "$header"" code works correctly. Thanks for your input :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.