文字附加到每个文件中的多行 [英] Append text to each of multiple lines in file

查看:124
本文介绍了文字附加到每个文件中的多行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个查询将被使用的唯一标识符的数千行(28,000奇更准确)执行。
这是(简体)查询:

I have a query which is to be performed on thousands of rows (28,000 odd to be more exact) using a unique identifier. This is (simplified) the query:

update "table1" set event_type = 'NEW' where id=

和有一个文件 ids.txt 它包含的行更新要执行的IDS:

And there is a file ids.txt which contains the ids for the rows the update is to be performed on:

10003
10009
....
....
79345
79356

生成的文件应该是这样的:

The resultant file should be like this:

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的查询,有一个简单的方法要做到这一点?

Other than taking the ids.txt file and using vim to form all the 28000 queries using global string substitution, is there an easy way to do this?

推荐答案

使用一些简单的像 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

previous答案

我用的方法基于bash的循环,而这是一个有点overkilling。请参阅相关的问题:为什么使用shell循环来处理文本被认为是不好的做法

循环是这样的:

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;

这篇关于文字附加到每个文件中的多行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆