AWK菜单shell脚本

时间:2016-01-14 11:33:41

标签: bash shell unix

我需要有关shell脚本中awk print f的帮助。

请参阅下面的我的脚本

#!/bin/sh
date_time=`date '+%d-%b-%Y_%H:%M:%S'`
usr="test"

echo "Hai" | awk -V '{
printf("==========================================\n");
printf("|%10s|%10s|\n","Date" $date_time);
printf("|%10s|%10s|\n","user" $usr);
printf("==========================================\n");
}'

我想要输出如下

==============================
| date | 14-Jan-2016_16:49:40|
| user | Test                |
==============================

1 个答案:

答案 0 :(得分:1)

您可能正在寻找:

#!/bin/sh
date_time=`date '+%d-%b-%Y_%H:%M:%S'`
usr="test"
printf "==================================\n"
printf "|%10s |%20s| \n" "Date" "$date_time"
printf "|%10s |%-20s| \n" "User" "$usr"  # here - before 20 is for left align
printf "==================================\n"

如果由于某种原因awk是强制性的,请尝试以下内容:

#!/bin/sh
date_time=`date '+%d-%b-%Y_%H:%M:%S'`
usr="test"
awk -v date_time="$date_time" -v usr="$usr" 'BEGIN{
printf "==================================\n"
printf "|%10s |%20s|\n","Date",date_time
printf "|%10s |%-20s|\n","User",usr
printf "==================================\n"
}' </dev/null