相关疑难解决方法(0)

使用printf格式的变量

假设我有一个这样的文件:

$ cat a
hello this is a sentence
and this is another one
Run Code Online (Sandbox Code Playgroud)

我想打印前两列,在它们之间有一些填充.由于这个填充可能会改变,我可以使用例如7:

$ awk '{printf "%7-s%s\n", $1, $2}' a
hello  this
and    this
Run Code Online (Sandbox Code Playgroud)

或者17:

$ awk '{printf "%17-s%s\n", $1, $2}' a
hello            this
and              this
Run Code Online (Sandbox Code Playgroud)

或者25,或......你明白了这一点:数字可能会有所不同.

然后弹出一个问题:是否可以为此分配变量N,而不是以%N-s格式对整数进行硬编码?

我尝试了这些事情没有成功:

$ awk '{n=7; printf "%{n}-s%s\n", $1, $2}' a
%{n}-shello
%{n}-sand

$ awk '{n=7; printf "%n-s%s\n", $1, $2}' a
%n-shello
%n-sand
Run Code Online (Sandbox Code Playgroud)

理想情况下,我想知道是否可以这样做.如果不是,那么最好的解决方法是什么?

awk printf awk-formatting

9
推荐指数
2
解决办法
5783
查看次数

使用bash水平排序数据

我在列中有一个充满数据的文件

sarah mark john
10    20   5
x     y    z 
Run Code Online (Sandbox Code Playgroud)

我想对数据进行排序,以便列保持不变,但第二行按递增顺序排列,所以它看起来像这样:

john sarah mark
5    10    20 
z    x     y 
Run Code Online (Sandbox Code Playgroud)

我一直在看sort命令,但只能找到垂直排序,而不是水平排序.我很高兴使用任何工具,任何帮助表示赞赏.谢谢!

sorting bash

3
推荐指数
1
解决办法
2887
查看次数

标签 统计

awk ×1

awk-formatting ×1

bash ×1

printf ×1

sorting ×1