0

I am trying to append a text to the existing file in Linux. Currently, instead of appending it to the existing line, it appends content to the next line. How can I append to the same line /or a specific line?

echo 'He' > /tmp/newfile
echo 'llo World' >> /tmp/newfile

the output of the file comes in two-line like the one below

He
llo World

how can I bring output like the below one

Hello World

How to bring the appending text to the same /specific line?

gibyalex
  • 529
  • 2
  • 7
  • 17
  • Incorrect dupe because it is NOT about echoing out without newline. It was `How to bring the appending text to the same /specific line?`. There might be some other dupe but this one is not right. – anubhava Mar 19 '21 at 08:26

2 Answers2

1

See man echo. You need -n to not append a trailing newline. If your shell's builtin echo does not support -n you need to use /bin/echo.

Wayne Vosberg
  • 1,003
  • 5
  • 7
0

You may use sed to append text in same line:

echo 'He' > /tmp/newfile
sed -i.bak '1s/$/llo World/' /tmp/newfile
cat /tmp/newfile

Hello World

1s/$/.../ will only append given text at the end of line #1

anubhava
  • 713,503
  • 59
  • 514
  • 593