awk for 迴圈檔案中的每一行

awk for 迴圈檔案中的每一行

我需要弄清楚如何循環遍歷文字文件,並為每一行獲取 name 、project # 和 email 字段,並將它們替換到要發送的電子郵件範本中。

這是名為 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

正如你所看到的,它確實正確地取代了字段,但僅限於陳傑克。 send.txt 檔案中有 3 行,因此上述範本必須有 3 個修改版本。

答案1

沒有理由使用awk它。您可以直接在 shell 中使用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輸入字段分隔符,當設定為,讀取逗號分隔字段時。這使您可以獲取每個字段並將其存儲在變數中。請注意,我使用了兩個額外的變數foobar。這是因為每個欄位都需要自己的變數名,而您有 6 個欄位。如果您只給 4 個變數名稱,則第 4 個 ( $e) 將包含欄位 4 到最後一個。


現在,您的腳本中存在各種其他語法錯誤。首先shebang行是錯的,你需要,在和之間#! /bin/sh不能有空行。另外,為了分配#!/bin/sh輸出將指令傳遞給變數時,需要使用var=`command`or ,最好使用var=$(command)格式。否則,命令本身作為字串而不是其輸出分配給變數。最後,print不是你想的那樣。您正在尋找printfecho。因此,編寫腳本的更好方法是:

#!/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

相關內容