Sponsored Content
Top Forums Shell Programming and Scripting Parse file name out of UNC path Post 302647733 by bakunin on Monday 28th of May 2012 05:09:53 PM
Old 05-28-2012
Quote:
Originally Posted by bytesnoop
Code:
# parse out, de-dupe and clean up failed source UNC paths. 
grep "ERROR 5" $1 |colrm 1 54 |uniq |tr -d '\r' >path

# create array of failed source UNC paths.
mapfile -t PATH <path

# parse out and save the source folder path and filenames from PATH array.
x=0
while [ $x -lt ${#PATH[@]} ]; do
	echo "${PATH["$x"]##*\\}" >>file
	echo "${PATH["$x"]%\\*}" >>folder
	x=$(( $x + 1 ))
done

# wrap each string from temp files in double quotes.
sed 's/^/"/; s/$/"/' file >>file.sed
sed 's/^/"/; s/$/"/' folder >>folder.sed

I have no idea what does go wrong with your script because i can't see a (syntactical) error and a first test worked for me. I can tell you, though, that you are going in circles.

You "cook up" a file <path> in your first line, which should be a single sed (awk, text-filter-of-your-choice) statement, while it is a whole pipeline of different commands. That could and should be optimized, but let us put that aside for the moment.

After creating file <path> you parse it into an array PATH (which is at least dangerous, because PATH is a special variable to the shell) and then circle through this array writing two new files. What is the array for when you already have the same information in your file?

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

typeset pathname=""
while read pathname ; do
	echo "${pathname##*\\}" >>file
	echo "${pathname%\\*}" >>folder
done < path

# wrap each string from temp files in double quotes.
sed 's/^/"/; s/$/"/' file >>file.sed
sed 's/^/"/; s/$/"/' folder >>folder.sed

Then you use two sed-invocations to add double quotes. Why don't you add them already in the echo-statements? It is possible to escape characters and use them literally instead of their special meaning to the shell:

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

typeset pathname=""
while read pathname ; do
	echo "\"${pathname##*\\}\"" >>file
	echo "\"${pathname%\\*}\"" >>folder
done < path

I won't even mention using full-qualified pathnames ("/path/to/file" instead of "file", etc.) inside of scripts because otherwise the result files are going to go wherever the script is called from - ever thought about putting this script in cron? You're guaranteed to have fun.

I hope this helps.

bakunin
This User Gave Thanks to bakunin For This Post:
 

9 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

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

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

7. 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

8. 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

9. 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
All times are GMT -4. The time now is 07:14 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy