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)
在 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
, 行的前缀b
,awk
或者sed
会这样做:
$ printf "%s" "$b" | awk '{printf "t"}1'
txyz
tpqr
tstu
Run Code Online (Sandbox Code Playgroud)
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)
归档时间: |
|
查看次数: |
1426 次 |
最近记录: |