Re: modifing a path (string)

Re: modifing a path (string)

am 18.12.2007 00:57:47 von RickM

On Dec 17, 3:15 pm, Janis Papanagnou
wrote:
> ri...@galaxy.nsc.com wrote:
> > On Dec 17, 2:36 pm, Janis Papanagnou
> > wrote:
>
> >>ri...@galaxy.nsc.com wrote:
>
> >>>I need to modify a path sttring thats in a file and looks like this:
>
> >>>before :
>
> >>>define dirF /dirA/dirB/dirC/dirD/dirF
>
> >>>after
>
> >>>define dirF ./dirF
>
> >> sed 's, /.*/, ./,' < oldfile > newfile
>
> >>Janis
>
> >>>basically, remove all of the path string except for the last portion
>
> >>>Thanks
>
> >>>RIck
>
> > I got the desired results with the below line but I doubt its the best
> > solution:
>
> > awk -F / '/DEFINE/ { print $1 "./"$NF }' cds.lib
>
> Awk operates as most Unix tools per default _case sensitive_; /define/.
>
>
>
> > Can you pleae briefly explain how sed 's, /.*/, ./,' < oldfile
>
> >>newfile
>
> > works......in a nutshell of course!
>
> Basic syntax
>
> sed s/pattern/replacement/
>
> substitutes pattern by replacement.
>
> Per convention sed uses / as separator but accepts other characters, too.
> If one handles path names with many slashes each slash has to be escaped
> by a backslash - which makes expressions quite unreadable.
>
> My example uses a , (comma) as separator to avoid escaping the slashes.
> Then you find " /.*/" as pattern - a space, a slash, any character (.*)
> until the final slash (greedy matching) - and " ./" as replacement for
> the above matched pattern.
>
> Janis
>
>
>
> > THANKS!!!!!!!

but define dirF /dirA/dirB/dirC/dirD/dirF has lots of cases
that match....right. ...or does
it work because the last field is where it stop looking. finds
dirA but its overwritten by dirB
and so on?

Why does the define dirF get outputted?

Re: modifing a path (string)

am 18.12.2007 01:10:15 von Janis Papanagnou

rickm@galaxy.nsc.com wrote:
> On Dec 17, 3:15 pm, Janis Papanagnou
> wrote:
>
>>ri...@galaxy.nsc.com wrote:
>>
>>>On Dec 17, 2:36 pm, Janis Papanagnou
>>>wrote:
>>
>>>>ri...@galaxy.nsc.com wrote:
>>
>>>>>I need to modify a path sttring thats in a file and looks like this:
>>
>>>>>before :
>>
>>>>>define dirF /dirA/dirB/dirC/dirD/dirF
>>
>>>>>after
>>
>>>>>define dirF ./dirF
>>
>>>> sed 's, /.*/, ./,' < oldfile > newfile
>>
>>>>Janis
>>
>>>>>basically, remove all of the path string except for the last portion
>>
>>>>>Thanks
>>
>>>>>RIck
>>
>>>I got the desired results with the below line but I doubt its the best
>>>solution:
>>
>>>awk -F / '/DEFINE/ { print $1 "./"$NF }' cds.lib
>>
>>Awk operates as most Unix tools per default _case sensitive_; /define/.
>>
>>
>>
>>
>>>Can you pleae briefly explain how sed 's, /.*/, ./,' < oldfile
>>
>>>>newfile
>>
>>>works......in a nutshell of course!
>>
>>Basic syntax
>>
>> sed s/pattern/replacement/
>>
>>substitutes pattern by replacement.
>>
>>Per convention sed uses / as separator but accepts other characters, too.
>>If one handles path names with many slashes each slash has to be escaped
>>by a backslash - which makes expressions quite unreadable.
>>
>>My example uses a , (comma) as separator to avoid escaping the slashes.
>>Then you find " /.*/" as pattern - a space, a slash, any character (.*)
>>until the final slash (greedy matching) - and " ./" as replacement for
>>the above matched pattern.
>>
>>Janis
>>
>>
>>
>>
>>>THANKS!!!!!!!
>
>
> but define dirF /dirA/dirB/dirC/dirD/dirF has lots of cases
> that match....right. ...or does
> it work because the last field is where it stop looking. finds
> dirA but its overwritten by dirB
> and so on?

That's what I meant by "greedy matching". Most tools support (primarily)
greedy matching.

. matches an arbitrary character
.* matches any amount of arbitrary characters (incl. '/')
/.* matches a / and any amount of arbitrary characters (incl. '/')
/.*/ since / is included in .* it continues to search until the
last / that it finds on the line

If you want to not include any intermediate slashes you have to use a
different pattern than the . (any character). For example

/[^/]*/ where [^/] means "any character but the '/'".

You may want to search the web for a regexp tutorial to learn more about
it.

>
> Why does the define dirF get outputted?

Because the pattern started with a blank followed by a slash " /", so
anything before that pattern won't be substituted.

Janis