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|[^/]*/||' Commented Jul 1, 2015 at 18:19
  • awk -F/ '{print $2}' file > newfile Commented Jul 1, 2015 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. Commented Jul 1, 2015 at 20:45
  • @Theophrastus your regEx looks good but its doing the opposite its stripping everthing and just leaving com/ Commented Jul 1, 2015 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 Commented Jul 1, 2015 at 23:24

4 Answers 4

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. Commented Jul 1, 2015 at 20:23
  • @mikeserv Thank you very much as always..i was taking the OP's examples literally..anyway i am clarifying it now.. Commented Jul 1, 2015 at 20:29
  • Let's hope that / doesn't appear more than once per line. Try cut -d/ -f2- and ignore the other suggestions. Commented Jul 1, 2015 at 20:45
3

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 Commented Jul 1, 2015 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. Commented Jul 1, 2015 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) Commented Jul 1, 2015 at 21:04
  • 1
    good argument; I'm convinced! Commented Jul 1, 2015 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 Commented Jul 1, 2015 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. Commented Jul 2, 2015 at 5:18
  • I dont know why it didnt work for me but i ended up using this one [^/]*$ Commented Jul 2, 2015 at 16:28

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.