2

all experts I facing an issue with adding a new line with sed

I want to add a line into the end of the text file, I tried with

echo "Hello, how are you?" >> myfile.txt

but the issue is its creating extra one more empty line like below

 1 Hello, how are you?
 2 

so I want it will create 1 line end of the text file line if there is no line into the text file then it will create as a 1st line & all next line will create after the last line without any empty/blank line, T.I.A

Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
  • 3
    Put another way, it is *correctly* creating a one-line text file where every line is terminated by a newline character. You appear to be coming from a Window background, where the last line of a text file doesn't need to be (and commonly isn't?) terminated. – chepner Sep 24 '18 at 20:53
  • Where are you using `sed`? – chepner Sep 24 '18 at 20:55
  • 1
    Arguably, it's your editor that's wrong for showing a line there. – o11c Sep 24 '18 at 21:07
  • Why did someone upvote this question? An upvote indicates it is useful and well researched. The best I can tell the question does not meet either criteria. How does this question meet the criteria? – jww Sep 24 '18 at 22:33

1 Answers1

3

By default, "echo" adds trailing new line character. Use "-n" with echo to suppress the new line

echo -n "Hello, how are you?" >> myfile.txt

Another option is - using printf

printf "Hello,how are you?" >> myfile.txt
Nishu Tayal
  • 20,106
  • 8
  • 49
  • 101
  • `echo -n` isn't specified (to do have any particular behavior) by POSIX. Much safer to use `printf` instead. See [the POSIX standard for the `echo` command](http://pubs.opengroup.org/onlinepubs/9699919799/utilities/echo.html), especially the APPLICATION USAGE section. – Charles Duffy Sep 24 '18 at 20:57
  • 2
    That is: `printf '%s' "Hello, how are you?"` -- though the result isn't a valid UNIX text file (`while read` will return an error for the last line, f/e, since that line isn't valid without a newline terminator). – Charles Duffy Sep 24 '18 at 20:57
  • @CharlesDuffy : Thanks for bringing to the notice. updated accordingly. :) – Nishu Tayal Sep 24 '18 at 21:02
  • The problem with `printf "your string here"` instead of `printf '%s' "your string here"` is that in the former case, the content is treated as a format string rather than a literal, so you need to be careful of `%` signs or backslashes within it; in the latter case, what gets printed is byte-for-byte exactly what's in the argument. – Charles Duffy Sep 24 '18 at 21:03
  • thanks for your replay when I used echo -n "Hello, how are you?" >> myfile.txt its add line without extra line but then if I used again echo -n "I am fine, how are you?" its insert in the same line, not end of the line Hello, 1 how are you?I am fine, how are you? but I was wanted to add each line end of the file line without create new line, hope I make my question clear, – Asterisk Developer Sep 24 '18 at 21:27
  • Please avoid answering [off-topic](http://stackoverflow.com/help/on-topic) fodder. Instead, close it and move on. It is sometimes helpful to suggest a site where the question may be on-topic. – jww Sep 24 '18 at 22:34
  • 1
    @jww what is your problem? if someone wants to answer or not its there problem, not yours, I think you are upset about something & you are getting out your angriness on this question/topic, Please deal with your own business, Thanks... – Asterisk Developer Sep 24 '18 at 23:43