Output to file print as single line, not separate line

example of problem:

when I echo "$e" >> /home/cogiz/file.txt

result prints to file as:

AA
BB
CC

I need it to save to file as this:

AA BB CC

I know it's probably something really simple but any help would be greatly appreciated.
Thank You.
Cogiz

printf "$e " >> /home/cogiz/file.txt

Hello cogiz,

One more easy way is there to do so, you could remove " in echo and do like echo $e it will print output as AA B CC , because " actually reads characters with their special meaning so values will not loose their new line but when we do simple echo they will(new line in current scenario) loose their special meaning, so in your case you could take advantage of this feature.

Thanks,
R. Singh

This is not true. It could be possible that your echo is aliased to echo -n (or may be the flavour of echo in your OS behaves this way? Not sure)

@cogiz: you may either try printf or echo -n

Hi
you have not specified which shell you are using.

Similar to echo -n there is a support in ksh ONLY with print command

-n
print does not append a newline to its output

Assuming you have a loop of some sort around your output statement, you could do something like this:-

for var in AA BB CC
do
   printf "%s" "$var"
done
printf "\n"

It's up to you where you want to set the redirection to append to the file. If you want to overwrite the file each time, you can perform a file open with:-

{ for var in AA BB CC
do
   printf "%s" "$var"
done
printf "\n"
} > output_file

I have written the printf with two arguments in case $var contains spurious data that could cause printf to do something unexpected. Some people find this annoying but it protects you somewhat from potentially damaging input.

I hope that this helps,
Robin

Hi cogiz,
To get the results shown above (i.e., changing <newline>s between words in the input to <space>s between words in the output, but keeping a trailing <newline> at the end of the output), e had to be set with something logically equivalent to:
e="AA
BB
CC"
Note that this variable contains two embedded <newline>s. Therefore, the suggestions provided by rdrtx1 and balajesuri will NOT work. The quoted argument ( "$e" ) keeps those embedded <newline> visible in the output from those suggestions. Note also that these non-working suggestions are a direct result of your failure to use CODE tags to make clear what input you had and what output you wanted.

The suggestion RavinderSingh13 provided should do exactly what you requested. The unquote argument ( $e ) passed to echo causes the shell to split the expansion of that variable into three operands, each of which will be printed as a string with a <space> separating the fields in the output and a single <newline> will be printed at the end.

1 Like

Longhand using OSX 10.12.1 default bash terminal.

Last login: Wed Nov 16 22:05:32 on ttys000
AMIGA:barrywalker~> echo "AA AB
> BB BC
> CC CD" > /tmp/text
AMIGA:barrywalker~> text=$( echo $( cat /tmp/text ) )
AMIGA:barrywalker~> echo "$text"
AA AB BB BC CC CD
AMIGA:barrywalker~> _

EDIT:
Numble apologies I may have misread the OP.