I want to merge two code blocks to accept both changes due to git conflict like below.
With editor like Atom, it can resolve by option "Theirs then ours" (or "Ours then theirs"), and I want to resolve by command line.
I thought about with sed and awk but I couldn't come up with.
Please tell me how to solve this problem by sed or awk, or other solution.
From this file
one
<<<<<<< HEAD
two
three
=======
four
five
>>>>>>> Add develop
to like this.
one
four
five
two
three
I used sed twice and tried as following, but did not get the result I want.
In the first time, move the lines between begin with <<<<<<< and end with ======= after >>>>>>> line.
Next, delete these <<<<<<<, =======, >>>>>>> lines in the second time.
The result contains extra blank line. Furthermore, this method doesn't consider the case where such a code block appears multiple times.
$ sed -E -e '/^<{7}.*$/,/^={7}.*$/{H;d};/^>{7}.*$/{G}' test.txt | sed -E -e '/^[<=>]{7}.*$/{d}'
one
four
five
two
three
Could you please try following.
awk '
/>>/ && found1 && found2{
print val2 ORS val1
}
/</{
val1=val2=found2=""
found1=1
next
}
found1 && /=/{
found2=1
next
}
found1 && !found2{
val1=(val1?val1 ORS:"")$0
next
}
found2{
val2=(val2?val2 ORS:"")$0
next
}
1
' Input_file
Explanation: Adding detailed manner explanation for above.
awk ' ##Starting awk program from here.
/>>/ && found1 && found2{ ##Checking condition if line has >> string in it and variable found1 and found2 is NOT NULL then do following.
print val2 ORS val1 ##Printing val2, ORS and val1 variables here.
} ##Closing BLOCK for above condition here.
/</{ ##Checking condition if line has < in it then do following.
val1=val2=found2="" ##Nullifying variables val1,val2 and found2.
found1=1 ##Setting found1 as 1 to be used in further statements from here.
next ##next keyword will skip all further statements from here onward.
} ##Closing BLOCK for above condition here.
found1 && /=/{ ##Checking condition if found1 variable is SET AND line has = in it then do following.
found2=1 ##Setting variable found2 to 1 here.
next ##next will skip all further statements from here,
} ##Closing BLOCK for above condition here.
found1 && !found2{ ##Checking condition ig variable foudn1 is SET and found2 is NOT SET then do following.
val1=(val1?val1 ORS:"")$0 ##Creating variable val1 whose value is keep concatenating its own value with a new line here.
next ##next will skip all further statements from here.
} ##Closing BLOCK for above condition here.
found2{ ##Checking condition ig variable found2 is SET then do following.
val2=(val2?val2 ORS:"")$0 ##Creating variable val2 whose value is keep concatenating to its own value with a new line.
next ##next will skip all further statements from here.
} ##Closing BLOCK for above condition here.
1 ##Mentioning 1 will print edited/non-edited line here.
' Input_file ##Mentioning Input_file name here.
This might work for you (GNU sed):
sed -nE '/^<{7}/{:a;n;/^={7}/bb;H;ba;:b;n;/^>{7}/bc;p;bb;:c;z;x;s/.//};p' file
Turn off implicit printing by setting the -n option and enable easier regexp by setting the -E option.
Gather up lines following a line beginning <<<<<<< in the hold space, then print the lines following a line beginning ======= until a line beginning >>>>>>> and then append the collected lines in the hold space (less the first character which will be a newline).
N.B. The z command, which empties the new hold space to be, ready for further amendments.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With