Need help in copying the file once

hi all ,
i had a following problem in my script

filename=oas/data/output.txt
printf"content1" >> $filename
printf"content2" >> $filename
printf"content3" >> $filename
printf"content4" >> $filename
printf"content5" >> $filename
printf"content6" >> $filename

my contents are different like based on condition iam adding them to file .. i had a problem like if i am running the script for first time iam getting correct output,but if iam running script for second time it getting append the same for the existing file like its appearing twice... if i use > instead of >> operand then iam getting the output as content6 wat should i do please guide me

thanks in advance
hemanthsaikumar.

At the start of the script, clear/remove the file before appending to it. Or, make all the printf one compound command (using e.g. {...; }) and redirect that output using >.

You can do someting like this :

filename=oas/data/output.txt
{
  printf "%s\n" "content1"
  printf "%s\n" "content2"
  printf "%s\n" "content3"
  printf "%s\n" "content4"
  printf "%s\n" "content5"
  printf "%s\n" "content6"
} >"$filename"

or

printf "%s\n" "content1" "content2" "content3" "content4" "content5" "content6" >"$filename"

or

cat << "EOF" > "$filename"
content1
content2
content3
content4
content5
content6
EOF

try

filename=oas/data/output.txt
rm $filename
printf"content1" >> $filename
printf"content2" >> $filename
printf"content3" >> $filename
printf"content4" >> $filename
printf"content5" >> $filename
printf"content6" >> $filename

Or:

filename=oas/data/output.txt
printf"content1" > $filename
printf"content2" >> $filename
printf"content3" >> $filename
printf"content4" >> $filename
printf"content5" >> $filename
printf"content6" >> $filename

hi all,
thanks for the reply go it made the following change

printf"content1" > $filename
printf"content2" >> $filename
printf"content3" >> $filename
printf"content4" >> $filename
printf"content5" >> $filename
printf"content6" >> $filename

just replaced the first operand its working

i had one more query like

a=qw
b=rter
c=fdfd
curency=1000
printf"${curency} $a $b $c" > filename

can i have printf statement that can change the currency from 1000 to 1,000 like it should convert the number to currency format ..?

@hemanthsaikumar. Please open a new thread for a new question..

1 Like