Scheduled Maintenance: We are aware of an issue with Google, AOL, and Yahoo services as email providers which are blocking new registrations. We are trying to fix the issue and we have several internal and external support tickets in process to resolve the issue. Please see: viewtopic.php?t=158230

 

 

 

[Software] How to avoid line feeds in output from remote machine over SSH

If none of the specific sub-forums seem right for your thread, ask here.
Post Reply
Message
Author
Alan1901
Posts: 15
Joined: 2019-01-28 04:15

[Software] How to avoid line feeds in output from remote machine over SSH

#1 Post by Alan1901 »

Hi All,

I am trying to get the output from a command issued over SSH to a remote machine all on one line.

This is what I am using (from my own local machine, which is running Ubuntu 20.04 LTS) - the remote machine is running Debian 10 Buster:

Code: Select all

ssh -p22 UserA@MachineA -- 'hostnamectl | grep "Operating System" && echo -n " -- " && hostnamectl | grep "Kernel"'
It works fine, but it outputs like this:
Operating System: Debian GNU/Linux 10 (buster)
-- Kernel: Linux 4.19.0-23-amd64
whereas I would really like to get it all on one line like this:
Operating System: Debian GNU/Linux 10 (buster) -- Kernel: Linux 4.19.0-23-amd64
There are also spaces in there - I'm not worried about those, just wanting to avoid the line-feed or carriage-return. I can do that with a straight 'echo -n', but not sure how to do it with the actual commands.


Thanks in advance for any ideas or pointers!

Alan.

lindi
Debian Developer
Debian Developer
Posts: 412
Joined: 2022-07-12 14:10
Has thanked: 1 time
Been thanked: 77 times

Re: [Software] How to avoid line feeds in output from remote machine over SSH

#2 Post by lindi »

You can do this with

Code: Select all

ssh -p22 UserA@MachineA -- 'echo $(hostnamectl | grep "Operating System") -- $(hostnamectl | grep "Kernel")'
However, I strongly urge you to consider some other programming language like python.

Alan1901
Posts: 15
Joined: 2019-01-28 04:15

Re: [Software] How to avoid line feeds in output from remote machine over SSH

#3 Post by Alan1901 »

Hi lindi,
lindi wrote: 2023-03-22 23:10 You can do this with

Code: Select all

ssh -p22 UserA@MachineA -- 'echo $(hostnamectl | grep "Operating System") -- $(hostnamectl | grep "Kernel")'
However, I strongly urge you to consider some other programming language like python.
Brilliant - works perfectly.

Noted about using Python - it is on my list of things to learn, but I haven't gotten that far yet (still a newb in all things Linux really).


Thanks,

Alan.

User avatar
kent_dorfman766
Posts: 535
Joined: 2022-12-16 06:34
Location: socialist states of america
Has thanked: 57 times
Been thanked: 70 times

Re: [Software] How to avoid line feeds in output from remote machine over SSH

#4 Post by kent_dorfman766 »

Python or Perl, but the point is the same. SSH output is stdout so can be piped and filtered anyway you like with the right string of filters. I'd use Perl because it is what I know and the replacement pattern (while resembling garbled modem noise) is much more concise than a python script.

User avatar
donald
Debian Developer, Site Admin
Debian Developer, Site Admin
Posts: 1046
Joined: 2021-03-30 20:08
Has thanked: 186 times
Been thanked: 240 times

Re: [Software] How to avoid line feeds in output from remote machine over SSH

#5 Post by donald »

We should do a quick system admin commands thread. Stuff like this is invaluable sometimes. Not sure how to pull it off though as each reply to a post could garner a new topic...
Typo perfectionish.


"The advice given above is all good, and just because a new message has appeared it does not mean that a problem has arisen, just that a new gremlin hiding in the hardware has been exposed." - FreewheelinFrank

Post Reply