我有一个查询,要使用唯一标识符对数千行(更准确地说是 28,000 行)执行查询。 这是(简化的)查询:
update "table1" set event_type = 'NEW' where id=
还有一个文件 ids.txt
,其中包含要执行更新的行的 ID:
10003
10009
....
....
79345
79356
生成的文件应如下所示:
update "table1" set event_type = 'NEW' where id=10003;
update "table1" set event_type = 'NEW' where id=10009;
...
update "table1" set event_type = 'NEW' where id=79356;
除了之外,除了获取 ids.txt
文件并使用 vim 使用全局字符串替换形成所有 28000 个查询之外,还有其他简单的方法吗?
最佳答案
使用简单的东西,比如sed
:
sed -r 's/^([0-9]*)$/update "table1" set event_type = \'NEW\' where id=\1/' file
| write back using \1 as placeholder
catch digits in the file
上一个答案
我使用了基于 bash 循环的方法,但这有点矫枉过正。查看相关问题:Why is using a shell loop to process text considered bad practice?
像这样循环:
while read id
do
echo "update \"table1\" set event_type = 'NEW' where id=${id};"
done < ids.txt
它输出:
$ while read id; do echo "update \"table1\" set event_type = 'NEW' where id=${id};"; done < ids
update "table1" set event_type = 'NEW' where id=10003;
update "table1" set event_type = 'NEW' where id=10009;
...
update "table1" set event_type = 'NEW' where id=79345;
update "table1" set event_type = 'NEW' where id=79356;
关于linux - 将文本附加到文件中的多行的每一行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20123155/