working with paths

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!!!!
RickM [ Fr, 07 Dezember 2007 07:31 ] [ ID #1887680 ]

Re: working with paths

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 "$ [at] "
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
Steffen Schuler [ Fr, 07 Dezember 2007 08:15 ] [ ID #1887681 ]

Re: working with paths

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 <<EOT
/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
Steffen Schuler [ Fr, 07 Dezember 2007 08:40 ] [ ID #1887684 ]

Re: working with paths

On Thu, 6 Dec 2007 22:31:41 -0800 (PST), rickm [at] 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
Stephane CHAZELAS [ Fr, 07 Dezember 2007 09:10 ] [ ID #1887686 ]

Re: working with paths

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 "$ [at] "

Another difference between the Bourne shell and modern shs is
that if $# is 0, "$ [at] " 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
Stephane CHAZELAS [ Fr, 07 Dezember 2007 09:28 ] [ ID #1887687 ]

Re: working with paths

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
RickM [ Fr, 07 Dezember 2007 16:42 ] [ ID #1887698 ]

Re: working with paths

On Fri, 7 Dec 2007 07:42:10 -0800 (PST), rickm [at] 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
Stephane CHAZELAS [ Fr, 07 Dezember 2007 17:49 ] [ ID #1887701 ]

Re: working with paths

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 <stephane_chaze... [at] yahoo.fr>
wrote:
> On Fri, 7 Dec 2007 07:42:10 -0800 (PST), ri... [at] 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
RickM [ Fr, 07 Dezember 2007 18:08 ] [ ID #1887702 ]

Re: working with paths

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"
Icarus Sparry [ Fr, 07 Dezember 2007 19:17 ] [ ID #1887707 ]

Re: working with paths

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 <use... [at] icarus.freeuk.com> 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"
RickM [ Fr, 07 Dezember 2007 20:20 ] [ ID #1887712 ]

Re: working with paths

rickm [at] 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
krahnj [ Fr, 07 Dezember 2007 22:12 ] [ ID #1887716 ]

Re: working with paths

On Dec 7, 1:12 pm, "John W. Krahn" <kra... [at] telus.net> wrote:
> ri... [at] 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
RickM [ Fr, 07 Dezember 2007 22:45 ] [ ID #1887719 ]

Re: working with paths

On Fri, 7 Dec 2007 09:08:33 -0800 (PST), rickm [at] 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
Stephane CHAZELAS [ Fr, 07 Dezember 2007 23:34 ] [ ID #1887721 ]

Re: working with paths

rickm [at] galaxy.nsc.com wrote:
>
> On Dec 7, 1:12 pm, "John W. Krahn" <kra... [at] telus.net> wrote:
> > ri... [at] 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
krahnj [ Fr, 07 Dezember 2007 23:34 ] [ ID #1887722 ]
Linux » comp.unix.shell » working with paths

Vorheriges Thema: Sum of numbers in a file
Nächstes Thema: Set a variable to every line of a find -exec loop