sed script to delete the last word after a last pattern match

Hi Guys ,

I am having a file as stated below

File 1

sa0   --   i_core/i_core_apb/i_afe0_controller/U261/A
 sa0   --   i_core/i_core_apb/i_afe0_controller/U265/Z
 sa1   --   i_core/i_core_apb/i_afe0_controller/U265/A
 sa1   --   i_core/i_core_apb/i_afe0_controller/U268/Z
 sa1   --   i_core/i_core_apb/i_afe0_controller/U268/A
 sa1   --   i_core/i_core_apb/i_afe0_controller/U273/Z
sa0   --   i_core/i_core_ahb/i_hebi/i_hsdramc1/i_hsdramc1_ocms_scrambler/ext_key_regx63x/D
 sa1   AN   i_core/i_core_ahb/i_hebi/i_hsdramc1/i_hsdramc1_ocms_scrambler/ext_key_regx63x/SE

I want to search the last "/" in each and every line and delete the last word after that including last "/"

Output

sa0   --   i_core/i_core_apb/i_afe0_controller/U261
 sa0   --   i_core/i_core_apb/i_afe0_controller/U265
 sa1   --   i_core/i_core_apb/i_afe0_controller/U265
 sa1   --   i_core/i_core_apb/i_afe0_controller/U268
 sa1   --   i_core/i_core_apb/i_afe0_controller/U268
 sa1   --   i_core/i_core_apb/i_afe0_controller/U273
sa0   --   i_core/i_core_ahb/i_hebi/i_hsdramc1/i_hsdramc1_ocms_scrambler/ext_key_regx63x
 sa1   AN   i_core/i_core_ahb/i_hebi/i_hsdramc1/i_hsdramc1_ocms_scrambler/ext_key_regx63x

Could you help me out?

Try:

sed 's|/[^/]*$||' "File 1"

But, please always tell us what operating system and shell you're using when you start a new thread so we'll be less likely to post suggestions that won't work in your environment.

Hi Don

Thanks a lot for your reply! I am using Linux RH-6 .
If we need to match with the second last "/" and delete the lines from the second last "/
then how do we need to tweak the script. ?

Thanks
Kshitij

Hi Kshitij,
From your description above I have no idea which lines it is that you want to delete. In the first problem you presented in this thread you only wanted to remove characters from the end of each line. Now that you want to delete some lines, we need a clear specification of which lines you want to delete.

If you just want to remove another "/" character and the non-"/" characters from the ends of every line, I have already shown you how to do that. Why don't you show me how you would modify the code I suggested to delete the last two "/" characters and the non-"/" characters following them from the end of every line. Assuming that every line has at least two "/" characters (as in your example), there are at least three easy ways to do that.

If some lines might only contain one "/", you need to decide whether you want those lines to be changed at all (removing only the last "/" and the characters following it) or if you don't want to make any changes to a line if there aren't at least two "/" characters on the line. The way to do what you want to do depends on what it is that you want to do and on the data that you will be processing.

Hi Don,

can you explain how this reg ex will parse and give the output.I have understand a little the $ represent end of a line and * is all lines, but i didn't get the other things.

sed 's|/[^/]*$||' "File 1"

May I jump in as Don seems offline for a while now?'

sed operates on files line by line, it reads a line, performs the entire script on it, and reads the next. * is not "all lines" but a sort of mutiplier for the preceding "atom" ( man regex ).

s		substitute command
|		regex delimiter; any char allowed, see "man sed"
/[^/]*$		regex: slash, any number (*) non-slashes ([^/]), line end
|		separates regex from replacement
|		replacement terminator, empty replacement = delete regex
2 Likes

Hi nag_sathi,
It looks like RudiC answered the question for you while I was sleeping last night. Do you still have questions about how that sed search command works?

Note that the ability to use any character as a field delimiter is not portable to all operating systems, but does work on Linux systems (and other systems using the GNU sed utility). If you want to write a script that will be portable to other systems, avoid using the <newline> character as a delimiter and avoid using the <null> byte as a delimiter. Even on Linux systems, some shells might not correctly pass your substitute command to sed correctly if you use a literal <null> byte as your delimiter.

Note that I do not know of any shells usually deployed with Linux systems that have this restriction, but the standards say that the input given to the shell is a text file with unlimited line lengths and <null> bytes are not allowed in text files.

1 Like

To delete from the 2nd last / , you can apply the s command twice,
or repeat the expression before the $ (end of line anchor).
The following has the expression wrapped in \( \) followed by \{2\} that repeats it 2 times

sed 's|\(/[^/]*\)\{2\}$||' filename