working with paths

working with paths

am 07.12.2007 07:31:41 von RickM

I need a script that will look for specific portion of a path and
print all if it does not find it but if it does, then stop on
directory prior......like that make sense! :^)

Here an example:

look for modify :
------------------------------------------------------------ ----
/path/to/the/file/that/I_need/to/dont_modify

not found so print the entire line:

/path/to/the/file/that/I_need/to/dont_modify

------------------------------------------------------------ ----

/path/to/the/file/that/I_need/to/modify

modify is found so it needs to print:

/path/to/the/file/that/I_need/to


Whats the best why to do this?

THANKS!!!!

Re: working with paths

am 07.12.2007 08:15:04 von Steffen Schuler

On Thu, 06 Dec 2007 22:31:41 -0800, rickm wrote:

> I need a script that will look for specific portion of a path and print
> all if it does not find it but if it does, then stop on directory
> prior......like that make sense! :^)
>
> Here an example:
>
> look for modify :
> ------------------------------------------------------------ ----
> /path/to/the/file/that/I_need/to/dont_modify
>
> not found so print the entire line:
>
> /path/to/the/file/that/I_need/to/dont_modify
>
> ------------------------------------------------------------ ----
>
> /path/to/the/file/that/I_need/to/modify
>
> modify is found so it needs to print:
>
> /path/to/the/file/that/I_need/to
>
>
> Whats the best why to do this?
>
> THANKS!!!!

A Bourne shell script:

#!/bin/sh
for path
do
IFS=/
export IFS
set -- $path
found=0
for comp in "$@"
do
if [ "$comp" = modify ]
then
found=1
break
fi
done
if [ "$found" -ne 0 ]
then
path="${path%/*}"
if [ -z "$path" ]
then
printf "/\n"
else
printf "%s\n" "$path"
fi
else
printf "%s\n" "$path"
fi
done

Regards,

Steffen "goedel" Schuler

Re: working with paths

am 07.12.2007 08:40:34 von Steffen Schuler

On Thu, 06 Dec 2007 22:31:41 -0800, rickm wrote:

> I need a script that will look for specific portion of a path and print
> all if it does not find it but if it does, then stop on directory
> prior......like that make sense! :^)
>
> Here an example:
>
> look for modify :
> ------------------------------------------------------------ ----
> /path/to/the/file/that/I_need/to/dont_modify
>
> not found so print the entire line:
>
> /path/to/the/file/that/I_need/to/dont_modify
>
> ------------------------------------------------------------ ----
>
> /path/to/the/file/that/I_need/to/modify
>
> modify is found so it needs to print:
>
> /path/to/the/file/that/I_need/to
>
>
> Whats the best why to do this?
>
> THANKS!!!!

An AWK script:

BEGIN { OFS = FS = "/" }
{ for (i = 1; i <= NF; ++i)
if ($i == "modify") {
sub(/\/[^\/]+\/?$/, "")
break
} }
$0 == "" { $0 = "/" }
1

The script may be called e.g.

awk -f script < /modify
/dont_modify
/path/to/modify/
/path/to/dont_modify/
EOT

and returns:

/
/dont_modify
/path/to
/path/to/dont_modify/

Regards,

Steffen "goedel" Schuler

Re: working with paths

am 07.12.2007 09:10:07 von Stephane CHAZELAS

On Thu, 6 Dec 2007 22:31:41 -0800 (PST), rickm@galaxy.nsc.com wrote:
> I need a script that will look for specific portion of a path and
> print all if it does not find it but if it does, then stop on
> directory prior......like that make sense! :^)
>
> Here an example:
>
> look for modify :
> ------------------------------------------------------------ ----
> /path/to/the/file/that/I_need/to/dont_modify
>
> not found so print the entire line:
>
> /path/to/the/file/that/I_need/to/dont_modify
>
> ------------------------------------------------------------ ----
>
> /path/to/the/file/that/I_need/to/modify
>
> modify is found so it needs to print:
>
> /path/to/the/file/that/I_need/to
[...]

file=/path/to/the/file/that/I_need/to/modify
search=modify

IFS=/
set -f

set -- $file
result=
for i do
[ "$i" = "$search" ] && break
[ -n "$i" ] || continue
result=$result/$i
done
result=${result:-/}
printf '%s\n' "$result"

could be one way. It assumes "$file" is not a relative path.

--
Stephane

Re: working with paths

am 07.12.2007 09:28:21 von Stephane CHAZELAS

On 7 Dec 2007 07:15:04 GMT, Steffen Schuler wrote:
[...]
> A Bourne shell script:
>
> #!/bin/sh

Note that that shebang line generally doesn't get you a Bourne
shell, nowadays, it gives you a POSIX sh. POSIX sh behaves
differently from the old Bourne shell, so your Bourne script
will be interpreted differently. Not all Unix still have a
Bourne shell nowadays, and even fewer have it as "/bin/sh".

> for path
> do
> IFS=/
> export IFS

You don't need to export IFS, and actually you shouldn't.
You export a variable when you want that commands you execute
(as in the exec() family of system calls) inherit that variable.
If you export IFS that means that it could affect the behavior
of every shell script you execute! Fortunately, except for some
old Bourne shells, most shells will ignore the $IFS from their
environment.

> set -- $path

When you leave a variable unquoted, you need to disable filename
generation unless you want globbing to be performed. So you
need:

set -f

before doing that.

Also word splitting when IFS is not blank is one area where the
Bourne shell behaves differently from nowadays standard shs.

The Bourne shell will split "/foo//bar/" into "foo" and "bar"
while modern shs will split it into "", "foo", "" and "bar"
(some will add another "").

Yet another difference: if $path is empty or contains only
blanks, in the Bourne shell (and in the Bourne shell only),

set --

will leave the positional parameters untouched. So you should
insert a

shift "$#"

before, to make sure the list of positional parameters is empty
before starting.

Also note that $path is a special variable in zsh (an array tied
to $PATH) so it's a good idea to avoid using it.

> found=0
> for comp in "$@"

Another difference between the Bourne shell and modern shs is
that if $# is 0, "$@" will expand to one empty element instead
of no element at all as in modern sh. So, it's better to use the
more portable:

for i do
...

instead.

--
Stephane

Re: working with paths

am 07.12.2007 16:42:10 von RickM

Sorry for the confusion, I was not clear in my need which is a file of
paths not the variable $paths.
Could you please take another look

Thanks and again my appologies

Rick

Re: working with paths

am 07.12.2007 17:49:35 von Stephane CHAZELAS

On Fri, 7 Dec 2007 07:42:10 -0800 (PST), rickm@galaxy.nsc.com wrote:
> Sorry for the confusion, I was not clear in my need which is a file of
> paths not the variable $paths.
> Could you please take another look
[...]

sed 's,/modify\(/.*\)\{0,1\}$,,'

--
Stephane

Re: working with paths

am 07.12.2007 18:08:33 von RickM

The must be a problem with the syntax, heres the error message:

sed: command garbled: s,/modify\(/.*\)\{0,1\}$,,

Thanks



On Dec 7, 8:49 am, Stephane Chazelas
wrote:
> On Fri, 7 Dec 2007 07:42:10 -0800 (PST), ri...@galaxy.nsc.com wrote:
> > Sorry for the confusion, I was not clear in my need which is a file of
> > paths not the variable $paths.
> > Could you please take another look
>
> [...]
>
> sed 's,/modify\(/.*\)\{0,1\}$,,'
>
> --
> Stephane

Re: working with paths

am 07.12.2007 19:17:17 von Icarus Sparry

On Thu, 06 Dec 2007 22:31:41 -0800, rickm wrote:

> I need a script that will look for specific portion of a path and print
> all if it does not find it but if it does, then stop on directory
> prior......like that make sense! :^)
>
> Here an example:
>
> look for modify :
> ------------------------------------------------------------ ----
> /path/to/the/file/that/I_need/to/dont_modify
>
> not found so print the entire line:
>
> /path/to/the/file/that/I_need/to/dont_modify
>
> ------------------------------------------------------------ ----
>
> /path/to/the/file/that/I_need/to/modify
>
> modify is found so it needs to print:
>
> /path/to/the/file/that/I_need/to
>
>
> Whats the best why to do this?
>
> THANKS!!!!

This looks to be just a text manipulation problem, rather than anything
involving files. You want to remove "/modify" from the end of the string.

path="/path/to/the/file/that/I_need/to/modify"
result=${path%/modify}

You might want to remove as well /modify/ to the end of the string. To do
this add

result=${result%%/modify/*}

Then you can do what you want with the result, e.g.

echo "$result"

Re: working with paths

am 07.12.2007 20:20:05 von RickM

What I have is a file that has lots of paths and I need to create a
copy script but I need
it to either pass the entire path if the search result is false.
Heres a different example
the file contents:

/dirA/dirB/dirC/dirE/dirF/dirG/dirH
/dir1/dir2/dir3/dir4/dir5/dir6/dir7

If "dirG" exists, then print:

/dirA/dirB/dirC/dirE/dirF

if dirG does not exit, then print: the entire line:

/dir1/dir2/dir3/dir4/dir5/dir6/dir7








On Dec 7, 10:17 am, Icarus Sparry wrote:
> On Thu, 06 Dec 2007 22:31:41 -0800, rickm wrote:
> > I need a script that will look for specific portion of a path and print
> > all if it does not find it but if it does, then stop on directory
> > prior......like that make sense! :^)
>
> > Here an example:
>
> > look for modify :
> > ------------------------------------------------------------ ----
> > /path/to/the/file/that/I_need/to/dont_modify
>
> > not found so print the entire line:
>
> > /path/to/the/file/that/I_need/to/dont_modify
>
> > ------------------------------------------------------------ ----
>
> > /path/to/the/file/that/I_need/to/modify
>
> > modify is found so it needs to print:
>
> > /path/to/the/file/that/I_need/to
>
> > Whats the best why to do this?
>
> > THANKS!!!!
>
> This looks to be just a text manipulation problem, rather than anything
> involving files. You want to remove "/modify" from the end of the string.
>
> path="/path/to/the/file/that/I_need/to/modify"
> result=${path%/modify}
>
> You might want to remove as well /modify/ to the end of the string. To do
> this add
>
> result=${result%%/modify/*}
>
> Then you can do what you want with the result, e.g.
>
> echo "$result"

Re: working with paths

am 07.12.2007 22:12:29 von krahnj

rickm@galaxy.nsc.com wrote:
>
> What I have is a file that has lots of paths and I need to create a
> copy script but I need
> it to either pass the entire path if the search result is false.
> Heres a different example
> the file contents:
>
> /dirA/dirB/dirC/dirE/dirF/dirG/dirH
> /dir1/dir2/dir3/dir4/dir5/dir6/dir7
>
> If "dirG" exists, then print:
>
> /dirA/dirB/dirC/dirE/dirF
>
> if dirG does not exit, then print: the entire line:
>
> /dir1/dir2/dir3/dir4/dir5/dir6/dir7

Do you mean exists in the file system or exists in the string?


John
--
use Perl;
program
fulfillment

Re: working with paths

am 07.12.2007 22:45:21 von RickM

On Dec 7, 1:12 pm, "John W. Krahn" wrote:
> ri...@galaxy.nsc.com wrote:
>
> > What I have is a file that has lots of paths and I need to create a
> > copy script but I need
> > it to either pass the entire path if the search result is false.
> > Heres a different example
> > the file contents:
>
> > /dirA/dirB/dirC/dirE/dirF/dirG/dirH
> > /dir1/dir2/dir3/dir4/dir5/dir6/dir7
>
> > If "dirG" exists, then print:
>
> > /dirA/dirB/dirC/dirE/dirF
>
> > if dirG does not exit, then print: the entire line:
>
> > /dir1/dir2/dir3/dir4/dir5/dir6/dir7
>
> Do you mean exists in the file system or exists in the string?
>
> John
> --
> use Perl;
> program
> fulfillment



in a string that are lines in a file. The file has lines of file
pointers with paths, that s what Im trying to parse

Re: working with paths

am 07.12.2007 23:34:19 von Stephane CHAZELAS

On Fri, 7 Dec 2007 09:08:33 -0800 (PST), rickm@galaxy.nsc.com wrote:
> The must be a problem with the syntax, heres the error message:
>
> sed: command garbled: s,/modify\(/.*\)\{0,1\}$,,
[...]

Sorry, I should have used another character than "," for the
separator as "," is used as well in the pattern, try with : for
instance:

sed 's:/modify\(/.*\)\{0,1\}$::'

--
Stephane

Re: working with paths

am 07.12.2007 23:34:43 von krahnj

rickm@galaxy.nsc.com wrote:
>
> On Dec 7, 1:12 pm, "John W. Krahn" wrote:
> > ri...@galaxy.nsc.com wrote:
> >
> > > What I have is a file that has lots of paths and I need to create a
> > > copy script but I need
> > > it to either pass the entire path if the search result is false.
> > > Heres a different example
> > > the file contents:
> >
> > > /dirA/dirB/dirC/dirE/dirF/dirG/dirH
> > > /dir1/dir2/dir3/dir4/dir5/dir6/dir7
> >
> > > If "dirG" exists, then print:
> >
> > > /dirA/dirB/dirC/dirE/dirF
> >
> > > if dirG does not exit, then print: the entire line:
> >
> > > /dir1/dir2/dir3/dir4/dir5/dir6/dir7
> >
> > Do you mean exists in the file system or exists in the string?
>
> in a string that are lines in a file. The file has lines of file
> pointers with paths, that s what Im trying to parse

$ echo "/dirA/dirB/dirC/dirE/dirF/dirG/dirH
/dir1/dir2/dir3/dir4/dir5/dir6/dir7" | sed -e's!/dirG/.*!!'
/dirA/dirB/dirC/dirE/dirF
/dir1/dir2/dir3/dir4/dir5/dir6/dir7



John
--
use Perl;
program
fulfillment