我有檔案1
(1'a lot of singapore 1.2.3.4 'some other info',0,null, 12345),
(2,'a lot of brazil 4.2.3.1, 'some other info',0,null, 12345),
(3,'a lot of india 3.4.2.1, 'some other info',0,null, 12345),
(4,'a lot of laos 1.3.4.5, 'some other info',0,null, 12345),
(5,'a lot of china 1.2.3.5, 'some other info',0,null, 12345);
和檔案2
(1'a lot of singapore A.B.C.D 'some other info',0,null, 12345),
(2,'a lot of brazil E.F.G.H, 'some other info',0,null, 12345),
(3,'a lot of india H.I.J.K, 'some other info',0,null, 12345),
(4,'a lot of laos L.M.N.O, 'some other info',0,null, 12345),
(5,'a lot of china P.Q.R.S, 'some other info',0,null, 12345);
我創建了一個腳本,但要復制并替換為行號,但需要輸入以SINGAPORE在檔案 1 中查找并復制下一個單詞1.2.3.4并singapore在 file2 中查找并從此處替換下一個單詞1.2.3.4-A.B.C.D最終的 file2 看起來像這樣
(1'a lot of singapore 1.2.3.4 'some other info',0,null, 12345),
Python 腳本或 Awk 或sed任何腳本都會有所幫助。
到目前為止,我已經創建了這個來復制和替換行號
sed -i '2d' File2.txt
awk 'NR==5380{a=$0}NR==FNR{next}FNR==2{print a}1' file1.txt file2.txt
uj5u.com熱心網友回復:
我不確定它會起作用,它是最好的解決方案,但你需要這樣的東西。
import re
def try_to_get_country_data(line, country):
line_parts = line.split(',')
part_with_data = line_parts[1]
if (match := re.search(f'.* {country} (.*)', part_with_data)) is not None:
return match.group(1)
return None
if __name__ == "__main__":
found_data = None
country = 'singapore'
with open('some_file.txt', 'r') as f:
for line in f:
if (found_data := try_to_get_country_data(line, country)) is not None:
break
if found_data is not None:
with open('second_file.txt', 'r') as f2:
data = f2.readlines()
for i, line in enumerate(data):
if (replaced_data := try_to_get_country_data(line, country)) is not None:
data[i] = line.replace(replaced_data, found_data)
break
with open('second_file.txt', 'w') as f2:
f2.writelines(data)
所以,我已經檢查過了,如果每條線的線型相同,它就可以作業。
uj5u.com熱心網友回復:
這是一個簡單的 Awk 腳本,用于從第一個輸入檔案中查找替換文本并替換第二個輸入檔案中的相應標記。
awk -v country="singapore" 'NR == FNR {
for (i=2; i<=NF; i ) if ($(i-1) == country) token = $i; next }
$0 ~ country { for(i=2; i<=NF; i ) if ($(i-1) == country) $i = token
} 1' file1 file2 >newfile2
當我們閱讀file1時,NR == FNR是真的。我們回圈輸入標記并檢查匹配的標記country;如果我們找到一個,我們設定token為那個值。這意味著如果 country 關鍵字有多個匹配項,則將提取第一個輸入檔案中的最后一個。
該next陳述句使 Awk 跳過此輸入檔案的其余腳本,因此file1僅讀取來自的行,而不會進一步處理。
如果我們一直到最后一行,我們現在正在閱讀file2. 如果我們看到一行包含關鍵字,我們在關鍵字之后對country關鍵字執行替換。(這要求關鍵字是一個孤立的標記,而不是較長單詞中的子字串等。)最后1會導致所有到達這一點的行都列印回標準輸出,從而生成第二個檔案的副本,并執行任何替換.
如果您對此處使用的資料格式有任何控制權,或許可以嘗試想辦法以一種不太隨意的 ad-hoc 格式(如 JSON)獲取輸入。
uj5u.com熱心網友回復:
如果您想要一個簡短的bash腳本并假設檔案的結構是不變的,您可以嘗試這樣的事情:
country="singapore"
a=$(grep "${country}" file0 | awk '{print $5}')
if [[ "${a}" ]]
then
b=$(grep -w "${country}" file1 | awk '{print $5}')
sed "s/${country} ${b}/${country} ${a}/g" file1
fi
在腳本的輸出下方找到:
(1'a lot of singapore 1.2.3.4 'some other info',0,null, 12345),
(2,'a lot of brazil E.F.G.H, 'some other info',0,null, 12345),
(3,'a lot of india H.I.J.K, 'some other info',0,null, 12345),
(4,'a lot of laos L.M.N.O, 'some other info',0,null, 12345),
(5,'a lot of china P.Q.R.S, 'some other info',0,null, 12345);
用于就地sed -i編輯。file1
為了避免多次讀取同一個檔案并降低一點可讀性,最初的方法可以很容易地重構如下:
country="singapore"
file0c=$(cat file0)
file1c=$(cat file1)
a=$(echo "${file1c}" | grep -w "${country}" | awk '{print $5}')
if [[ "${a}" ]]
then
b=$(echo "${file1c}" | grep -w "${country}" | awk '{print $5}')
echo "${file1c}" | sed "s/${country} ${b}/${country} ${a}/g" |
tee file1_new
fi
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/462217.html
