I am trying to use the following command_line01
to replace the first and second occurrence of home_cool
with the first line of 1.txt
individually for each occurrence, and proceeding, replace the first and second occurrence of home_cool01
also by the first line of 1.txt
individually, and then.., replace the third and fourth occurrences of home_cool with the second line of 1.txt
individually., and etc, that is, every 2 nth occurrences of home_cool
or home_cool01
, respectively replace both strings with the nth line of 1.txt
.
I tried command_line01
bellow:
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
but this only works for the first two occurrences for the next two occurrences home_cool
or home_cool01
is replaced by nothing as show bellow:
"#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",
Here my two sources files:
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",
and
1.txt:
(food, apple, sky, cat,blue,)(bag, tortoise,)
(food, apple, sky, cat,blue,)(bag,)
(food, apple, sky, cat,blue,)(bag, moon, tortoise,)
and my desire output 2.txt is:
"#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",
EDIT: from @markp-fuso comments:
1 - If there are multiple entries I would reboot from second if there is already finished processing the first entry.
2 - If I have more than 6 home_coool
entries ... I continue from the beginning of 1.txt
.
3 - I would like not to be limited to only two patterns of research, so it should be a proper solution when there is home_coool02
, home_coool03
, ..., home_coool_some_sufix
, but I need to keep an MWE as I posted initially
CodePudding user response:
Assumptions:
- only need to worry about search patterns
home_cool
andhome_cool01
(more can be added but will need a bit of rework; likely use an associative array to keep track of counts of each unique pattern) - replacement pattens are to be applied twice before moving on to the next replacement pattern
- if we reach the end of the replacement patterns we start over from the beginning
Sample input:
$ 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",
One awk
idea:
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
Where:
n==3
- number of rows in1.txt
%6
- will generate outputs of0-5
(%6/2)
- turns output into0.0, 0.5, 1.0, 1.5, 2.0, 2.5
int(...)
- turns output into0, 0, 1, 1, 2, 2
1
- gives us our array indices of1, 1, 2, 2, 3, 3
- NOTE: yeah, a bit convoluted on the indexing; I'm open to suggestions to simplify
This generates:
"#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",