我想用下面的command_line01更換的第一和第二次出現home_cool與第一線1.txt分別在每種情況下,起訴程式,更換的第一和第二次出現home_cool01的第一行還1.txt單獨,然后..,更換home_cool 的第三次和第四次出現,第二行1.txt分別是., 等等,即每第 2 n次出現home_coolor home_cool01,分別用 的第 n 行替換這兩個字串1.txt。
我試過command_line01如下:
awk 'NR==FNR {a[NR]=$0; next} /home_cool01/{gsub("home_cool01", a[ i<3])} /home_cool/{gsub("home_cool", a[ j<3])} 1' 1.txt 0.txt > 2.txt
但這僅適用于在第一兩次出現在未來兩年出現home_cool或者home_cool01被替換什么作為顯示波紋管:
"#sun\t",
"car_snif = house.group_tree((food, apple, sky, cat,blue,)(bag, tortoise,))\t",
"machine(shoes_shirt.shop)\t",
"car_snif = house.group_tree((food, apple, sky, cat,blue,)(bag, tortoise,))\t",
"machine(shoes_shirt.shop)\t",
"car_snif = house.group_tree()\t",
"machine(shoes_shirt.shop)\t",
"car_snif = house.group_tree()\t",
"machine(shoes_shirt.shop)\t",
"car_snif = house.group_tree((food, apple, sky, cat,blue,)(bag, tortoise,))\t",
"machine(shoes_shirt.shop)\t",
"car_snif = house.group_tree((food, apple, sky, cat,blue,)(bag, tortoise,))\t",
"machine(shoes_shirt.shop)\t",
"car_snif = house.group_tree()\t",
"machine(shoes_shirt.shop)\t",
"car_snif = house.group_tree()\t",
"machine(shoes_shirt.shop)\t",
這是我的兩個源檔案:
0.txt:
"#sun\t",
"car_snif = house.group_tree(home_cool)\t",
"machine(shoes_shirt.shop)\t",
"car_snif = house.group_tree(home_cool)\t",
"machine(shoes_shirt.shop)\t",
"car_snif = house.group_tre(home_cool)\t",
"machine(shoes_shirt.shop)\t",
"car_snif = house.group_tree(home_cool)\t",
"machine(shoes_shirt.shop)\t",
"car_snif = house.group_tree(home_cool01)\t",
"machine(shoes_shirt.shop)\t",
"car_snif = house.group_tree(home_cool01)\t",
"machine(shoes_shirt.shop)\t",
"car_snif = house.group_tree(home_cool01)\t",
"machine(shoes_shirt.shop)\t",
"car_snif = house.group_tree(home_cool01)\t",
"machine(shoes_shirt.shop)\t",
和
1.txt:
(food, apple, sky, cat,blue,)(bag, tortoise,)
(food, apple, sky, cat,blue,)(bag,)
(food, apple, sky, cat,blue,)(bag, moon, tortoise,)
我的愿望輸出2.txt是:
"#sun\t",
"car_snif = house.group_tree((food, apple, sky, cat,blue,)(bag, tortoise,))\t",
"machine(shoes_shirt.shop)\t",
"car_snif = house.group_tree((food, apple, sky, cat,blue,)(bag, tortoise,))\t",
"machine(shoes_shirt.shop)\t",
"car_snif = house.group_tree((food, apple, sky, cat,blue,)(bag,))\t",
"machine(shoes_shirt.shop)\t",
"car_snif = house.group_tree((food, apple, sky, cat,blue,)(bag,))\t",
"machine(shoes_shirt.shop)\t",
"car_snif = house.group_tree((food, apple, sky, cat,blue,)(bag, tortoise,))\t",
"machine(shoes_shirt.shop)\t",
"car_snif = house.group_tree((food, apple, sky, cat,blue,)(bag, tortoise,))\t",
"machine(shoes_shirt.shop)\t",
"car_snif = house.group_tree((food, apple, sky, cat,blue,)(bag,))\t",
"machine(shoes_shirt.shop)\t",
"car_snif = house.group_tree((food, apple, sky, cat,blue,)(bag,))\t",
"machine(shoes_shirt.shop)\t",
編輯:來自@markp-fuso 評論:
1 - 如果有多個條目,如果已??經完成了第一個條目的處理,我會從第二個條目重新啟動。
2 - 如果我有超過 6home_coool個條目......我從1.txt.
3 - 我不希望僅限于兩種研究模式,因此當存在home_coool02, home_coool03, ...,時它應該是一個適當的解決方案home_coool_some_sufix,但我需要保留我最初發布的 MWE
uj5u.com熱心網友回復:
假設:
- 只需要擔心搜索模式
home_cool和home_cool01(可以添加更多,但需要一些返工;可能使用關聯陣列來跟蹤每個唯一模式的計數) - 更換圖案將應用兩次,然后才能進入下一個更換圖案
- 如果我們到達替換模式的末尾,我們會從頭開始
樣本輸入:
$ cat 0.txt
"#sun\t",
"car_snif = house.group_tree(home_cool)\t",
"machine(shoes_shirt.shop)\t",
"car_snif = house.group_tree(home_cool)\t",
"machine(shoes_shirt.shop)\t",
"car_snif = house.group_tree(home_cool)\t",
"machine(shoes_shirt.shop)\t",
"car_snif = house.group_tree(home_cool)\t",
"machine(shoes_shirt.shop)\t",
"car_snif = house.group_tree(home_cool)\t",
"machine(shoes_shirt.shop)\t",
"car_snif = house.group_tree(home_cool01)\t",
"machine(shoes_shirt.shop)\t",
"car_snif = house.group_tree(home_cool01)\t",
"machine(shoes_shirt.shop)\t",
"car_snif = house.group_tree(home_cool01)\t",
"machine(shoes_shirt.shop)\t",
"car_snif = house.group_tree(home_cool01)\t",
"machine(shoes_shirt.shop)\t",
"car_snif = house.group_tree(home_cool)\t", # pick up where we left off with 'home_cool'
"machine(shoes_shirt.shop)\t",
"car_snif = house.group_tree(home_cool)\t", # restart from beginning of 0.txt replacment patterns
"machine(shoes_shirt.shop)\t",
"car_snif = house.group_tree(home_cool)\t",
"machine(shoes_shirt.shop)\t",
一個awk想法:
awk '
NR==FNR { a[NR]=$0; n=NR; next}
/home_cool01/ { gsub("home_cool01", a[int((i )%(n*2)/2) 1])}
/home_cool/ { gsub("home_cool", a[int((j )%(n*2)/2) 1])}
1
' 1.txt 0.txt
在哪里:
n==3- 中的行數1.txt;所以n*2 == 6%6- 將產生輸出0-5(%6/2)- 將輸出變成0.0, 0.5, 1.0, 1.5, 2.0, 2.5int(...)- 將輸出變成0, 0, 1, 1, 2, 21- 給我們我們的陣列索引1, 1, 2, 2, 3, 3- 注意:是的,索引有點復雜;我愿意接受簡化的建議
這會產生:
"#sun\t",
"car_snif = house.group_tree((food, apple, sky, cat,blue,)(bag, tortoise,))\t",
"machine(shoes_shirt.shop)\t",
"car_snif = house.group_tree((food, apple, sky, cat,blue,)(bag, tortoise,))\t",
"machine(shoes_shirt.shop)\t",
"car_snif = house.group_tree((food, apple, sky, cat,blue,)(bag,))\t",
"machine(shoes_shirt.shop)\t",
"car_snif = house.group_tree((food, apple, sky, cat,blue,)(bag,))\t",
"machine(shoes_shirt.shop)\t",
"car_snif = house.group_tree((food, apple, sky, cat,blue,)(bag, moon, tortoise,))\t",
"machine(shoes_shirt.shop)\t",
"car_snif = house.group_tree((food, apple, sky, cat,blue,)(bag, tortoise,))\t",
"machine(shoes_shirt.shop)\t",
"car_snif = house.group_tree((food, apple, sky, cat,blue,)(bag, tortoise,))\t",
"machine(shoes_shirt.shop)\t",
"car_snif = house.group_tree((food, apple, sky, cat,blue,)(bag,))\t",
"machine(shoes_shirt.shop)\t",
"car_snif = house.group_tree((food, apple, sky, cat,blue,)(bag,))\t",
"machine(shoes_shirt.shop)\t",
"car_snif = house.group_tree((food, apple, sky, cat,blue,)(bag, moon, tortoise,))\t",
"machine(shoes_shirt.shop)\t",
"car_snif = house.group_tree((food, apple, sky, cat,blue,)(bag, tortoise,))\t",
"machine(shoes_shirt.shop)\t",
"car_snif = house.group_tree((food, apple, sky, cat,blue,)(bag, tortoise,))\t",
"machine(shoes_shirt.shop)\t",
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/351935.html
