如何在Unix中格式化文件的数据? [英] How to format the data of a file in Unix?
本文介绍了如何在Unix中格式化文件的数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一些这种形式的数据(3列)存储在变量 ABC
中:
I have some data in this form (3 columns) stored in the variable ABC
:
d1 d2 d3
d4 d5 d6
d7 d8 d9
d10
,我想用这种格式(4列)对其进行格式化:
and I would like to format it in this form (4 columns):
d1 d2 d3 d4
d5 d6 d7 d8
d9 d10
我尝试过类似
printf "%8.3e %8.3e %8.3e %8.3e\n" "${ABC}"
,但是它不起作用。谁能看到问题出在哪里?
but it doesn't work. Can anyone see where the problem is?
推荐答案
所以您有一个文件,其内容如下:
So you have a file with a content like this:
d1 d2 d3
d4 d5 d6
d7 d8 d9
d10
,而您想将其转换为
d1 d2 d3 d4
d5 d6 d7 d8
d9 d10
将其从每行3列转换为4。
That is, convert it from 3 columns per line into 4.
为此,您可以使用 xargs
像这样:
For this you can use xargs
like:
xargs -n 4 < file
或者,如果数据在变量中:
Or, if the data is in a variable:
xargs -n 4 <<< "$variable"
来自 man xargs
-n max-args,--max-args = max-args
-n max-args, --max-args=max-args
在每个命令行中最多使用max-args个参数。
Use at most max-args arguments per command line.
Test
Test
$ cat a
1 2 3
4 5 6
7 8 9
10
$ xargs -n 4 < a
1 2 3 4
5 6 7 8
9 10
这篇关于如何在Unix中格式化文件的数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文