Parse file name out of UNC path


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Parse file name out of UNC path
# 1  
Old 05-17-2012
Parse file name out of UNC path

Hello,

I searched the forums and didn't see a situation like this:

I cannot figure out how to parse out just the file name from the full path. The path looks like this:

\\foo\bar\filename.ext

I don't think something like 'cut' will work so I tried to whip up a regex but couldn't get it quite right. Any ideas? Thanks in advance.
# 2  
Old 05-17-2012
try basename
# 3  
Old 05-17-2012
Try:
Code:
sed 's/.*\\//'

or
Code:
${var##*\\}

Code:
$ echo '\\foo\bar\filename.ext' | sed 's/.*\\//'
filename.ext

Code:
$ var='\\foo\bar\filename.ext'
$ echo "${var##*\\}"
filename.ext

The first is more suited for filtering stdin or files, the second is more suited for single strings..

Last edited by Scrutinizer; 05-17-2012 at 02:19 PM..
This User Gave Thanks to Scrutinizer For This Post:
# 4  
Old 05-23-2012
Thanks for the quick feedback. How would I parse out everything BUT the file name? In other words, how can I parse our just the folder path?
# 5  
Old 05-23-2012
This User Gave Thanks to Corona688 For This Post:
# 6  
Old 05-23-2012
Thanks Corona, that index will prove useful someday. Let me run this scenario by you guys and maybe I can get a more elegant solution than what I've been rolling with thus far.

I want to create a script that will parse out a specific string (UNC or regular folder paths) from a Robocopy log. The string searching, parsing and de-duping has all been taken care of with this one liner:

Code:
grep "ERROR 5" $1 |colrm 1 54 |uniq |tr -d '\r' >$1.tmp

colrm removes all the junk I don't need up to the beggining of the path. Uniq de-dupes it because I have Robocopy set to retry so I generally have 2 of the same error and tr cleans up the carriage returns (if there are any) so sed can work on the strings later.

What I am left with is a file containing 1 string per line: \\foo\bar\filename.ext

The end goal is to have the folder path and file name seperated into either temprary files or into seperate array's so that I can drop them into a new robocopy batch file with the following format:

Code:
RCXP026.exe  <path>  <destination>  <filename.ext> /R:1 /W:10 /TEE /Log+:”<Report Path with File Name>

The only other piece to the puzzle is to make sure each path and file name is wrapped in quotes. To this point I have everything figured out except for separating the folder and file name.

Thanks again for your time and consideration.

Last edited by Scrutinizer; 05-23-2012 at 02:19 PM.. Reason: code tags
# 7  
Old 05-25-2012
Code:
LINE="\\foo\bar\filename.ext"
FILE="${LINE##*\\}"
FOLDER= "${LINE%\\*}"

This User Gave Thanks to Corona688 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Convert Relative path to Absolute path, without changing directory to the file location.

Hello, I am creating a file with all the source folders included in my git branch, when i grep for the used source, i found source included as relative path instead of absolute path, how can convert relative path to absolute path without changing directory to that folder and using readlink -f ? ... (4 Replies)
Discussion started by: Sekhar419
4 Replies

2. Shell Programming and Scripting

Parse Directory path - awk

Hi All, Need some help in parsing a directory listing .. output into 2 files Input file level1,/level2/level3/level4/ora001,10,IBB23 level1,/level2/level3/level4/ora001/blu1,,IBB23 level1,/level2/level3/level4/ora001/clu1,,IBB23 level1,/level2/level3/level4/ora002,,IBB24... (10 Replies)
Discussion started by: greycells
10 Replies

3. Shell Programming and Scripting

Determine if variable is the Server component of UNC

Hi all, Using sh/csh, unfortunately shell scripts are not my strong suit. Trying to write a script that gets called from a program for pre-processing. The program passes individual components of a UNC (//server/path1/path2/filename). Thus the unc path of: //server/path1/path2/filename, is... (7 Replies)
Discussion started by: Festus Hagen
7 Replies

4. Shell Programming and Scripting

Parse output path to set variable

I am looking to parse a text file output and set variables based on what is cropped from the parsing. Below is my script I am looking to add this feature too. All it does is scan a certain area of users directories for anyone using up more than X amount of disk space. It then writes to the... (4 Replies)
Discussion started by: es760
4 Replies

5. Shell Programming and Scripting

Using Perl to connect UNC Filepaths on Domain

I'm trying to use Perl on Windows (Doh!) to connect to a folder on a Domain Controller via UNC. Right now, I have perl -e "`runas /user:DOMAIN\\Username dir \\\\SERVER\\d\$\\Path`" This does not seem to connect nor does it prompt for password. Should I try throwing it into a script and... (0 Replies)
Discussion started by: adelsin
0 Replies

6. Shell Programming and Scripting

Retrieve directory path from full file path through sh

Hi, I have a file abcd.txt which has contents in the form of full path file names i.e. $home> vi abcd.txt /a/b/c/r1.txt /q/w/e/r2.txt /z/x/c/r3.txt Now I want to retrieve only the directory path name for each row i.e /a/b/c/ /q/w/e/ How to get the same through shell script?... (7 Replies)
Discussion started by: royzlife
7 Replies

7. Windows & DOS: Issues & Discussions

Long UNC path not working in CMD.EXE on remote machine

Hi, I am trying to connect to a remote server using Plink tool. Both my local and remote machines are Windows. On remote server, I have OpenSSH server installed. I am able to run commands on remote machine but there is some problem with long UNC path, which I noticed today. For... (3 Replies)
Discussion started by: Technext
3 Replies

8. Shell Programming and Scripting

Parse value from multiple row to create the path

Hi all, Hope all the expert can help me in this situation. Let say I have one file with multiple record like below: NAME=FRAGMENT LANGUAGE=1 DIALECT=0 GENDER=NONE FILE=TEST1 DIRECTORY=D:/DETAILS/1/0/test1.txt END NAME=FRAGMENT LANGUAGE=1 DIALECT=0 GENDER=NONE (13 Replies)
Discussion started by: shirleyeow
13 Replies

9. UNIX for Dummies Questions & Answers

vi - replacing a relative path with absolute path in a file

Hi, I have a file with about 60 lines of path: app-defaults/boxXYZ....... I want to change this to /my/path/goes/here/app-defaults/boxXYZ, but of course vi doesn't like the regualr :s/old/new/ command. Is there any other quick way to do this? Thanks ;) (2 Replies)
Discussion started by: Yinzer955i
2 Replies
Login or Register to Ask a Question