Frage

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

War es hilfreich?

Lösung

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
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top