0

I'm having some problems running my current program. I need to take a file of customers information (each line is a new customer) and substitute it into a template. Each customer info will created a file from the template and will be named using the customer's email and placed into the Email subdirectory using awk and sed. Right now my program is not 'doing anything' except printing the line ./test.bash: line 19: Emails/: Is a directory .

  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/$email"
 20 done;

awk file

  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 END{}

1 Answers1

2

The line

sed "$detail" "$template" > "$OUTPUT_DIR/$email"

uses the parameter email which is defined for awk only what does not affect the shell environment.

Hauke Laging
  • 90,279
  • I see what you mean about the shell environment variables. Since Im using awk to set the variables and to create a sed file, is there anyway I could get the customers email from the file while doing that, or would I maybe have to run a read loop on the file at the same time also to get each individual customers email were the >"OUTPUT_DIR/$email" is? @Hauke Laging – below_avg_st Oct 10 '17 at 18:01
  • Perhaps a new awk command to print the email or a grep command? – below_avg_st Oct 10 '17 at 18:09
  • @below_avg_st what you probably are looking for here is source: https://ss64.com/bash/source.html This runs the file in the shell that called the source command as opposed to a subshell. A benefit of this is that if you have a file that just sets variables, you can use source to pull them all into a different shell. – Thegs Oct 10 '17 at 18:11
  • @below_avg_st Why would you use awk at all in order to generate sed commands? awk is the mightier tool and can do the sed work. – Hauke Laging Oct 10 '17 at 19:32
  • My teacher wants us to use only awk, sed, and bash in this practice example. – below_avg_st Oct 10 '17 at 19:34
  • You can demonstrate to your teacher that only awk is required. And teach him about UUOS and UUOB (Useless Use of Sed , Useless Use of Bash), in regard to UUOC. Imagine your file contains thousand and thousand of lines, you solution will be very slow due to thousand and thousand of sed process spawning. School is good time to learn about elegancy and efficiency. If i had a very bad mind i would hope for you, you didn't pay much for such a poor teaching. – netmonk Oct 10 '17 at 21:43
  • @below_avg_st To me "to use only awk, sed, and bash" does not sound like "have to use both awk and sed and bash". – Hauke Laging Oct 11 '17 at 07:30
  • This look like part of a "fill templates with parameters defined in another file" task. See this question for an example how to solve this with sed only. – Philippos Oct 11 '17 at 07:41