we have the following file example
we want to remove the ,
character on the last line that topic
word exists
more file {"topic":"life_is_hard","partition":84,"replicas":[1006,1003]}, {"topic":"life_is_hard","partition":85,"replicas":[1001,1004]}, {"topic":"life_is_hard","partition":86,"replicas":[1002,1005]}, {"topic":"life_is_hard","partition":87,"replicas":[1003,1006]}, {"topic":"life_is_hard","partition":88,"replicas":[1004,1001]}, {"topic":"life_is_hard","partition":89,"replicas":[1005,1002]}, {"topic":"life_is_hard","partition":90,"replicas":[1006,1004]}, {"topic":"life_is_hard","partition":91,"replicas":[1001,1005]}, {"topic":"life_is_hard","partition":92,"replicas":[1002,1006]}, {"topic":"life_is_hard","partition":93,"replicas":[1003,1001]}, {"topic":"life_is_hard","partition":94,"replicas":[1004,1002]}, {"topic":"life_is_hard","partition":95,"replicas":[1005,1003]}, {"topic":"life_is_hard","partition":96,"replicas":[1006,1005]}, {"topic":"life_is_hard","partition":97,"replicas":[1001,1006]}, {"topic":"life_is_hard","partition":98,"replicas":[1002,1001]}, {"topic":"life_is_hard","partition":99,"replicas":[1003,1002]},
expected output
{"topic":"life_is_hard","partition":84,"replicas":[1006,1003]}, {"topic":"life_is_hard","partition":85,"replicas":[1001,1004]}, {"topic":"life_is_hard","partition":86,"replicas":[1002,1005]}, {"topic":"life_is_hard","partition":87,"replicas":[1003,1006]}, {"topic":"life_is_hard","partition":88,"replicas":[1004,1001]}, {"topic":"life_is_hard","partition":89,"replicas":[1005,1002]}, {"topic":"life_is_hard","partition":90,"replicas":[1006,1004]}, {"topic":"life_is_hard","partition":91,"replicas":[1001,1005]}, {"topic":"life_is_hard","partition":92,"replicas":[1002,1006]}, {"topic":"life_is_hard","partition":93,"replicas":[1003,1001]}, {"topic":"life_is_hard","partition":94,"replicas":[1004,1002]}, {"topic":"life_is_hard","partition":95,"replicas":[1005,1003]}, {"topic":"life_is_hard","partition":96,"replicas":[1006,1005]}, {"topic":"life_is_hard","partition":97,"replicas":[1001,1006]}, {"topic":"life_is_hard","partition":98,"replicas":[1002,1001]}, {"topic":"life_is_hard","partition":99,"replicas":[1003,1002]}
we try to removed the character ,
from the the last line that contain topic
word as the following sed cli but this syntax not renewed the ,
sed -i '${s/,[[:blank:]]*$//}' file
sed (GNU sed) 4.2.2
Advertisement
Answer
In case you have control M characters in your Input_file then remove them by doing:
tr -d 'r' < Input_file > temp && mv temp Input_file
Could you please try following once. From your question what I understood is you want to remove comma from very last line which has string topic
in it, if this is the case then I am coming up with tac
+ awk
solution here.
tac Input_file | awk '/topic/ && ++count==1{sub(/,$/,"")} 1' | tac
Once you are happy with above results then append > temp && mv temp Input_file
to above command too, to save output into Input_file itself.
Explanation:
Atac
will read Input_file from bottom line to first line then passing it’s output to awk where I am checking if first occurrence of topic is coming remove comma from last and rest of lines simply print then passing this output to tac again to make Input_file in original form again.