0

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.

Community
  • 1
  • 1
Léo Léopold Hertz 준영
  • 134,464
  • 179
  • 445
  • 697

2 Answers2

1

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 --- ex-moderator kitten
  • 98,632
  • 24
  • 142
  • 234
1

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
  • 334,560
  • 70
  • 407
  • 495