Need pattern matching in vi editor

Hi,

I want to remove all the digits/characters after the last underscore in the following set of lines

abc_cdef_12_456_98765
defs_qw_4567_23_0877_89976769870
deffon_asdcdc_23_980980_098
sdfre_rtt_grt

the result im expecting is

abc_cdef_12_456
defs_qw_4567_23_0877
deffon_asdcdc_23
sdfre_rtt

I want to know inside VI editor what pattern replacement can be used to perform the above operation.

Pl. note no.of underscores are different. After underscore any thing can come, say , character or digit!:rolleyes:

Thanks in advance!!!

Appu

:,%s/_[^_]*$//
1 Like

you could use a macro if your version of vi supports them - i've been using vim so long I can no longer remember what the original vi can do.

the macro could be:

$F_d$j

$ - go to end of line
F_ go back to the first occurance of a _
d$ - delete to the end of line
j move down one line

which can be run over the whole file by going to the top of the file and 999@a (or whatever register you save the macro to, and also change the number of times to run to match the number of lines (or greater) in the file.

1 Like

Thanks a lot guys!!!