You are not logged in.

#1 2010-10-20 10:34:08

snack
Member
From: Italy
Registered: 2009-01-13
Posts: 876

[SOLVED] How to remove the newline in bash commands output?

Hi, I have a problem with the  output of bash commands. I'd like to print the result of a command like 'cut' to console without the newline character at the end, much like 'echo -n' does. It would be sufficient to find a command which eliminates the newline and doesn't put it back when it outputs the result, like 'sed' does. Is there an option to tell 'sed' to give its output without appendinga a newline?
Thanks

Last edited by snack (2010-10-20 10:50:59)

Offline

#2 2010-10-20 10:42:32

skanky
Member
From: WAIS
Registered: 2009-10-23
Posts: 1,847

Re: [SOLVED] How to remove the newline in bash commands output?

There will be a better method than this, I'm sure, but you could use awk:

awk '{printf $0}' 

"...one cannot be angry when one looks at a penguin."  - John Ruskin
"Life in general is a bit shit, and so too is the internet. And that's all there is." - scepticisle

Offline

#3 2010-10-20 10:48:37

lolilolicon
Member
Registered: 2009-03-05
Posts: 1,722

Re: [SOLVED] How to remove the newline in bash commands output?

printf %s "$(your_cmd_here)"

or

echo -n "$(your_cmd_here)"

This silver ladybug at line 28...

Offline

#4 2010-10-20 10:50:24

snack
Member
From: Italy
Registered: 2009-01-13
Posts: 876

Re: [SOLVED] How to remove the newline in bash commands output?

Thank you both, I tried  the  solution of skanky and  it worked perfectly.

Offline

#5 2010-10-20 10:56:17

lolilolicon
Member
Registered: 2009-03-05
Posts: 1,722

Re: [SOLVED] How to remove the newline in bash commands output?

snack wrote:

Thank you both, I tried  the  solution of skanky and  it worked perfectly.

In that case, you want

awk '{printf("%s",$0);}'

insead of

awk '{printf $0}'

Try:

echo '%s foobar' | awk '{printf $0}'

This silver ladybug at line 28...

Offline

#6 2010-10-20 11:47:20

snack
Member
From: Italy
Registered: 2009-01-13
Posts: 876

Re: [SOLVED] How to remove the newline in bash commands output?

Thanks lolilolicon, but awk '{printf $0}' works for my purpose, and it fits my needs (I havo to put it inside a cmake configuration file). I will also try your solutions, in case this stops to work.

Offline

#7 2010-10-20 12:05:49

skanky
Member
From: WAIS
Registered: 2009-10-23
Posts: 1,847

Re: [SOLVED] How to remove the newline in bash commands output?

Adding the %s is better practice and not doing so could well bite at some point in the future (esp. if you copy it to use elsewhere) - I was in a hurry earlier and hadn't thought my suggestion through properly.

Last edited by skanky (2010-10-20 12:06:02)


"...one cannot be angry when one looks at a penguin."  - John Ruskin
"Life in general is a bit shit, and so too is the internet. And that's all there is." - scepticisle

Offline

Board footer

Powered by FluxBB