Replace string until 3Rd occurance of forward slash(/)

I have a file abc.txt which has records like

456 /home/fgg/abdc.txt
3567 /home/fdss/vfgb.txt
23 /home/asd/dfght.txt

I WANT TO REMOVE STRING UNTIL 3RD OCCURANCE OF FORWARD SLASH
Output should be like

abdc.txt
vfgb.txt
dfght.txt
echo '456 /home/fgg/abdc.txt' | sed 's#.*/##'
1 Like

Hi himanshupant...
Longhand, OSX 10.13.5, default bash terminal.

Last login: Wed Jul 11 16:02:47 on ttys000
AMIGA:barrywalker~> echo '456 /home/fgg/abdc.txt
> 3567 /home/fdss/vfgb.txt
> 23 /home/asd/dfght.txt' > /tmp/text
AMIGA:barrywalker~> while read line; do printf "%b" "${line##*/}\n"; done < /tmp/text
abdc.txt
vfgb.txt
dfght.txt
AMIGA:barrywalker~> _

Of course if there are any "/" after the three you want removing then this will not work.

1 Like

@vgersh99 Thanks, It is working. Would you please tell how sed is working here ?

How about cut? Not the sharpest hammer in the box, but useful:-

cut -f4- -d"/" input_file > output_file

The -f value has a trailing - so it will grab anything beyond the 3rd / , including other / s if they are there. This might not be what you want, but you can drop it if you only want the 4th field.

Kind regards,
Robin

Try (untested):

awk -F"/" '{print $4}' file