使用printf awk函数输出第n个参数

时间:2011-09-22 13:39:45

标签: shell awk

给出以下文件的内容:

alias command with whitespace-separated argument list
anotheralias othercommand and its arguments

我怎么能打印像:

       alias = command with whitespace-separated argument list
anotheralias = othercommand and its arguments

目前我正在使用以下命令,但这是错误的。

cat aliases | awk '{printf "%20s = %s\n", $1, $0}'

4 个答案:

答案 0 :(得分:3)

cat aliases | awk '{$1=sprintf("%20s =",$1);print}'

答案 1 :(得分:1)

cat aliases | awk '{ printf("%20s =", $1); $1=""; printf("%s\n", $0) }'

答案 2 :(得分:0)

另一种方式:

sed 's/ /=/1' yourFile|awk -F= '{printf ("%20s = %s\n",$1,$2)}'

答案 3 :(得分:0)

只需使用shell:

while read line; do 
  set -- $line
  cmd=$1
  shift
  printf "%20s = %s\n" "$cmd" "$*"
done < filename