Skip to content
Advertisement

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

JavaScript

expected output

JavaScript

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 ,

JavaScript

sed (GNU sed) 4.2.2

Advertisement

Answer

In case you have control M characters in your Input_file then remove them by doing:

JavaScript


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.

JavaScript

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.

Advertisement