3

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.

8
  • 2
    pipe it through something like: sed -e 's|[^/]*/||' Jul 1 '15 at 18:19
  • awk -F/ '{print $2}' file > newfile
    – jasonwryan
    Jul 1 '15 at 18:32
  • @Theophrastus that's the most solid solution proposed so far. Turn it into an answer and I for one will upvote it.
    – roaima
    Jul 1 '15 at 20:45
  • @Theophrastus your regEx looks good but its doing the opposite its stripping everthing and just leaving com/ Jul 1 '15 at 22:53
  • @user3738022 not when i do it: echo "testing.db.com/8a8589bf-49e3-4cd7-af15-6753067355c6" | sed -e 's|[^/]*/||' ends up returning this: 8a8589bf-49e3-4cd7-af15-6753067355c6 Jul 1 '15 at 23:24
5

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.

3
  • I'm upvoting this for cut - because good work and that's what I came here to say. But you've got it in mixed company - for this task these commands are nowhere near equivalent. cut should be the only thing here - to make it act like grep -o use cut -sd. Those other two commands are merely a distraction. Run some time tests on large inputs and you'll see what I mean.
    – mikeserv
    Jul 1 '15 at 20:23
  • @mikeserv Thank you very much as always..i was taking the OP's examples literally..anyway i am clarifying it now..
    – heemayl
    Jul 1 '15 at 20:29
  • Let's hope that / doesn't appear more than once per line. Try cut -d/ -f2- and ignore the other suggestions.
    – roaima
    Jul 1 '15 at 20:45
2

easy enough with sed:

echo "testing.db.com/7fad416d-f2b3-4259-b98d-2449957a3123" | sed -e "s/.*\///"

basic syntax is "s/search/replace/" and here we search for .*\/ which means everything ending with slash (escaped) and we replace it with nothing.

1
  • 1
    Let's hope that / doesn't appear more than once per line
    – roaima
    Jul 1 '15 at 20:42
0

Using Perl:

< file.txt perl -pe 's/.*\///'
3
  • 1
    Let's hope that / doesn't appear more than once per line. You might want to use 's|.*?/||' to bullet-proof the suggestion.
    – roaima
    Jul 1 '15 at 20:47
  • @roaima We don't have enough elements to judge; according to the example either matching greedily or lazily it's the same, because there's always one single /; if there are more than one in the part to exclude than one should match greedily like I did, otherwise one should match lazily like you suggested. Not knowing this, since the string looks pretty regular (only composed by numbers and -), I'd prefer to bullet-proof the solution against possible subdirs in the link (e.g. testing.db.com/subdir)
    – kos
    Jul 1 '15 at 21:04
  • 1
    good argument; I'm convinced!
    – roaima
    Jul 1 '15 at 21:23
-1

Two methods using sed:

sed -i "s|testing.db.com/||g" file_path_here

or

echo "testing.db.com/7fad416d-f2b3-4259-b98d-2449957a3123" | sed -e "s|testing.db.com/||g"
3
  • I have tried this too but it is doing the opposite :( It's leaving com/ instead of the other way round Jul 1 '15 at 22:57
  • I don't understand. I tested it on my server and it works fine, it leaves "7fad416d-f2b3-4259-b98d-2449957a3123" only.
    – Artyum
    Jul 2 '15 at 5:18
  • I dont know why it didnt work for me but i ended up using this one [^/]*$ Jul 2 '15 at 16:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.