Joining Multi-line Output with Paste

I was checking the memory on approximately 140 servers using ansible:

ansible --user root -k -i servers.list -m shell -a  "free -h; mysql" test

but the output appears over several lines e.g.

server103.domain.dev | SUCCESS | rc=0 >>
total used free shared buffers cached
Mem: 94G 44G 49G 59M 203M 4.6G
-/+ buffers/cache: 39G 54G
Swap: 0B 0B 0B

but what I wanted was the server name and the total memory available. So I first grepped the output to remove the lines I didn’t want e.g.

grep -A3 "domain.dev" | egrep -v "^--|Swap|total|buffer"

leaving me with two lines of output per server e.g.

server103.domain.dev | SUCCESS | rc=0 >>
Mem: 94G 44G 49G 59M 203M 4.6G
server100.domain.dev | SUCCESS | rc=0 >>
Mem: 94G 54G 40G 59M 207M 7.7G

so to combine the two lines for each server into one line:

paste - - -d";\n" < inputfile.txt

The ‘paste’ command combines using two hyphens – each representing a line of input. The delimiter between the first line and the second line is a semi-colon and after the second line uses a line break.

Once I had the memory output from each server on a single line with the server name it was easy to sort and format the output.

 

 

 

 

Leave a Reply

  • (will not be published)

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>