remove character on the last line that specific word appears

Judy

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

RavinderSingh13

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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

remove character on the last line that specific word appears

Remove last character on each line

sed + remove the "#" character in case line appears

Remove last character from the first word in a string

Bash - Remove the last character of the line this before?

Shell: Remove last character of line if it is double quote

How to remove last character of Nth line linux

Remove last character of every nth line in place

shell script to remove last character of a line

Remove Last Character of Each Line that Starts With @

How do you check if the last line character appears inside the string

remove the characters after the last of a specific character

Remove last specific character in a string c#

Remove characters after the last occurrence of a specific character

remove last specific character if next position is a whitespace

How to remove a particular character after a specific word

Python, Remove word start with specific character

replace last word in specific line sed

Remove first and last character if has specific character in SQL Server?

sed + remove word from specific line

Regex first word after match and last word in specific line

How can i filter/remove a line that a specific word is not exist in the line?

Remove everything after the last occurrence of selected character in the first line

sed-remove all but last occurrence of a character from a line

Remove line if the second or second to last character is a space in the first column of a CSV

regex last character of a WORD

How to remove all except the first 3 and last of a specific character with sed

Delete a whole line on keeping the last specific character in unix

How to remove word from a string if it contains a specific character?