omgmakeme
omgmakeme

Reputation: 766

Display Unique Shell Columns

Given we have two formatted strings that are unrelated to each other. #test.rb string_1 = "Title\nfoo bar\nbaz\nfoo bar baz boo" string_2 = "Unrelated Title\ndog cat farm\nspace moon"

How can I use ruby or call shell commands to have each of these string display as columns in terminal? The key is that the data of each string are not building a correlated row, ie this is not a table, rather 2 lists side by side.

Title            Unrelated Title
foo bar          dog cat farm
baz              space moon
foo bar baz boo  

Upvotes: 1

Views: 85

Answers (3)

SURESH GOACHER
SURESH GOACHER

Reputation: 1

Hi , If you Use temp files

string_1 = "Title\nfoo bar\nbaz\nfoo bar baz boo"
string_2 = "Unrelated Title\ndog cat farm\nspace moon"
echo -e $string_1 >a.txt
echo -e $string_2 >b.txt
paste a.txt b.txt

I hope it will help.

Upvotes: 0

Keith Bennett
Keith Bennett

Reputation: 4970

In Ruby, you could do it this way:

#!/usr/bin/env ruby

string_1 = "Title\nfoo bar\nbaz\nfoo bar baz boo"
string_2 = "Unrelated Title\ndog cat farm\nspace moon"

a1 = string_1.split("\n")
a2 = string_2.split("\n")

a1.zip(a2).each { |pair| puts "%-20s%s" % [pair.first, pair.last] }
# or
# a1.zip(a2).each { |left, right| puts "%-20s%s" % [left, right] }

This produces:

Title               Unrelated Title
foo bar             dog cat farm
baz                 space moon
foo bar baz boo

Upvotes: 2

jaypal singh
jaypal singh

Reputation: 77135

You can try using paste and column command together. Note that this is a shell command so spaces between the assignment operator should be corrected.

$ string_1="Title\nfoo bar\nbaz\nfoo bar baz boo"
$ string_2="Unrelated Title\ndog cat farm\nspace moon"
$ paste -d '|' <(echo -e "$string_1") <(echo -e "$string_2") | column -s'|' -t
Title            Unrelated Title
foo bar          dog cat farm
baz              space moon
foo bar baz boo

We paste the lines with | as delimiter and tell column command to use | as a reference to form columns.

Upvotes: 4

Related Questions