【发布时间】:2014-08-20 14:37:04
【问题描述】:
假设我有一个这样的文件:
$ cat a
hello this is a sentence
and this is another one
我想打印前两列并在它们之间添加一些填充。由于此填充可能会发生变化,因此我可以使用 7:
$ awk '{printf "%7-s%s\n", $1, $2}' a
hello this
and this
或17:
$ awk '{printf "%17-s%s\n", $1, $2}' a
hello this
and this
或者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
理想情况下,我想知道是否可以这样做。如果不是,最好的解决方法是什么?
【问题讨论】:
-
哦! fedorqui 问了一个问题!