
텍스트 파일을 통해 루프를 얻는 방법을 파악하고 모든 줄에 대해 이름, 프로젝트 번호 및 이메일 필드를 가져와서 전송할 이메일 템플릿에서 교체해야 합니다.
이것이 send.txt라는 텍스트 파일입니다.
Project 1,Jack,Chen,06,12,[email protected]
Project 2,Emily,Weiss,06,12,[email protected]
Project 3,Mary,Gonzalas,06,12,[email protected]
이는 Reminder.email이라는 이메일 템플릿입니다.
Dear __FULLNAME__:
This is a kindly reminder that our __Project__ meeting will be held on today.
Best Regards,
CIS5027
따라서 텍스트 파일의 모든 줄에 대해 이 이메일 템플릿의 필드를 바꿔야 합니다.전체 이름: , 그리고프로젝트. 첫 번째 줄에 대해 해당 값을 수행할 수 있지만 모든 줄에 대해 수행할 수는 없습니다.
이것은 내 스크립트입니다
#
!/bin/sh
#Start your code from here
date= date "+%m/%d/%y"
print $date
#The following line is to scan a file called Event-member.data and return any lines with todays date and save them to a file called sendtoday.txt
grep -n $(date +"%m,%d") Event-member.data > sendtoday.txt
#The following line is to remove the first to characters of the file created above sendtoday.txt and output that to a file called send.txt.
cat sendtoday.txt | sed 's/^..//' > send.txt
#This is where im having trouble. When storing the values for the variables below of name, email, project #. The value of NR==1 thus it never goes through the rest of the lines. I've tried different solutions but none seem to work.
p=$(awk -F ',' 'NR==1{print $1}' send.txt)
n=$(awk -F ',' 'NR==1{print $2}' send.txt)
l=$(awk -F ',' 'NR==1{print $3}' send.txt)
e=$(awk -F ',' 'NR==1{print $6}' send.txt)
echo $p $n $l $e
#This part is to replace the values in the email template using sed and save the modified template as sendnow.txt.
sed -e "s/__FULLNAME__:/\ $n $l :/g;s/__Project__/\ $p /g" Reminder.email > sendnow.txt
cat sendnow.txt
#Yet to be written ... send out modified email templates.
exit 0
########
이것이 생성하는 출력은 다음과 같습니다.
06/12/14
Project 1 Jack Chen [email protected]
Dear Jack Chen :
This is a kindly reminder that our Project 1 meeting will be held on today.
Best Regards,
CIS5027
보시다시피 필드를 적절하게 교체했지만 Jack Chen의 경우에만 해당됩니다. send.txt 파일에는 3개의 라인이 있으므로 위 템플릿의 3개 수정 버전이 있어야 합니다.
답변1
awk
이것을 사용할 이유가 없습니다 . 다음을 사용하여 쉘에서 직접 수행할 수 있습니다.read
. 일반적인 형식은 read foo bar
첫 번째 필드를 로 저장 $foo
하고 각 줄의 나머지 부분을 로 저장하는 것입니다 $bar
. 따라서 귀하의 경우 다음과 같은 작업을 수행합니다.
while IFS="," read p n l foo bar e; do
sed -e "s/__FULLNAME__:/\ $n $l :/g;s/__Project__/\ $p /g" Reminder.email;
done < file
IFS
쉼표로 구분된 필드를 읽 도록 설정된 경우 입력 필드 구분 기호 입니다 ,
. 이를 통해 각 필드를 가져와 변수에 저장할 수 있습니다. 두 개의 추가 변수 foo
와 bar
. 이는 각 필드마다 고유한 변수 이름이 필요하고 6개의 필드가 있기 때문입니다. 4개의 변수 이름만 제공하는 경우 4번째( $e
)에는 4부터 마지막까지의 필드가 포함됩니다.
이제 스크립트에 다양한 기타 구문 오류가 있습니다. 우선 shebang 줄이 잘못되었습니다. 가 필요합니다. 와 #! /bin/sh
사이에 빈 줄이 있어서는 안 됩니다 . 또한, 할당을 위해서는#!
/bin/sh
산출명령을 변수로 지정하려면 var=`command`
또는 var=$(command)
형식을 사용해야 합니다. 그렇지 않으면 명령 자체가 문자열이 아닌 출력이 변수에 할당됩니다. 마지막으로,print
당신이 생각하는 그런 것이 아닙니다. 당신은 printf
또는 을 찾고 있습니다 echo
. 따라서 스크립트를 작성하는 더 좋은 방법은 다음과 같습니다.
#!/bin/sh
date=$(date "+%m/%d/%y")
echo $date
## The following line is to scan a file called Event-member.data
## and return any lines with todays date and save them to a file
## called sendtoday.txt
grep -n $(date +"%m,%d") Event-member.data > sendtoday.txt
## The following line is to remove the first to characters of the file
## created above sendtoday.txt and output that to a file called
## send.txt.
## I rewrote this to avoid the useless use of cat.
sed 's/^..//' sendtoday.txt > send.txt
## This is where you use read
while IFS="," read p n l foo bar e; do
sed -e "s/__FULLNAME__:/\ $n $l :/g;s/__Project__/\ $p /g" Reminder.email > sendnow.txt
cat sendnow.txt
## This is where you need to add the code that sends the emails. Something
## like this:
sendmail $e < sendnow.txt
done < send.txt
exit 0
########
답변2
NR==1 조건을 사용했습니다 NR==1{print $1}
. 이는 의 첫 번째 줄을 고려한다는 의미입니다 send.txt
. 두 번째 라인 등을 얻으려면 NR==2 조건을 사용하십시오. 또는 루프를 사용하여 다음과 같은 모든 줄을 살펴봅니다.
while read line
do
p=`echo $line | awk -F '.' '{print $1}'`
n=`echo $line | awk -F '.' '{print $2}'`
l=`echo $line | awk -F '.' '{print $3}'`
e=`echo $line | awk -F '.' '{print $1}'`
sed -e "s/\__FULLNAME\__:/\ $n $l :/g;s/\__Project__/\ $p /g" Reminder.email > sendnow.txt
done<send.txt
답변3
어쩌면 스크립트를 다음과 같이 감쌀 수도 있습니다.
for i in $(cat send.txt); do echo "line: $i"; done
?
답변4
cat xtmp | awk '{s++; do_aything_you_want_here }'
**s++ 각 줄을 살펴봅니다 **
cat xtmp
111
222
333
444
cat xtmp | awk '{s++; print($1,"QQQ") }'
111 QQQ
222 QQQ
333 QQQ
444 QQQ
cat xtmp | awk '{s++; print($1+3,"QQQ") }'
114 QQQ
225 QQQ
336 QQQ
447 QQQ
cat xtmp | awk '{s++; for(i=1; i<=4 ; i++) print $1 }' # this will repeat each line 4 time
cat tmp | awk '{s++; a=a+$1; print(s" "$1" "a)}' instead of a=a+$1 you can use a+=$1
1 111 111
2 222 333
3 333 666
4 444 1110