如何使用其他文件中的文本命名文件?

时间:2017-10-10 20:05:00

标签: bash awk sed

我有一个文件,每行包含一个新的客户信息(电子邮件,名称,标题,账单),这些信息将被替换为模板文件,以便为账单超过付款的客户创建文件。我遇到的问题是?????所在的位置。我想将输出重定向到使用客户电子邮件命名的文件名,到目前为止我还没有能够做到这一点。我不知道如何解决这个问题,我已经尝试过多种方法,但它并没有起作用。

  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 个答案:

答案 0 :(得分:1)

您可以尝试在第17行的gawk命令周围添加另一个循环,并使用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;