Delete everything before “/” on every line

regular expressiontext processing

I am trying to delete certain text that appears before / on every line.

I have something like:

testing.db.com/7fad416d-f2b3-4259-b98d-2449957a3123
testing.db.com/8a8589bf-49e3-4cd7-af15-6753067355c6

and I just want to end up with:

7fad416d-f2b3-4259-b98d-2449957a3123
8a8589bf-49e3-4cd7-af15-6753067355c6 

Can anyone help me with a regular expression? Everything I have found is deleting after /, not before.

Best Answer

Using cut :

$ cut -sd'/' -f2 file.txt   ##This will print only the lines containing /
7fad416d-f2b3-4259-b98d-2449957a3123
8a8589bf-49e3-4cd7-af15-6753067355c6

The following suggestions assumes that / appears only once in a line :

Using grep :

$ grep -o '[^/]*$' file.txt  ##This will print the lines not having / too
7fad416d-f2b3-4259-b98d-2449957a3123
8a8589bf-49e3-4cd7-af15-6753067355c6

If you have / in all of the lines, you can use these too:

Using bash parameter expansion:

$ while read line; do echo "${line#*/}"; done <file.txt 
7fad416d-f2b3-4259-b98d-2449957a3123
8a8589bf-49e3-4cd7-af15-6753067355c6

Or python :

#!/usr/bin/env python2
with open('file.txt') as f:
    for line in f:
        print line.split('/')[1].rstrip()

Note that as far as your example is concerned all of the above suggestions are valid.

Related Question