我想合并两个代码块以接受由于git冲突引起的两个更改,如下所示。
使用像Atom这样的编辑器,它可以通过选项“先是我们的”(或“先他们的”)来解决,而我想通过命令行来解决。
我想到了sed和awk,但我想不出来。请告诉我如何通过sed
或解决awk
其他问题。
来自这个档案
one
<<<<<<< HEAD
two
three
=======
four
five
>>>>>>> Add develop
喜欢这个
one
four
five
two
three
我使用了sed
两次,并尝试了以下操作,但是没有得到我想要的结果。
在第一时间,与移动开始之间的线<<<<<<<
和一端与=======
后>>>>>>>
线。接下来,删除这些<<<<<<<
,=======
,>>>>>>>
在第二时间线。
结果包含额外的空白行。此外,此方法不考虑这种代码块多次出现的情况。
$ sed -E -e '/^<{7}.*$/,/^={7}.*$/{H;d};/^>{7}.*$/{G}' test.txt | sed -E -e '/^[<=>]{7}.*$/{d}'
one
four
five
two
three
您可以尝试以下吗?
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
说明:上面添加了详细的方式说明。
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.
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句