re-write a string with text and numbers onto a newline and insert text

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
0
down vote

favorite












In Short, I want to split single line output string into multiple rows and label each raw with the hostname.



Hostname ProcessName CPUCore



output=$(ssh -q -o "StrictHostKeyChecking yes" $ssh_host 'ps -eo comm,lastcpu | grep rrcp')



How to insert loop through a return out put that looks like.



rrcpd 17 rrcpd 0


and re-write as



Hostname1 rrcpd 17
Hostname1 rrcpd 0


Note



  • The numbers can very from 0-32


  • Hostname is a variable string $myhost



  • The text does change, meaning it is not strictly rrcpd, it could be
    e.g.



    rrcpd 17 rrcpd 0



    Cheese 3 cakes 8



    Cats 9 dogs 3



Command that run on each host



output=$(ssh -q -o "StrictHostKeyChecking yes" $ssh_host 'ps -eo comm,lastcpu | grep rrcp')



The output can come back with 3-5 process when I remove the grep, hence I want to be able to split each output and add the hostname to the raw to label each output. I am checking what processes are running on each host.



rrcpd 0 rrcp_mon.sh 24



rrcpd 0



rrcpd 0



rrcpd 0 rrcpd 1



rrcpd 0 rrcpd 0 rrcp_mon 24 rrcp_mon 24



rrcpd 0



rrcpd 0



rrcpd 17 rrcpd 0



ard 9 ssh 32 httpd 21 bax 22



I have modified the provided below and managed to get it to work



#!/bin/bash
for remote in $(cat ssh_hosts2.txt)
do
ssh -q -o StrictHostKeyChecking=yes "$remote" ps -eo comm,lastcpu |
sed "s/^/$remote /"
done









share|improve this question























  • I don't get it. Please add a proper example and result. For the given examples, the answer is valid. Please properly format you text so that we don't have to guess what you mean.
    – RoVo
    Aug 13 at 11:45











  • Why is the last line outputted if you grep for rrcp?
    – Kusalananda
    Aug 13 at 11:46














up vote
0
down vote

favorite












In Short, I want to split single line output string into multiple rows and label each raw with the hostname.



Hostname ProcessName CPUCore



output=$(ssh -q -o "StrictHostKeyChecking yes" $ssh_host 'ps -eo comm,lastcpu | grep rrcp')



How to insert loop through a return out put that looks like.



rrcpd 17 rrcpd 0


and re-write as



Hostname1 rrcpd 17
Hostname1 rrcpd 0


Note



  • The numbers can very from 0-32


  • Hostname is a variable string $myhost



  • The text does change, meaning it is not strictly rrcpd, it could be
    e.g.



    rrcpd 17 rrcpd 0



    Cheese 3 cakes 8



    Cats 9 dogs 3



Command that run on each host



output=$(ssh -q -o "StrictHostKeyChecking yes" $ssh_host 'ps -eo comm,lastcpu | grep rrcp')



The output can come back with 3-5 process when I remove the grep, hence I want to be able to split each output and add the hostname to the raw to label each output. I am checking what processes are running on each host.



rrcpd 0 rrcp_mon.sh 24



rrcpd 0



rrcpd 0



rrcpd 0 rrcpd 1



rrcpd 0 rrcpd 0 rrcp_mon 24 rrcp_mon 24



rrcpd 0



rrcpd 0



rrcpd 17 rrcpd 0



ard 9 ssh 32 httpd 21 bax 22



I have modified the provided below and managed to get it to work



#!/bin/bash
for remote in $(cat ssh_hosts2.txt)
do
ssh -q -o StrictHostKeyChecking=yes "$remote" ps -eo comm,lastcpu |
sed "s/^/$remote /"
done









share|improve this question























  • I don't get it. Please add a proper example and result. For the given examples, the answer is valid. Please properly format you text so that we don't have to guess what you mean.
    – RoVo
    Aug 13 at 11:45











  • Why is the last line outputted if you grep for rrcp?
    – Kusalananda
    Aug 13 at 11:46












up vote
0
down vote

favorite









up vote
0
down vote

favorite











In Short, I want to split single line output string into multiple rows and label each raw with the hostname.



Hostname ProcessName CPUCore



output=$(ssh -q -o "StrictHostKeyChecking yes" $ssh_host 'ps -eo comm,lastcpu | grep rrcp')



How to insert loop through a return out put that looks like.



rrcpd 17 rrcpd 0


and re-write as



Hostname1 rrcpd 17
Hostname1 rrcpd 0


Note



  • The numbers can very from 0-32


  • Hostname is a variable string $myhost



  • The text does change, meaning it is not strictly rrcpd, it could be
    e.g.



    rrcpd 17 rrcpd 0



    Cheese 3 cakes 8



    Cats 9 dogs 3



Command that run on each host



output=$(ssh -q -o "StrictHostKeyChecking yes" $ssh_host 'ps -eo comm,lastcpu | grep rrcp')



The output can come back with 3-5 process when I remove the grep, hence I want to be able to split each output and add the hostname to the raw to label each output. I am checking what processes are running on each host.



rrcpd 0 rrcp_mon.sh 24



rrcpd 0



rrcpd 0



rrcpd 0 rrcpd 1



rrcpd 0 rrcpd 0 rrcp_mon 24 rrcp_mon 24



rrcpd 0



rrcpd 0



rrcpd 17 rrcpd 0



ard 9 ssh 32 httpd 21 bax 22



I have modified the provided below and managed to get it to work



#!/bin/bash
for remote in $(cat ssh_hosts2.txt)
do
ssh -q -o StrictHostKeyChecking=yes "$remote" ps -eo comm,lastcpu |
sed "s/^/$remote /"
done









share|improve this question















In Short, I want to split single line output string into multiple rows and label each raw with the hostname.



Hostname ProcessName CPUCore



output=$(ssh -q -o "StrictHostKeyChecking yes" $ssh_host 'ps -eo comm,lastcpu | grep rrcp')



How to insert loop through a return out put that looks like.



rrcpd 17 rrcpd 0


and re-write as



Hostname1 rrcpd 17
Hostname1 rrcpd 0


Note



  • The numbers can very from 0-32


  • Hostname is a variable string $myhost



  • The text does change, meaning it is not strictly rrcpd, it could be
    e.g.



    rrcpd 17 rrcpd 0



    Cheese 3 cakes 8



    Cats 9 dogs 3



Command that run on each host



output=$(ssh -q -o "StrictHostKeyChecking yes" $ssh_host 'ps -eo comm,lastcpu | grep rrcp')



The output can come back with 3-5 process when I remove the grep, hence I want to be able to split each output and add the hostname to the raw to label each output. I am checking what processes are running on each host.



rrcpd 0 rrcp_mon.sh 24



rrcpd 0



rrcpd 0



rrcpd 0 rrcpd 1



rrcpd 0 rrcpd 0 rrcp_mon 24 rrcp_mon 24



rrcpd 0



rrcpd 0



rrcpd 17 rrcpd 0



ard 9 ssh 32 httpd 21 bax 22



I have modified the provided below and managed to get it to work



#!/bin/bash
for remote in $(cat ssh_hosts2.txt)
do
ssh -q -o StrictHostKeyChecking=yes "$remote" ps -eo comm,lastcpu |
sed "s/^/$remote /"
done






linux bash shell-script shell string






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Aug 13 at 14:49

























asked Aug 13 at 10:55









SaSa2929

11




11











  • I don't get it. Please add a proper example and result. For the given examples, the answer is valid. Please properly format you text so that we don't have to guess what you mean.
    – RoVo
    Aug 13 at 11:45











  • Why is the last line outputted if you grep for rrcp?
    – Kusalananda
    Aug 13 at 11:46
















  • I don't get it. Please add a proper example and result. For the given examples, the answer is valid. Please properly format you text so that we don't have to guess what you mean.
    – RoVo
    Aug 13 at 11:45











  • Why is the last line outputted if you grep for rrcp?
    – Kusalananda
    Aug 13 at 11:46















I don't get it. Please add a proper example and result. For the given examples, the answer is valid. Please properly format you text so that we don't have to guess what you mean.
– RoVo
Aug 13 at 11:45





I don't get it. Please add a proper example and result. For the given examples, the answer is valid. Please properly format you text so that we don't have to guess what you mean.
– RoVo
Aug 13 at 11:45













Why is the last line outputted if you grep for rrcp?
– Kusalananda
Aug 13 at 11:46




Why is the last line outputted if you grep for rrcp?
– Kusalananda
Aug 13 at 11:46










2 Answers
2






active

oldest

votes

















up vote
2
down vote













please try this,



I have saved the given sample output in file /tmp/test.txt.



 xargs -n 2 | sed 's/^/Hostname1 /' 



  • -n means maximum arguments per line.

For example;



cat /tmp/test.txt | xargs -n 2 | sed 's/^/Hostname1 /g' 
Hostname1 rrcpd 17
Hostname1 rrcpd 0
Hostname1 Cheese 3
Hostname1 cakes 8
Hostname1 Cats 9
Hostname1 dogs 3





share|improve this answer






















  • There is only one match of ^ per line, so using /g is not needed.
    – Kusalananda
    Aug 13 at 12:02

















up vote
0
down vote













The only reason you would see the type of data that you show (more than two fields per line in the output of the ssh command) is when you collect the output of your ssh+ps call into a variable and then output it without quoting it properly, as in output=$(ssh ...) followed by echo $output instead of echo "$output". Luckily, you don't need to collect the data in a variable at all.



while read remote; do
ssh -n -q -o StrictHostKeyChecking=yes "$remote" ps -eo comm,lastcpu |
awk -v host="$remote" '/rrcp/ print host, $1, $2 '
done <hosts.txt


Here, awk is reading directly from the ssh command and also does the work originally done by grep in your code. It assumes that it will get lines with two fields and will prefix each pair of fields with the given hostname (read from hosts.txt).



Alternatively,



while read remote; do
ssh -n -q -o StrictHostKeyChecking=yes "$remote" ps -eo comm,lastcpu |
sed "s/^/$remote /"
done <hosts.txt


Here, we simply prefix each line with the hostname read from the text file.






share|improve this answer






















  • I want to read the output in of a single line of string and numbers which is normally process name followed by a number which is the core. Sometimes its a single text and number and sometimes it return 2, 3 or 4 processes with text followed by number. My aim is to get this command output ps -eo comm,lastcpu and split each text number, text number ........ to multiple rows and label each row with a host name followed by text number.
    – SaSa2929
    Aug 13 at 12:21










  • @SaSa2929 Did you actually test the code I posted? The ps utility will only ever output two columns. The only reason you find yourself working with lines of more columns is that you echo an unquoted variable.
    – Kusalananda
    Aug 13 at 12:25











  • Thank you, it works, almost, Tried it, it only reads the first host, how do I get it to loop to the next host?
    – SaSa2929
    Aug 13 at 12:34










  • @SaSa2929 Ah, I forgot, ssh would eat the data from the input file. Use ssh -n. I will update the answer...
    – Kusalananda
    Aug 13 at 12:38










  • I have modified it to use a for loop and it works now and returns all the processes, how is it possible to go one step further and provide a list of processes that I am interested in and ignore all others. I can defind the processes in an array of some sort, or include list, all others are exluded. #!/bin/bash for remote in $(cat ssh_hosts2.txt) do ssh -q -o StrictHostKeyChecking=yes "$remote" ps -eo comm,lastcpu | sed "s/^/$remote /" done
    – SaSa2929
    Aug 13 at 14:51










Your Answer







StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "106"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: false,
noModals: false,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













 

draft saved


draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f462274%2fre-write-a-string-with-text-and-numbers-onto-a-newline-and-insert-text%23new-answer', 'question_page');

);

Post as a guest






























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
2
down vote













please try this,



I have saved the given sample output in file /tmp/test.txt.



 xargs -n 2 | sed 's/^/Hostname1 /' 



  • -n means maximum arguments per line.

For example;



cat /tmp/test.txt | xargs -n 2 | sed 's/^/Hostname1 /g' 
Hostname1 rrcpd 17
Hostname1 rrcpd 0
Hostname1 Cheese 3
Hostname1 cakes 8
Hostname1 Cats 9
Hostname1 dogs 3





share|improve this answer






















  • There is only one match of ^ per line, so using /g is not needed.
    – Kusalananda
    Aug 13 at 12:02














up vote
2
down vote













please try this,



I have saved the given sample output in file /tmp/test.txt.



 xargs -n 2 | sed 's/^/Hostname1 /' 



  • -n means maximum arguments per line.

For example;



cat /tmp/test.txt | xargs -n 2 | sed 's/^/Hostname1 /g' 
Hostname1 rrcpd 17
Hostname1 rrcpd 0
Hostname1 Cheese 3
Hostname1 cakes 8
Hostname1 Cats 9
Hostname1 dogs 3





share|improve this answer






















  • There is only one match of ^ per line, so using /g is not needed.
    – Kusalananda
    Aug 13 at 12:02












up vote
2
down vote










up vote
2
down vote









please try this,



I have saved the given sample output in file /tmp/test.txt.



 xargs -n 2 | sed 's/^/Hostname1 /' 



  • -n means maximum arguments per line.

For example;



cat /tmp/test.txt | xargs -n 2 | sed 's/^/Hostname1 /g' 
Hostname1 rrcpd 17
Hostname1 rrcpd 0
Hostname1 Cheese 3
Hostname1 cakes 8
Hostname1 Cats 9
Hostname1 dogs 3





share|improve this answer














please try this,



I have saved the given sample output in file /tmp/test.txt.



 xargs -n 2 | sed 's/^/Hostname1 /' 



  • -n means maximum arguments per line.

For example;



cat /tmp/test.txt | xargs -n 2 | sed 's/^/Hostname1 /g' 
Hostname1 rrcpd 17
Hostname1 rrcpd 0
Hostname1 Cheese 3
Hostname1 cakes 8
Hostname1 Cats 9
Hostname1 dogs 3






share|improve this answer














share|improve this answer



share|improve this answer








edited Aug 13 at 12:06

























answered Aug 13 at 11:04









EBIN GLADSON

656




656











  • There is only one match of ^ per line, so using /g is not needed.
    – Kusalananda
    Aug 13 at 12:02
















  • There is only one match of ^ per line, so using /g is not needed.
    – Kusalananda
    Aug 13 at 12:02















There is only one match of ^ per line, so using /g is not needed.
– Kusalananda
Aug 13 at 12:02




There is only one match of ^ per line, so using /g is not needed.
– Kusalananda
Aug 13 at 12:02












up vote
0
down vote













The only reason you would see the type of data that you show (more than two fields per line in the output of the ssh command) is when you collect the output of your ssh+ps call into a variable and then output it without quoting it properly, as in output=$(ssh ...) followed by echo $output instead of echo "$output". Luckily, you don't need to collect the data in a variable at all.



while read remote; do
ssh -n -q -o StrictHostKeyChecking=yes "$remote" ps -eo comm,lastcpu |
awk -v host="$remote" '/rrcp/ print host, $1, $2 '
done <hosts.txt


Here, awk is reading directly from the ssh command and also does the work originally done by grep in your code. It assumes that it will get lines with two fields and will prefix each pair of fields with the given hostname (read from hosts.txt).



Alternatively,



while read remote; do
ssh -n -q -o StrictHostKeyChecking=yes "$remote" ps -eo comm,lastcpu |
sed "s/^/$remote /"
done <hosts.txt


Here, we simply prefix each line with the hostname read from the text file.






share|improve this answer






















  • I want to read the output in of a single line of string and numbers which is normally process name followed by a number which is the core. Sometimes its a single text and number and sometimes it return 2, 3 or 4 processes with text followed by number. My aim is to get this command output ps -eo comm,lastcpu and split each text number, text number ........ to multiple rows and label each row with a host name followed by text number.
    – SaSa2929
    Aug 13 at 12:21










  • @SaSa2929 Did you actually test the code I posted? The ps utility will only ever output two columns. The only reason you find yourself working with lines of more columns is that you echo an unquoted variable.
    – Kusalananda
    Aug 13 at 12:25











  • Thank you, it works, almost, Tried it, it only reads the first host, how do I get it to loop to the next host?
    – SaSa2929
    Aug 13 at 12:34










  • @SaSa2929 Ah, I forgot, ssh would eat the data from the input file. Use ssh -n. I will update the answer...
    – Kusalananda
    Aug 13 at 12:38










  • I have modified it to use a for loop and it works now and returns all the processes, how is it possible to go one step further and provide a list of processes that I am interested in and ignore all others. I can defind the processes in an array of some sort, or include list, all others are exluded. #!/bin/bash for remote in $(cat ssh_hosts2.txt) do ssh -q -o StrictHostKeyChecking=yes "$remote" ps -eo comm,lastcpu | sed "s/^/$remote /" done
    – SaSa2929
    Aug 13 at 14:51














up vote
0
down vote













The only reason you would see the type of data that you show (more than two fields per line in the output of the ssh command) is when you collect the output of your ssh+ps call into a variable and then output it without quoting it properly, as in output=$(ssh ...) followed by echo $output instead of echo "$output". Luckily, you don't need to collect the data in a variable at all.



while read remote; do
ssh -n -q -o StrictHostKeyChecking=yes "$remote" ps -eo comm,lastcpu |
awk -v host="$remote" '/rrcp/ print host, $1, $2 '
done <hosts.txt


Here, awk is reading directly from the ssh command and also does the work originally done by grep in your code. It assumes that it will get lines with two fields and will prefix each pair of fields with the given hostname (read from hosts.txt).



Alternatively,



while read remote; do
ssh -n -q -o StrictHostKeyChecking=yes "$remote" ps -eo comm,lastcpu |
sed "s/^/$remote /"
done <hosts.txt


Here, we simply prefix each line with the hostname read from the text file.






share|improve this answer






















  • I want to read the output in of a single line of string and numbers which is normally process name followed by a number which is the core. Sometimes its a single text and number and sometimes it return 2, 3 or 4 processes with text followed by number. My aim is to get this command output ps -eo comm,lastcpu and split each text number, text number ........ to multiple rows and label each row with a host name followed by text number.
    – SaSa2929
    Aug 13 at 12:21










  • @SaSa2929 Did you actually test the code I posted? The ps utility will only ever output two columns. The only reason you find yourself working with lines of more columns is that you echo an unquoted variable.
    – Kusalananda
    Aug 13 at 12:25











  • Thank you, it works, almost, Tried it, it only reads the first host, how do I get it to loop to the next host?
    – SaSa2929
    Aug 13 at 12:34










  • @SaSa2929 Ah, I forgot, ssh would eat the data from the input file. Use ssh -n. I will update the answer...
    – Kusalananda
    Aug 13 at 12:38










  • I have modified it to use a for loop and it works now and returns all the processes, how is it possible to go one step further and provide a list of processes that I am interested in and ignore all others. I can defind the processes in an array of some sort, or include list, all others are exluded. #!/bin/bash for remote in $(cat ssh_hosts2.txt) do ssh -q -o StrictHostKeyChecking=yes "$remote" ps -eo comm,lastcpu | sed "s/^/$remote /" done
    – SaSa2929
    Aug 13 at 14:51












up vote
0
down vote










up vote
0
down vote









The only reason you would see the type of data that you show (more than two fields per line in the output of the ssh command) is when you collect the output of your ssh+ps call into a variable and then output it without quoting it properly, as in output=$(ssh ...) followed by echo $output instead of echo "$output". Luckily, you don't need to collect the data in a variable at all.



while read remote; do
ssh -n -q -o StrictHostKeyChecking=yes "$remote" ps -eo comm,lastcpu |
awk -v host="$remote" '/rrcp/ print host, $1, $2 '
done <hosts.txt


Here, awk is reading directly from the ssh command and also does the work originally done by grep in your code. It assumes that it will get lines with two fields and will prefix each pair of fields with the given hostname (read from hosts.txt).



Alternatively,



while read remote; do
ssh -n -q -o StrictHostKeyChecking=yes "$remote" ps -eo comm,lastcpu |
sed "s/^/$remote /"
done <hosts.txt


Here, we simply prefix each line with the hostname read from the text file.






share|improve this answer














The only reason you would see the type of data that you show (more than two fields per line in the output of the ssh command) is when you collect the output of your ssh+ps call into a variable and then output it without quoting it properly, as in output=$(ssh ...) followed by echo $output instead of echo "$output". Luckily, you don't need to collect the data in a variable at all.



while read remote; do
ssh -n -q -o StrictHostKeyChecking=yes "$remote" ps -eo comm,lastcpu |
awk -v host="$remote" '/rrcp/ print host, $1, $2 '
done <hosts.txt


Here, awk is reading directly from the ssh command and also does the work originally done by grep in your code. It assumes that it will get lines with two fields and will prefix each pair of fields with the given hostname (read from hosts.txt).



Alternatively,



while read remote; do
ssh -n -q -o StrictHostKeyChecking=yes "$remote" ps -eo comm,lastcpu |
sed "s/^/$remote /"
done <hosts.txt


Here, we simply prefix each line with the hostname read from the text file.







share|improve this answer














share|improve this answer



share|improve this answer








edited Aug 13 at 12:38

























answered Aug 13 at 11:48









Kusalananda

106k14209327




106k14209327











  • I want to read the output in of a single line of string and numbers which is normally process name followed by a number which is the core. Sometimes its a single text and number and sometimes it return 2, 3 or 4 processes with text followed by number. My aim is to get this command output ps -eo comm,lastcpu and split each text number, text number ........ to multiple rows and label each row with a host name followed by text number.
    – SaSa2929
    Aug 13 at 12:21










  • @SaSa2929 Did you actually test the code I posted? The ps utility will only ever output two columns. The only reason you find yourself working with lines of more columns is that you echo an unquoted variable.
    – Kusalananda
    Aug 13 at 12:25











  • Thank you, it works, almost, Tried it, it only reads the first host, how do I get it to loop to the next host?
    – SaSa2929
    Aug 13 at 12:34










  • @SaSa2929 Ah, I forgot, ssh would eat the data from the input file. Use ssh -n. I will update the answer...
    – Kusalananda
    Aug 13 at 12:38










  • I have modified it to use a for loop and it works now and returns all the processes, how is it possible to go one step further and provide a list of processes that I am interested in and ignore all others. I can defind the processes in an array of some sort, or include list, all others are exluded. #!/bin/bash for remote in $(cat ssh_hosts2.txt) do ssh -q -o StrictHostKeyChecking=yes "$remote" ps -eo comm,lastcpu | sed "s/^/$remote /" done
    – SaSa2929
    Aug 13 at 14:51
















  • I want to read the output in of a single line of string and numbers which is normally process name followed by a number which is the core. Sometimes its a single text and number and sometimes it return 2, 3 or 4 processes with text followed by number. My aim is to get this command output ps -eo comm,lastcpu and split each text number, text number ........ to multiple rows and label each row with a host name followed by text number.
    – SaSa2929
    Aug 13 at 12:21










  • @SaSa2929 Did you actually test the code I posted? The ps utility will only ever output two columns. The only reason you find yourself working with lines of more columns is that you echo an unquoted variable.
    – Kusalananda
    Aug 13 at 12:25











  • Thank you, it works, almost, Tried it, it only reads the first host, how do I get it to loop to the next host?
    – SaSa2929
    Aug 13 at 12:34










  • @SaSa2929 Ah, I forgot, ssh would eat the data from the input file. Use ssh -n. I will update the answer...
    – Kusalananda
    Aug 13 at 12:38










  • I have modified it to use a for loop and it works now and returns all the processes, how is it possible to go one step further and provide a list of processes that I am interested in and ignore all others. I can defind the processes in an array of some sort, or include list, all others are exluded. #!/bin/bash for remote in $(cat ssh_hosts2.txt) do ssh -q -o StrictHostKeyChecking=yes "$remote" ps -eo comm,lastcpu | sed "s/^/$remote /" done
    – SaSa2929
    Aug 13 at 14:51















I want to read the output in of a single line of string and numbers which is normally process name followed by a number which is the core. Sometimes its a single text and number and sometimes it return 2, 3 or 4 processes with text followed by number. My aim is to get this command output ps -eo comm,lastcpu and split each text number, text number ........ to multiple rows and label each row with a host name followed by text number.
– SaSa2929
Aug 13 at 12:21




I want to read the output in of a single line of string and numbers which is normally process name followed by a number which is the core. Sometimes its a single text and number and sometimes it return 2, 3 or 4 processes with text followed by number. My aim is to get this command output ps -eo comm,lastcpu and split each text number, text number ........ to multiple rows and label each row with a host name followed by text number.
– SaSa2929
Aug 13 at 12:21












@SaSa2929 Did you actually test the code I posted? The ps utility will only ever output two columns. The only reason you find yourself working with lines of more columns is that you echo an unquoted variable.
– Kusalananda
Aug 13 at 12:25





@SaSa2929 Did you actually test the code I posted? The ps utility will only ever output two columns. The only reason you find yourself working with lines of more columns is that you echo an unquoted variable.
– Kusalananda
Aug 13 at 12:25













Thank you, it works, almost, Tried it, it only reads the first host, how do I get it to loop to the next host?
– SaSa2929
Aug 13 at 12:34




Thank you, it works, almost, Tried it, it only reads the first host, how do I get it to loop to the next host?
– SaSa2929
Aug 13 at 12:34












@SaSa2929 Ah, I forgot, ssh would eat the data from the input file. Use ssh -n. I will update the answer...
– Kusalananda
Aug 13 at 12:38




@SaSa2929 Ah, I forgot, ssh would eat the data from the input file. Use ssh -n. I will update the answer...
– Kusalananda
Aug 13 at 12:38












I have modified it to use a for loop and it works now and returns all the processes, how is it possible to go one step further and provide a list of processes that I am interested in and ignore all others. I can defind the processes in an array of some sort, or include list, all others are exluded. #!/bin/bash for remote in $(cat ssh_hosts2.txt) do ssh -q -o StrictHostKeyChecking=yes "$remote" ps -eo comm,lastcpu | sed "s/^/$remote /" done
– SaSa2929
Aug 13 at 14:51




I have modified it to use a for loop and it works now and returns all the processes, how is it possible to go one step further and provide a list of processes that I am interested in and ignore all others. I can defind the processes in an array of some sort, or include list, all others are exluded. #!/bin/bash for remote in $(cat ssh_hosts2.txt) do ssh -q -o StrictHostKeyChecking=yes "$remote" ps -eo comm,lastcpu | sed "s/^/$remote /" done
– SaSa2929
Aug 13 at 14:51

















 

draft saved


draft discarded















































 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f462274%2fre-write-a-string-with-text-and-numbers-onto-a-newline-and-insert-text%23new-answer', 'question_page');

);

Post as a guest













































































Popular posts from this blog

How to check contact read email or not when send email to Individual?

Bahrain

Postfix configuration issue with fips on centos 7; mailgun relay