我有一个文本文件,其中包含IP地址 -
1.2.3.4
4.5.6.67
9.10.23.40
11.12.3.4
4.15.16.67
19.10.23.40
现在我需要阅读上面的文本文件,并为每个IP地址构造下面的sql字符串 - 正如您所看到的,只有IP地址部分正在发生变化,除此之外一切都是相同的。
insert into some_table (id, ip_address, last_modified_date, customer_name) values (1, '1.2.3.4', dateOf(now()), 'hello')
insert into some_table (id, ip_address, last_modified_date, customer_name) values (1, '4.5.6.67', dateOf(now()), 'hello')
insert into some_table (id, ip_address, last_modified_date, customer_name) values (1, '9.10.23.40', dateOf(now()), 'hello')
insert into some_table (id, ip_address, last_modified_date, customer_name) values (1, '11.12.3.4', dateOf(now()), 'hello')
insert into some_table (id, ip_address, last_modified_date, customer_name) values (1, '4.15.16.67', dateOf(now()), 'hello')
insert into some_table (id, ip_address, last_modified_date, customer_name) values (1, '19.10.23.40', dateOf(now()), 'hello')
在shell脚本中执行此操作的最佳方法是什么?
更新: -
这是我试过的 -
#!/bin/bash
while read host rest; do
# not sure what should I do here
EOF
done < ipList.txt
wait
我无法理解在获取IP地址后如何创建sql字符串
答案 0 :(得分:2)
首先,您的文件只包含一列,而您正在阅读两列(read host rest
)。
第二,什么是dateOf(now()),我的意思是,它是一个bash函数吗?
可能的解决方案如下:
while read ip; do
echo "insert into some_table (id, ip_address, last_modified_date, customer_name) values (1, '$ip', dateOf(now()), 'hello')"
done < ipList.txt
如果dateOf(now())
是bash函数,您可以将结果存储在变量中,然后以下列方式使用:
function dateOfNow() {
echo -n $(date); # Just an example :)
}
while read ip; do
d=$(dateOfNow)
echo "insert into some_table (id, ip_address, last_modified_date, customer_name) values (1, '$ip', $d), 'hello')"
done < ipList.txt
示例输出:
insert into some_table (id, ip_address, last_modified_date, customer_name) values (1, '1.2.3.4', Wed Dec 17 15:05:55 CST 2014), 'hello')
insert into some_table (id, ip_address, last_modified_date, customer_name) values (1, '4.5.6.67', Wed Dec 17 15:05:55 CST 2014), 'hello')
insert into some_table (id, ip_address, last_modified_date, customer_name) values (1, '9.10.23.40', Wed Dec 17 15:05:55 CST 2014), 'hello')
insert into some_table (id, ip_address, last_modified_date, customer_name) values (1, '11.12.3.4', Wed Dec 17 15:05:55 CST 2014), 'hello')
insert into some_table (id, ip_address, last_modified_date, customer_name) values (1, '4.15.16.67', Wed Dec 17 15:05:55 CST 2014), 'hello')
答案 1 :(得分:1)
尝试这样做:
$ while read ip; do printf "insert into some_table (id, ip_address, last_modified_date, customer_name) values (1, '%s', dateOf(now()), 'hello')\n" $ip; done < file
insert into some_table (id, ip_address, last_modified_date, customer_name) values (1, '1.2.3.4', dateOf(now()), 'hello')
insert into some_table (id, ip_address, last_modified_date, customer_name) values (1, '4.5.6.67', dateOf(now()), 'hello')
insert into some_table (id, ip_address, last_modified_date, customer_name) values (1, '9.10.23.40', dateOf(now()), 'hello')
insert into some_table (id, ip_address, last_modified_date, customer_name) values (1, '11.12.3.4', dateOf(now()), 'hello')
insert into some_table (id, ip_address, last_modified_date, customer_name) values (1, '4.15.16.67', dateOf(now()), 'hello')
insert into some_table (id, ip_address, last_modified_date, customer_name) values (1, '19.10.23.40', dateOf(now()), 'hello')
答案 2 :(得分:1)
这个怎么样:
cat ipList.txt | xargs -n1 -I % echo "insert into some_table (id, ip_address, last_modified_date, customer_name) values (1, '%', dateOf(now()), 'hello')"
输出:
insert into some_table (id, ip_address, last_modified_date, customer_name) values (1, '1.2.3.4', dateOf(now()), 'hello')
insert into some_table (id, ip_address, last_modified_date, customer_name) values (1, '4.5.6.67', dateOf(now()), 'hello')
insert into some_table (id, ip_address, last_modified_date, customer_name) values (1, '9.10.23.40', dateOf(now()), 'hello')
insert into some_table (id, ip_address, last_modified_date, customer_name) values (1, '11.12.3.4', dateOf(now()), 'hello')
insert into some_table (id, ip_address, last_modified_date, customer_name) values (1, '4.15.16.67', dateOf(now()), 'hello')
insert into some_table (id, ip_address, last_modified_date, customer_name) values (1, '19.10.23.40', dateOf(now()), 'hello')