我一直在尝试解析Paypal电子邮件并将结果信息插入我的数据库。 我的大部分代码都在工作,但是我无法获取要插入到awk代码中的变量来创建sql插入查询。

if [  -f email-data.txt ]; then {
    grep  -e "Transaction ID:" -e "Receipt No: " email-data.txt \
      >> ../temp
    cat ../temp \
      | awk 'NR == 1 {printf("%s\t",$NF)}  NR == 2 {printf("%s\n",$NF)}' \
      >> ../temp1
    awk '{print $1}' $email-data.txt \
      | grep @ \
      | grep -v \( \
      | grep -v href \
      >> ../address
    email_addr=$(cat ../address)
    echo  $email_addr 
    cat ../temp1 \
      | awk '{print "INSERT INTO users (email,paid,paypal_tran,CCReceipt) VALUES"; print "(\x27"($email_addr)"\x27,'1',\x27"$2"\x27,\x27"$3"\x27);"}' \
      > /home/linux014/opt/post-new-member.sql

输出如下所示

INSERT INTO users (email,paid,paypal_tran,CCReceipt) VALUES('9MU013922L4775929  9MU013922L4775929',1,'9MU013922L4775929','');

应该看起来像

INSERT INTO users (email,paid,paypal_tran,CCReceipt) VALUES('dogcat@gmail.com',1,'9MU013922L4775929','1234-2345-3456-4567');

(为了保护无辜者而改名)
我正在使用的试用数据如下

Apr 18, 2014 10:46:17 GMT-04:00 | Transaction ID: 9MU013922L4775929

You received a payment of $50.00 USD from Dog Cat (dogcat@gmail.com)
Buyer:
Dog Cat
dogcat@gmail.com

Purchase Details 

Receipt No: 1234-2345-3456-4567

我无法弄清楚为什么未正确插入email-addr。

===============>>#1 票数:1 已采纳

您正在awk内调用shell变量。 正确的方法是使用-v选项创建awk变量。

例如,假设$email是您的shell变量,然后

... | awk -v awkvar="$email" '{do something with awkvar}' ...

阅读以获得更多详细信息。

但是,话虽如此,这就是我尝试解析文本文件的方式:

awk '
/Transaction ID:/ { tran  =  $NF }
/Receipt No:/     { receipt = $NF }
$1 ~ /@/          { email  =  $1 }
END {
     print "INSERT INTO users (email,paid,paypal_tran,CCReceipt) VALUES"; 
     print "("q email q","1","q tran q","q receipt q");"
}' q="'" data.txt

输出:

INSERT INTO users (email,paid,paypal_tran,CCReceipt) VALUES
('dogcat@gmail.com',1,'9MU013922L4775929','1234-2345-3456-4567');

  ask by user3386373 translate from so

未解决问题?本站智能推荐: