[英]remove character on the last line that specific word appears
we have the following file example我们有以下文件示例
we want to remove the ,
character on the last line that topic
word exists我们要删除的,
在最后一行字符topic
词存在
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 ,
我们试图删除的角色,
从包含最后一行topic
字作为下面的sed CLI但是这句法不续约,
sed -i '${s/,[[:blank:]]*$//}' file
sed (GNU sed) 4.2.2 sed (GNU sed) 4.2.2
您应该使用地址$
(最后一行):
sed '$s/,$//' file
In case you have control M characters in your Input_file then remove them by doing:如果您在 Input_file 中有控制 M 个字符,则通过执行以下操作将其删除:
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.从您的问题中,我了解到您想从包含字符串topic
最后一行中删除逗号,如果是这种情况,那么我将在这里提出tac
+ awk
解决方案。
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.一旦您对上述结果感到满意,那么也将> temp && mv temp Input_file
附加到上述命令中,以将输出保存到 Input_file 本身中。
Explanation:解释:
A tac
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. tac
将从底行读取 Input_file 到第一行,然后将其输出传递给 awk,在那里我检查是否第一次出现主题从最后一行中删除逗号,其余行简单地打印然后将此输出再次传递给 tac 以使 Input_file 为原始再次形成。
Using awk:使用 awk:
$ awk '{if(NR>1)print p;p=$0}END{sub(/,$/,"",p);print p}' file
Output:输出:
...
{"topic":"life_is_hard","partition":98,"replicas":[1002,1001]},
{"topic":"life_is_hard","partition":99,"replicas":[1003,1002]}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.