2017-10-10 71 views
0

我有一个文件,每行包含一个新的客户信息(电子邮件,名称,标题,账单),这些信息将被替换到模板文件中,为账单超出付款的客户创建文件。我遇到的问题是?????所在的位置。我想将输出重定向到一个使用客户电子邮件命名的文件名,到目前为止我还没有做到这一点。我不知道如何去做这件事,我已经尝试过很多东西,但它没有奏效。如何使用来自其他文件的文本命名文件?

1 #!/bin/bash 
    2 set -e 
    3 
    4 if [ "$#" -ne 1 ]; then 
    5  echo "Illegal number of parameters. Usage;./test1.bash <mm/dd/yyyy>" 
    6  exit 1 
    7 fi 
    8 
    9 OUTPUT_DIR="Emails" 
10 details="p4Customer.txt" 
11 template="template.txt" 
12 date=$1 
13 if [ ! -d "$OUTPUT_DIR" ]; then 
14  mkdir -p "$OUTPUT_DIR" 
15 fi 
16 
17 gawk -f g2.awk -v date="$date" "$details" | while read detail; 
18 do 
19  sed "$detail" "$template" > "OUTPUT_DIR/??????" 
20 done; 

AWK文件

1 BEGIN{ FS=",";} 
    2 { 
    3  email=$1; 
    4  fullname=$2; 
    5  title=$3; 
    6  n=split(fullname,A," "); 
    7  name=A[n]; 
    8  amount_owed=$5; 
    9  if($5>$4){ 
10   printf "s_EMAIL_%s_;s_FULLNAME_%s_;s_TITLE_%s_;s_NAME_%s_;s_AMOUNT_%s_;s_DATE_%s_\n" , email, fullname, title, name, amount_owed, date; 
11  } 
12 } 
13 END{} 

回答

1

你可以尝试把另一个循环围绕gawk命令第17行,并使用cut命令,让你在$details文件中的第一场。因此,您的bash脚本中的第17-20行将替换为:

# assume the first field is the email 
for email in `cut -d, -f 1 $details`; 
    do 
    gawk -f g2.awk -v date="$date" "$details" | while read detail; 
    do 
    sed "$detail" "$template" > "OUTPUT_DIR/$email" 
    done; 
done; 
相关问题