linux - 将文本附加到文件中的多行的每一行

标签 linux bash vim awk

我有一个查询,要使用唯一标识符对数千行(更准确地说是 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/

相关文章:

linux - Yum 失败条件

java - Netbeans 无法在 Ubuntu 14.10 中打开

linux - 将变量赋值给 if 语句中的变量

bash - 针对 bash 单维数组限制的更简洁的解决方法

python - pymongo 的 vim 自动完成

vim - 普通模式下的 Vi 组独立命令

linux - 使用单一模式字符串匹配多个单词Lua脚本

在本地临时目录中编译目标文件

linux - bash 脚本正确解释 awk

windows - 如何在 gVim (Win32) 中缩进选择