Copy file path and remove some parts?

xanis

Lifer
Sep 11, 2005
17,571
8
0
I want to copy a file path and remove the first parts of the result. For example, I want to turn this:

Code:
/Users/xanis/Dropbox (Work)/Client Projects/BigClient/somefile.jpg

into this:

Code:
BigClient/somefile.jpg

I already have an Automator action set up to copy the file path. What I want to do is use regex (or something else) to remove the parts mentioned.

Any suggestions? Is regex the best way or should I just kill myself now? :p
 

Kadarin

Lifer
Nov 23, 2001
44,296
16
81
Not sure of the "best" way, but how about piping it to awk?

awk -F'/' '{print $(NF-1)"/"$NF}'

Yeah, it's ugly. :(
 

TheStu

Moderator<br>Mobile Devices & Gadgets
Moderator
Sep 15, 2004
12,089
45
91
I want to copy a file path and remove the first parts of the result. For example, I want to turn this:

Code:
/Users/xanis/Dropbox (Work)/Client Projects/BigClient/somefile.jpg

into this:

Code:
BigClient/somefile.jpg

I already have an Automator action set up to copy the file path. What I want to do is use regex (or something else) to remove the parts mentioned.

Any suggestions? Is regex the best way or should I just kill myself now? :p

Automator doesn't have functionality that will do the rest?
 

xanis

Lifer
Sep 11, 2005
17,571
8
0
Not sure of the "best" way, but how about piping it to awk?

awk -F'/' '{print $(NF-1)"/"$NF}'

Yeah, it's ugly. :(

That (mostly) did the trick, thanks. I was going to try messing with sed/grep but this was a lot easier. :D

My final question is this: I want to always remove ONLY the first three directories from the path. I'm seeing that in this case, we're manually telling awk to look back a directory (the NF-1 bit).
 

Childs

Lifer
Jul 9, 2000
11,313
7
81
Not sure of the "best" way, but how about piping it to awk?

awk -F'/' '{print $(NF-1)"/"$NF}'

Yeah, it's ugly. :(

Thats actually pretty good. I didn't know NF was the total number of fields. All the time I have been using awk I have been specifying the field by number ($1, $2, etc).
 

manly

Lifer
Jan 25, 2000
13,048
3,805
136
can't you rsync with this as the source?
/Users/xanis/Dropbox (Work)/Client Projects/
 

xanis

Lifer
Sep 11, 2005
17,571
8
0
Okay, came up with something that's so far the closest yet.

The file path is a little wonky with parentheses and spaces, so it's having a hard time. Updating the OP with a more accurate sample path, but the most accurate example is:

Code:
/Users/xanis/Dropbox (Company Name)/Client Projects/filename.jpg

Running the following:

Code:
awk '{print substr($0, index($0,$3))}'

Results in

Code:
Name)/Client Projects/filename.jpg

Close, but not quite. I suspect I need to escape the space?
 
Last edited:

Childs

Lifer
Jul 9, 2000
11,313
7
81
It might be easier to use cut:

Code:
echo "/Users/xanis/Dropbox (Work)/Client Projects/BigClient/somefile.jpg" | cut -d/ -f5-