簡體   English   中英

如何通過從Shell腳本中的文件讀取IP地址來構造字符串?

[英]How to construct a string by reading ip address from a file in shell script?

我有一個文本文件,它將具有這樣的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')

嘗試這樣做:

$ 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')

這個怎么樣 :

 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')

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM