Operating System - HP-UX
1819801 Members
3067 Online
109607 Solutions
New Discussion юеВ

echo a string withou a new line

 
SOLVED
Go to solution
hp admin
Frequent Advisor

echo a string withou a new line

How can I added a string at the end of a file without return to a new line.

Please help.
14 REPLIES 14
Mark Grant
Honored Contributor

Re: echo a string withou a new line

Depends

echo -n "hello" will not produce a newline but Berkley "echo" would be something like

echo "hello\c". I'd try the first one of these first.
Never preceed any demonstration with anything more predictive than "watch this"
RAC_1
Honored Contributor

Re: echo a string withou a new line

may be echo "xxx\t"

echo xxx|tr "\n" " "
There is no substitute to HARDWORK
Sam White
Occasional Contributor

Re: echo a string withou a new line

You need to add a no carrage return to the end of your echo statement.

ie.

echo "blah blah\c"

Note the \c at the end.
Umapathy S
Honored Contributor

Re: echo a string withou a new line

In ksh and sh you can do as
echo "hello\c"
Arise Awake and Stop NOT till the goal is Reached!
Mark Grant
Honored Contributor

Re: echo a string withou a new line

Karim,

Just re-read your post with a little more detail and perhaps need to expand these answers a bit.

echo -n "some kind of string" >> file

Non-Berkley

echo "some kindof string\c" >> file
Never preceed any demonstration with anything more predictive than "watch this"
Leif Halvarsson_2
Honored Contributor

Re: echo a string withou a new line

Hi,
Another way is to use printf, using printf without any formatting arguments will do the job. Ex:

printf abcde
hp admin
Frequent Advisor

Re: echo a string withou a new line

Thanks a lot for your responses.

when I was in a terminal, all your commands run correctely.

But to added the string at the end of a file (exactelly just after the last word of the last line), it dosen't work.

Please help me again.
john korterman
Honored Contributor

Re: echo a string withou a new line

Hi,
what was the command you used? If you open the file with xvi you can check if there is a "0a" char at the end or not. Is there?

regards,
John K.
it would be nice if you always got a second chance
Mark Grant
Honored Contributor
Solution

Re: echo a string withou a new line

Karim,

If you want to add the text to the end of a file as you describe, you are going to have to remove the newline from the end of the file first. In perl you might do

#!/usr/bin/perl

$DATA=`cat datafile`;
chop $DATA;
print "$DATA"."The string I want at the end\n";
Never preceed any demonstration with anything more predictive than "watch this"
Jean-Luc Oudart
Honored Contributor

Re: echo a string withou a new line

with awk :
#!/bin/sh

export STRING="abcde"
awk -v string=$STRING '
{ print $0;
}
END{ printf("%s",string);}' yourfile > out1

result in out1

Rgds,
Jean-Luc
fiat lux
Bruno Ganino
Honored Contributor

Re: echo a string withou a new line

I don't understand what is the type of file in question.
If file is command-file of shell
echo "argument\c" print line without new-line character
echo "argument\n" new-line character
echo "argument\t" tab
echo "argument\r" carriage return.
if file is text-file and you edit it, (example with vi editor) you must to be in insert mode.
If lines added is in text-screen you must verifies setup of terminal in the options New-line and Wrap (check setting of tab also).
Bruno

Torino (Turin) +2H
hp admin
Frequent Advisor

Re: echo a string withou a new line

Thanks again for your answer:

Here the solution with ksh:
#!/usr/bin/ksh
DATA=`cat /etc/PATH`
echo "$DATA""le_mot_a_ajouter\c" > /tmp/PATH.new

and with perl:
#!/usr/bin/perl
$DATA=`cat datafile`;
chop $DATA;
print "$DATA"."The string I want at the end\n";


Mark Grant
Honored Contributor

Re: echo a string withou a new line

Karim,

I think I'm right in saying that there is a maximum string length in the shell (could be that I'm still living in the past on that though) so the shell solution here might not work for bigger files.
Never preceed any demonstration with anything more predictive than "watch this"
Bruno Ganino
Honored Contributor

Re: echo a string withou a new line

Karim,
I hope that your problem is solved... but i don't sure totally.
Good luck!
Bruno
Torino (Turin) +2H