0

I am in a unix env and I need to append below content to a txt file without starting at a new line: the content is:

set value = 9.0.1.3 || set var = 12.4

the txt file content is:

echo on
set name = 'charlie' ||

but when I use the command 'cat', I can append the content to the txt file, but the content will start from a new line, like this:

echo on
set name = 'charlie' ||
set value = 9.0.1.3 || set var = 12.4

well, my goal is to get it like this:

echo on
set name = 'charlie' || set value = 9.0.1.3 || set var = 12.4

What command should I use to reach my goal? thx.

CharlieShi
  • 888
  • 3
  • 17
  • 43
  • [Check this][1] [1]: http://stackoverflow.com/questions/602921/what-unix-command-can-we-use-to-append-some-text-to-a-specific-line-in-a-file – ndalama Jan 30 '12 at 07:57
  • Thx, appriciate it very much. – CharlieShi Jan 30 '12 at 08:01
  • This does exactly what you're looking for: http://stackoverflow.com/questions/3936738/bash-cat-multiple-files-content-in-to-single-string-without-newlines – Dawood Jan 30 '12 at 08:02

2 Answers2

0
sed -i '$s/$/ set value = 9.0.1.3 || set var = 12.4/' FILE

That is inplace editing FILE (by -i, if you need a backup, use -i.BACKUPEXTENSION). The 1st $ after the ' addresses the last line, then ssubstitute the empty $tring on it at the end of the line with your string.

If it should be done on other line(s) than the last, you can address the substitute command with line numbers, regex, etc.

Zsolt Botykai
  • 50,406
  • 14
  • 85
  • 110
0

By default cat is not putting any new line charecter in the end of the file. If you already have a newline character in the end of the file then that will be concatinated.

>echo "TTTT\c">test1
>echo "GGGG\c">test2
>cat test1 test2
TTTTGGGG>

Please remove the newline character at the end of your file and then run the cat command.

You can remove the newline character at the end of file using below command:

awk 'BEGIN{flag=0}{if(flag==1)print a;flag=1;a=$0}END{printf("%s",a)}' FILE_NAME >FILE_NAME
chanchal1987
  • 2,320
  • 7
  • 31
  • 64