Score:-1

Remove last occurrence in a file with sed

mv flag

I want to remove the very last occurrence of "(* cds_ams_schematic *)" in a file using sed command.

`view schematic

`timescale 1ns / 1ns 
(* cds_ams_schematic *)

module ...

endmodule

// LAST TIME SAVED: Sep  9 12:14:07 2022
// NETLIST TIME: Dec 13 09:13:34 2022

`view schematic

`timescale 1ns / 1ns 
(* cds_ams_schematic *)

module ...
inout  N, P, sub;


endmodule

// LAST TIME SAVED: Oct 10 14:16:33 2022
// NETLIST TIME: Dec 13 09:13:34 2022

`view schematic

`timescale 1ns / 1ns 
(* cds_ams_schematic *)

module ...
inout  PAD;

endmodule
    
`view schematic_incisiv

`timescale 1ns / 1ns 
(* cds_ams_schematic *)

`noworklib
`noview

I tried the command

sed -rn 's/[(][*]\scds_ams_schematic\s[*][)]//' sample_txt_file.txt

However it didnt work. Any ideas?

Score:0
cn flag

GNU sed has the -z option to use the NULL character instead of LF as separator, so for a text file (without NULL) the whole file is processed as one line and you can make use of the greedy .* to find the last occurence in the file:

sed -z 's/\(.*\)(\* cds_ams_schematic \*)/\1/' yourfile.txt

Not a good idea for huge files, but no problem for files like your example.

Score:0
pa flag

You can try this command (Reference)

tac input.txt | awk '!found && /(\* cds_ams_schematic \*)/{found=1;next}1' | tac > output.txt
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.