banner



How To Store Each Output In Registers Mars

3 tricks to get ii commands output in a single row. Normally two commands outputs will be always separated by a carriage render in Linux terminal.

One of the major hurdles in scripting is to go your outputs formatted well according to requirements. In Linux/Unix each command stdout its output always in a new line. This is a hurdle in many situations where the coder wants 2 outputs, ii terms, ii variables in a single row for example CSV format. In this mail, nosotros will see how to nowadays two or more commands output in a single row.

Read as well : Scripting related posts

Normally when we execute more than one command, they will brandish outputs in different rows:

# engagement ; hostname Sat Dec 24 01:35:50 EDT 2016 testsrv2  # echo text1; echo text2; echo text3 text1 text2 text3          

In the above example, commands have outputs per row. Nosotros are going to achieve all outputs in one row using the beneath tricks.

Trick i:

Using the interpret function. AS I said earlier each command output is accompanied by a carriage render. We will be translating railroad vehicle return with tab character hence getting 2 or more than commands output in a single row.

# (appointment; hostname) |tr '\n' '\t' Sat Dec 24 01:36:22 EDT 2016    testsrv2  # (echo text1; repeat text2; echo text3) |tr '\n' '\t' text1   text2   text3          

Bypassing outputs to translate function we achieved single row output. Here we instructed tr function to translate all carriage returns (\north) to tab (\t).

If you want to output in CSV format (comma-separated value) then supervene upon tab with a comma and you will go it.

# (engagement; hostname) |tr '\northward' ',' Sabbatum December 24 01:43:09 EDT 2016,testsrv2,  # (echo text1; echo text2; echo text3) |tr '\n' ',' text1,text2,text3,          

You can observe 1 trailing comma which shows every command output ends with \n and hence it gets replaced past ,

Pull a fast one on 2:

By defining each command output as a variable. Here nosotros volition store the output of each command in one variable and and so we will echo those variables in a unmarried line.

# mydate=`engagement`  # myname=`hostname`  # echo $myname $mydate testsrv2 Saturday Dec 24 01:46:04 EDT 2016          

Here nosotros stored date and hostname command's output in mydate and myname variables. In the last control, we echoed both variables with space in betwixt. Note that commands output can be stored in a variable by executing the control using backticks (in-line execution)!

Trick 3:

By echoing in-line execution outputs. In-line execution i.due east. using backticks to execute commands within another command. We will be using this trick to repeat outputs in a single row.

# echo "`date` `hostname`" Sat December 24 01:fifty:36 EDT 2016 testsrv2          

In the to a higher place instance, first, we have executed commands and got outputs. But those outputs are fed into echo command. Hence echo control stdout them in a single row.

# echo "`repeat text1` `echo text2` `repeat text3`" text1 text2 text3          

Make a note that we executed each command in dissever back tick quotes.

How To Store Each Output In Registers Mars,

Source: https://kerneltalks.com/scripts/3-ways-get-multiple-commands-output-row/

Posted by: howellablion.blogspot.com

0 Response to "How To Store Each Output In Registers Mars"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel