删除出现特定单词的最后一行的字符

时间:2019-12-23 21:31:40

标签: linux shell awk sed

我们有以下文件示例

我们要删除,单词存在的最后一行上的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]},

预期产量

{"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]}

我们尝试从包含,字词的最后一行中删除字符topic作为以下sed cli,但是此语法不会更新,

sed -i '${s/,[[:blank:]]*$//}' file

sed(GNU sed)4.2.2

3 个答案:

答案 0 :(得分:2)

您应该使用地址$(最后一行):

sed '$s/,$//' file

答案 1 :(得分:2)

如果Input_file中有控制M个字符,请执行以下操作将其删除:

tr -d '\r' < Input_file > temp && mv temp Input_file


能否请您尝试一次。从您的问题中得知,您想从最后一行包含字符串topic的逗号中删除逗号,如果是这种情况,那么我想出了tac + awk解决方案在这里。

tac Input_file | 
awk '/topic/ && ++count==1{sub(/,$/,"")} 1' | 
tac

一旦您对以上结果感到满意,然后也将> temp && mv temp Input_file附加到以上命令中,以将输出保存到Input_file本身。

说明:

A tac将从下一行读取Input_file到第一行,然后将其输出传递给awk,在这里我正在检查是否首先出现主题,从最后一行删除逗号,然后简单地打印其余行,然后将此输出传递给再次tac再次使Input_file成为原始形式。

答案 2 :(得分:0)

使用awk:

$ awk '{if(NR>1)print p;p=$0}END{sub(/,$/,"",p);print p}' file

输出:

...
{"topic":"life_is_hard","partition":98,"replicas":[1002,1001]},
{"topic":"life_is_hard","partition":99,"replicas":[1003,1002]}