For a text file:
A B A B A A
A A B A A
B B B A
expected O/P:
A B A B A A
A A B A A
{because [n(A) > n(B)]}
Note: It would be a great help, if the solution can be a command like grep or awk with condition explained above.
CodePudding user response:
You can do:
awk 'gsub(/A/,"&")>gsub(/B/,"&")' file
A B A B A A
A A B A A
This works because gsub returns the number of substations made. The & in the replacement means replace with what was matched.
CodePudding user response:
This might work for you (GNU sed):
sed -E 'h;:a;s/A(.*)B|B(.*)A/\1\2/;ta;/A/!d;x' file
Make a copy of each line in the hold space.
Subtract A and B or B and A from the line until failure.
If an A remains, replace the current line with the copy and print, otherwise delete the current line.
