• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

Copy file path and remove some parts?

xanis

Lifer
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? 😛
 
Not sure of the "best" way, but how about piping it to awk?

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

Yeah, it's ugly. 🙁
 
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? 😛

Automator doesn't have functionality that will do the rest?
 
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. 😀

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).
 
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).
 
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:
It might be easier to use cut:

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