tags:

views:

44

answers:

3

Duplicate of prepend to a file one liner shell? .

I trying to put my paste to the very first line without opening an editor similarly as but for the reverse direction

echo Masi >> file

How can you put data to the first line in terminal without opening an editor?

For instance,

I have a file

M
M
M

I want to put Masi to my file such that

Masi
M
M
M

without opening an editor in terminal.

+1  A: 

You could try:

mv file temp_file
echo "Masi" > file
cat temp_file >> file
rm temp_file

Write a script if you have to do it a lot...


Or I believe you could do it with ed. You'll have to decide for yourself if you think that counts as "opening an editor"...

dmckee
A: 

I think this question is the same as this older question: "prepend to a file one liner shell"

Daft Viking
+1  A: 

Doing it with ed:

#!/bin/sh
/bin/ed $1 <<__EOT__
1i
Masi
.
wq
__EOT__

Technically, this is firing up an editor, but it's non-interactive, so may meet your criteria.

Alnitak