Unix Shell - modifing a path (string)

This is Interesting: Free IT Magazines  
Home > Archive > Unix Shell > December 2007 > modifing a path (string)





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author modifing a path (string)
rickm@galaxy.nsc.com

2007-12-17, 7:21 pm

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

basically, remove all of the path string except for the last portion

Thanks

RIck
Janis Papanagnou

2007-12-17, 7:21 pm

rickm@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

rickm@galaxy.nsc.com

2007-12-17, 7:21 pm

On Dec 17, 2:36 pm, Janis Papanagnou <Janis_Papanag...@hotmail.com>
wrote:
> ri...@galaxy.nsc.com wrote:
>
>
>
>
>
> sed 's, /.*/, ./,' < oldfile > newfile
>
> Janis
>
>
>
>
>

I got the desired results with the below line but I doubt its the best
solution:

awk -F / '/DEFINE/ { print $1 "./"$NF }' cds.lib

Can you pleae briefly explain how sed 's, /.*/, ./,' < oldfile[vbcol=seagreen]
> newfile

works......in a nutshell of course!

THANKS!!!!!!!



Janis Papanagnou

2007-12-17, 7:21 pm

rickm@galaxy.nsc.com wrote:
> On Dec 17, 2:36 pm, Janis Papanagnou <Janis_Papanag...@hotmail.com>
> wrote:
>
>
>
> 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
>
>
> 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!!!!!!!
>
>
>

rickm@galaxy.nsc.com

2007-12-17, 7:21 pm

On Dec 17, 3:15 pm, Janis Papanagnou <Janis_Papanag...@hotmail.com>
wrote:[vbcol=seagreen]
> ri...@galaxy.nsc.com wrote:
>
>
>
>
>
>
>
>
>
>
>
>
>
>
> Awk operates as most Unix tools per default _case sensitive_; /define/.
>
>
>
>
>
>
> 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
>
>
>

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?



Janis Papanagnou

2007-12-17, 7:21 pm

rickm@galaxy.nsc.com wrote:
> On Dec 17, 3:15 pm, Janis Papanagnou <Janis_Papanag...@hotmail.com>
> wrote:
>
>
>
> 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
Bobby.Higgins

2007-12-29, 1:28 pm

Using the shell "Remove Large Left Pattern"
The initial assignment
dirF=/dirA/dirB/dirC/dirD/dirF
could be modified using:
echo "./${dirF##*/}"
The output would be:
../dirF

This should work with ksh, dtksh, and bash.

<rickm@galaxy.nsc.com> wrote in message
news:92d0c268-1611-4db8-90c7-fa64e969313f@e25g2000prg.googlegroups.com...
>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
>
> basically, remove all of the path string except for the last portion
>
> Thanks
>
> RIck



Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2009 webservertalk.com