从一行生成列[bash]
有这样一行文字:从一行生成列[bash],bash,shell,awk,sed,Bash,Shell,Awk,Sed,有这样一行文字: a b c d e f ... 我想要这个: a b c d e f ... 我正在尝试使用sed,awk,for循环,但我没有得到它 你能帮我吗 谢谢 awk肯定能做到。但是对于给定的示例和预期结果,我将使用xargs: xargs -n2 -a file 测试: 我假设空格之间可能有不止一个字符,所以 $ cat tmp.txt a b c d e f g h i j k l m n o p q r s t u v w x y z $ cat tmp.txt |
a b c d e f ...
我想要这个:
a b
c d
e f
...
我正在尝试使用sed,awk,for循环,但我没有得到它
你能帮我吗
谢谢 awk肯定能做到。但是对于给定的示例和预期结果,我将使用
xargs
:
xargs -n2 -a file
测试:
我假设空格之间可能有不止一个字符,所以
$ cat tmp.txt
a b c d e f g h i j k l m n o p q r s t u v w x y z
$ cat tmp.txt | sed 's/\([^ ]* [^ ]*\) /\1\n/g'
a b
c d
e f
g h
i j
k l
m n
o p
q r
s t
u v
w x
y z
使用rs
:
$ echo a b c d e f ... | rs -C\ 0 2
a b
c d
e f
...
NAME
rs — reshape a data array
SYNOPSIS
rs [-C[x]] [rows [cols]]
DESCRIPTION
...
-C[x] Output columns are delimited by the single character x.
A missing x is taken to be ‘^I’.
man rs
:
$ echo a b c d e f ... | rs -C\ 0 2
a b
c d
e f
...
NAME
rs — reshape a data array
SYNOPSIS
rs [-C[x]] [rows [cols]]
DESCRIPTION
...
-C[x] Output columns are delimited by the single character x.
A missing x is taken to be ‘^I’.
仅使用
bash
:
$ IFS=$' \t' read -ra a <<< "a b c d e f"
$ printf '%s %s\n' "${a[@]}"
a b
c d
e f
$IFS=$'\t'阅读-ra了解为什么你会获得否决票,你的问题可能最终会被关闭。可能会有帮助