opmn
opmn

Reputation: 11

How can I attach a string to the beginning or end of a file without using pipe or redirection in shell script?

And the initial file is empty. Is there any command? I tried sed but it does not work on an empty file. Any ideas?

Upvotes: 0

Views: 129

Answers (2)

Dennis Williamson
Dennis Williamson

Reputation: 360345

I'm assuming that this is for a bar bet or something like that.

Here are some options:

shuf is a part of Linux coreutils. It can accept a string and an output file as arguments.

shuf -e -o outputfile 'this is some text'

You could also use that to output only a newline then use sed -i.

This does something similar. The dd creates a file with one zero byte, then sed writes a string to it.

dd if=/dev/zero of=outputfile bs=1 count=1
sed -i 's/.*/foo\n/' outputfile

Of course there's Perl:

perl -e 'open $fh, ">", "outputfile"; print $fh "foo\n"'

Upvotes: 1

darklion
darklion

Reputation: 1055

You could use the line editor:

ed tmp.tmp << EOC
a
PUT STRING HERE
.
w
q
EOC

Upvotes: 2

Related Questions