Souvik Banerjee
Souvik Banerjee

Reputation: 1

Append newline variables together shell script

In a shell script, I have two variables which are created as:

newvar1=`echo "$var1"`
newvar2=`echo "$var2"`

Eg newvar1:

A|1
B|2

newvar2:

K/L/M
X/Y/Z

Need to get output like

A|1:K/L/M
B|2:X/Y/Z

Upvotes: 0

Views: 44

Answers (1)

Timur Shtatland
Timur Shtatland

Reputation: 12347

Use paste to print the lines side by side. They're delimited by tabs by default; we can change that to colons with -d ':'.

paste -d ':' <( echo $'A|1\nB|2' ) <( echo $'K/L/M\nX/Y/Z' )

Output:

A|1:K/L/M
B|2:X/Y/Z

If you use variables make sure to quote them to preserve the newlines:

paste -d ':' <( echo "$newvar1" ) <( echo "$newvar2" )

Upvotes: 5

Related Questions