格式化 awk 命令的输出

格式化 awk 命令的输出

我必须格式化 awk 命令的输出,该命令从文件获取输入

Sample Input File

123 ABC 12 23 34 45 56 67 78 13
234 DEF 23 34 45 56 67 78 89
345 GHI 34 45 56 67 78 90 10 01 99
456 JKL 45 56 67 78 90 12 34

Required Output

user | 123 ABC 12 | 23 34 45 56 67 78 13
user | 234 DEF 23 | 34 45 56 67 78 89
user | 345 GHI 34 | 45 56 67 78 90 10 01 99
user | 456 JKL 45 | 56 67 78 90 12 34

我尝试了下面的命令,这给了我一些错误

awk -v user=$user '{ printf user "|" $1" "$2" "$3 "|" "$1=$2=$3=""" $0;printf("\n"); }' sampleinputfile

Error Message

user | 123 ABC 12 | 23 34 45 56 67 78 13
user | 234 DEF 23 | 34 45 56 67 78 89
user | 345 GHI 34 | 45 56 67 78 90 10 01 99
user | 456 JKL 45 | 56 67 78 90 12 34
awk: (FILENAME=sampleinputfile FNR=4) fatal: not enough arguments to satisfy format string
        `user | 456 JKL 45|$1=$2=$3=456 JKL 45 56 67 78 90 12 34
                                                      ^ ran out for this one

答案1

单程:

awk '{$3=$3" | ";print "user | "$0;}' file

相关内容