gpt4 book ai didi

linux - 如何通过从 shell 脚本中的文件中读取 ip 地址来构造字符串?

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:36:57 25 4
gpt4 key购买 nike

我有一个文本文件,它的 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 字符串

最佳答案

首先,您的文件只包含一列,而您正在读取两列(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')

关于linux - 如何通过从 shell 脚本中的文件中读取 ip 地址来构造字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27534625/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com