简体   繁体   English

从文本文件到另一个字符串的字符串替换

[英]string substitution from text file to another string

I have a text file with three columns of text (strings) per line. 我有一个文本文件,每行三列文本(字符串)。 I want to create an SQL insert command by substituting each of the three strings into a skeleton SQL command. 我想通过将三个字符串中的每一个替换为框架SQL命令来创建SQL插入命令。 I have put place markers in the skeleton script and used SED s/placemarker1/first string/ but with no success. 我已将标记放置在框架脚本中,并使用了SED s / placemarker1 /第一个字符串/,但没有成功。 Is there an easier way to accomplish this task. 有没有更简单的方法来完成此任务。 I used pipes to repeat the process for 'second string' etc. I actually used awk to get the fields but could not convert to the actual values. 我使用管道对“第二个字符串”等重复该过程。我实际上使用awk获取字段,但无法转换为实际值。

enter code here
    for i in [ *x100* ]; do
    if [ -f "$i" ]; then {
        grep -e "You received a payment" -e "Transaction ID:" -e "Receipt No: " $i  >> ../temp
        cat ../temp | awk 'NR == 1 {printf("%s\t",$9)} NR == 2 {printf("%s\t",$9)}  NR == 3 {printf("%s\n",$3)}' | awk '{print $2,$1,$3}' | sed 's/(/ /' | sed 's/)./ /' >> ../temp1
        cat temp1 | awk 'email="$1"; transaction="$2"; ccreceipt="$3";'
        cat /home/linux014/opt/skeleton.sql | sed 's/EMAIL/"$email"/' | sed 's/TRANSACTION/"$transaction"/' | sed 's/CCRECEIPT/"$ccreceipt"/' > /home/linux014/opt/new-member.sql
        rm -f ../temp
    } fi
    done

I cannot figure out how to get the values instead of the names of the variables inserted into my string. 我无法弄清楚如何获取值,而不是插入字符串中的变量名。

Sample input (one line only): 样本输入(仅一行):

catdog@gmail.com 2w4e5r6t7y8u9i8u7 1111-2222-3333-4444

Sample actual output: 样本实际输出:

INSERT INTO users (email,paypal_tran,CCReceipt) VALUES ('"$email"','"$transaction"','"$ccreceipt"');

Preferred output: 首选输出:

INSERT INTO users (email,paypal_tran,CCReceipt) VALUES ('catdog@gmail.com','2w4e5r6t7y8u9i8u7','1111-2222-3333-4444');
awk '{print "INSERT INTO users (email,paypal_tran,CCReceipt) VALUES"; print "(\x27"$1"\x27,\x27"$2"\x27,\x27"$3"\x27);"}' input.txt

Converts your sample input to preferred output. 将样本输入转换为首选输出。 It should work for multi line input. 它应该适用于多行输入。

EDIT 编辑

The variables you are using in this line: 您在此行中使用的变量:

cat temp1 | awk 'email="$1"; transaction="$2"; ccreceipt="$3";'

are only visible to awk and in this command. 仅对awk和此命令可见。 They are not shell variables. 它们不是shell变量。 Also in your sed commands remove those single quotes then you can get the values: 同样,在您的sed命令中,删除那些单引号,然后您可以获得值:

sed "s/EMAIL/$email/"

You can try this bash , 你可以试试这个bash

while read email transaction ccreceipt; do echo "INSERT INTO users (email,paypal_tran,CCReceipt) VALUES ('$email','$transaction','$ccreceipt');"; done<inputfile

inputfile: 输入文件:

catdog@gmail.com 2w4e5r6t7y8u9i8u7 1111-2222-3333-4444
dog@gmail.com 2dsdsda53563u9i8u7 3333-4444-5555-6666

Test: 测试:

sat:~$ while read email transaction ccreceipt; do echo "INSERT INTO users (email,paypal_tran,CCReceipt) VALUES ('$email','$transaction','$ccreceipt')"; done<inputfile
INSERT INTO users (email,paypal_tran,CCReceipt) VALUES ('catdog@gmail.com','2w4e5r6t7y8u9i8u7','1111-2222-3333-4444')
INSERT INTO users (email,paypal_tran,CCReceipt) VALUES ('dog@gmail.com','2dsdsda53563u9i8u7','3333-4444-5555-6666')
You can write a small procedure for this

CREATE PROCEDURE [dbo].[appInsert]--
@string VARCHAR(500)
AS
BEGIN

DECLARE @I INT  
DECLARE @SubString VARCHAR(500)
SET @String = 'catdog@gmail.com 2w4e5r6t7y8u9i8u7 1111-2222-3333-4444'
SET @I = 1  
SET @String = REPLACE(@String, ' ', '`~`')

    WHILE @I > 0
        BEGIN
        SET @SubString = SUBSTRING (REVERSE(@String), 1, ( CHARINDEX( '`~`', REVERSE(@String)) - 1))
        SET @String = SUBSTRING(@String, 1, LEN(@String) - CHARINDEX( '`~`', REVERSE(@String)) - 2 )
        print REVERSE(@SubString) + ' === ' + @String
        SET @i = CHARINDEX( '`~`', @String)
        END

END

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM