Zero typing problem!

H,
I
I have this below script for removing the full path from a string which is indeed a filepath location if windows.
It converts input
\abc\asssh\abc
To
abc

But if filename has 0 like:
\abc\abc\00000Hgg
Then its typing
abc00000Hgg

PLEASE note that its solaris.
Script is:
more convertname.sh

#!/bin/sh
str=$@
echo $str | sed 's/.*\\//'
exit 0

Strange!

You can use basename to get the last part of a path.

As sh means sh in Solaris, that's probably the easiest approach.

Edit: Seing that it's backslashes, not forward slashes, basename might not cut it, but I don't understand if those are normal (ASCII) zero's why sed would behave this way.

Edit 2: Duh! Because sed treats \0 as a Null character!

Edit 3: That was a duh @ me, not a duh @ you :slight_smile:

See if this works for you

str=$@
echo "$str" | tr -s '\' ' ' | nawk '{print $(NF)}' 

Try using double quotes..

#!/bin/sh
str=$@
echo "$str" | sed 's/.*\\//'
exit 0

@Jim

I think we can directly do like this also(Don't know is there any problem with Solaris:D)

echo "$str" | nawk -F '\' '{ print $NF}'

The easiest way would be to do this:

$ echo ${str##*\\}
00000Hgg

Not in sh in Solaris it wouldn't :slight_smile:

Any solution guys!

I guess you did not get the implication that you need to use one of the suggestions
in your code. Rewrite the code with pamu's suggestion:

#!/bin/sh
str=$@
echo "$str" | nawk -F '\\' '{ print $NF}'
exit

Just tested it - NOTE: you have to put tics around the filename

./t.shl 'abc\0000\abc'
abc

I called my script t.shl

Hi Jim

You solution has not worked for me,
I put

./convert.sh "\\code\abc\asaks\00000asjahsjs vvfivfh.txt

and I got output:

asaks00000asjahsjs vvfivfh.txt 

as output
:frowning:

Please show us what you have tried...

check this .. at least one should work for you...:smiley:

$ cat test.sh
#!/bin/sh
str=$@
echo "1 - awk"
echo "$str" | awk -F '\\' '{ print $NF}'
echo "2 - sed"
echo "$str" | sed 's/.*\\//g'
echo "3 - tr + awk"
echo "$str" | tr '\\' '\n' | awk 'END{print }'
exit

$ sh test.sh "\\code\abc\asaks\00000asjahsjs vvfivfh.txt"
1 - awk
00000asjahsjs vvfivfh.txt
2 - sed
00000asjahsjs vvfivfh.txt
3 - tr + awk
00000asjahsjs vvfivfh.txt

That is not the same. You are using double quotes instead of single quotes (and you left out the second quote).