Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

create blank file

Status
Not open for further replies.

rhnaeco

Technical User
Aug 11, 2005
45
i know this may seem like an obvious question, but i am trying to create a blank text file from the command line (so that it doen't open), because i want to send continuous data into it from a loop ( i.e the first command is >> file.dat.
at the moment i am using :
$ echo > file.dat

which leaves a blank line at the beginning once data arrives.
i have tried just using >> to create the file but it produces an error for some of my input files so it seems easier to just make a blank file (which has a different name depending on the input data file).
I have also tried touch, but again i want to overwrite if the name exists.
thanks in advance
 
i knew it'd be simple! thanks!
 
Hi

Code:
echo -n > file.txt      [gray]# man echo : [b]-n[/b] do not output the trailing newline[/gray]
cp /dev/null file.txt   [gray]# DOS habit ( copy null file.txt )[/gray]

Feherke.
 
The ">file.txt" won't work in c-shell (invalid null command) but other suggestions will.

Also some unix's echo commands don't understand -n option (check your man page), in that case:

echo "\c" >file.txt

to suppress the trailing newline...


HTH,

p5wizard
 
'touch', being created for this specific purpose is, I think, the cleanest solution.

-Haben sie fosforos?
-No tiengo caballero, but I have un briquet.
 
This works for csh...
Code:
:> file.dat
It also works in other shells too. The ":" is the "null command".

Hope this helps.
 
Hi

A question : why to create an empty file, then append to it from a loop ? If your code looks like this :
Code:
for i in `seq 10`; do
  echo "$i*$i=$((i*i))" >> file.dat
done

Try to change it like this :
Code:
for i in `seq 10`; do
  echo "$i*$i=$((i*i))"
done > file.dat

The second is faster and, at least for me, easyer to maintain.

Feherke.
 
- or -

Code:
logfile=/tmp/log.txt

rm -f ${logfile}

any command >>${logfile}

any other command >>${logfile}

while condition_cmd
do
 loop command
done >>${logfile}

The first >> redirection will create the file anew since it was deleted in the beginning of the script.

the rm -f will not complain if the file isn't there, will just silently do nothing in that case.

Note that using fixed names for logfiles basically makes your program single-user/single-task...


HTH,

p5wizard
 
One main reason to empty the file instead of recreating it is the preservation of original permissions/ownerships.

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Feherke, i need to start with a blank file as it is called file$sample.dat, and at least for the time being, something is going horribly wrong with some of the maths in the loop and i need to see the files step by step.
also, i am still very new to scripting, and didn't think of that as a solution for when the maths DOES start working!

thanks to everyone for their solutions, this really is the best forum to come with a computer problem!

rhnaeco
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top