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
Scott
October 19, 2012, 8:23am
2
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
See if this works for you
str=$@
echo "$str" | tr -s '\' ' ' | nawk '{print $(NF)}'
pamu
October 19, 2012, 8:29am
4
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}'
Subbeh
October 19, 2012, 8:36am
6
The easiest way would be to do this:
$ echo ${str##*\\}
00000Hgg
Scott
October 19, 2012, 8:37am
7
Not in sh in Solaris it wouldn't
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
nixhead
October 21, 2012, 11:42pm
11
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
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
pamu
October 22, 2012, 2:19am
12
Please show us what you have tried...
check this .. at least one should work for you...
$ 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
nixhead:
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
That is not the same. You are using double quotes instead of single quotes (and you left out the second quote).