并排连接存储在变量中的字符串

pra*_*ddy 5 command-line text-processing

我有两个包含一组字符串的变量。我需要连接这些变量的结果以并排显示。

变量a有:

t
t
t
Run Code Online (Sandbox Code Playgroud)

变量b有:

xyz
pqr
stu
Run Code Online (Sandbox Code Playgroud)

我需要将输出作为

txyz
tpqr
tstu 
Run Code Online (Sandbox Code Playgroud)

mur*_*uru 7

在 bash 上,您可以使用进程替换和paste

$ a='t                                 
t
t'
$ b='xyz                               
pqr
stu'
$ paste <(echo "$a") <(echo "$b") -d ''
txyz
tpqr
tstu
Run Code Online (Sandbox Code Playgroud)

如果您想要的只是t, 行的前缀bawk或者sed会这样做:

$ printf "%s" "$b" | awk '{printf "t"}1'
txyz
tpqr
tstu
Run Code Online (Sandbox Code Playgroud)


hee*_*ayl 5

paste与进程替换一起使用:

paste -d '' <(echo "$a") <(echo "$b")
Run Code Online (Sandbox Code Playgroud)
  • <()是进程替换模式,里面的命令的输出将被文件描述符替换,这是需要的,因为paste需要将文件作为输入

  • d '' 根据需要将分隔符设置为 null

例子:

$ echo "$a"
t
t
t

$ echo "$b"
xyz
pqr
stu

$ paste -d '' <(echo "$a") <(echo "$b")
txyz
tpqr
tstu
Run Code Online (Sandbox Code Playgroud)