# Zero typing problem!

**URL:** <https://community.unix.com/t/zero-typing-problem/318510>\
**Category:** Shell Programming and Scripting\
**Created:** [October 19, 2012, 8:20am UTC](https://community.unix.com/t/zero-typing-problem/318510 "2012-10-19T08:20:16Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![nixhead](https://community.unix.com/letter_avatar/nixhead/32/6_449c533f586939b6e5f246632a46421f.png) [@nixhead](https://community.unix.com/u/nixhead)\
**Post date:** [October 19, 2012, 8:20am UTC](https://community.unix.com/t/zero-typing-problem/318510/1 "2012-10-19T08:20:16Z")

</div>

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

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

```

---

<div class="post-metadata">

**Author:** ![Scott](https://community.unix.com/letter_avatar/scott/32/6_449c533f586939b6e5f246632a46421f.png) [@Scott](https://community.unix.com/u/Scott)\
**Post date:** [October 19, 2012, 8:23am UTC](https://community.unix.com/t/zero-typing-problem/318510/2 "2012-10-19T08:23:02Z")

</div>

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 🙂

---

<div class="post-metadata">

**Author:** ![jim\_mcnamara](https://community.unix.com/letter_avatar/jim_mcnamara/32/6_449c533f586939b6e5f246632a46421f.png) [@jim\_mcnamara](https://community.unix.com/u/jim_mcnamara)\
**Post date:** [October 19, 2012, 8:25am UTC](https://community.unix.com/t/zero-typing-problem/318510/3 "2012-10-19T08:25:09Z")

</div>

See if this works for you

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

```

---

<div class="post-metadata">

**Author:** ![pamu](https://community.unix.com/letter_avatar/pamu/32/6_449c533f586939b6e5f246632a46421f.png) [@pamu](https://community.unix.com/u/pamu)\
**Post date:** [October 19, 2012, 8:29am UTC](https://community.unix.com/t/zero-typing-problem/318510/4 "2012-10-19T08:29:44Z")

</div>

Try using double quotes..

```nohighlight
#!/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)

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

```

---

<div class="post-metadata">

**Author:** ![Subbeh](https://community.unix.com/letter_avatar/subbeh/32/6_449c533f586939b6e5f246632a46421f.png) [@Subbeh](https://community.unix.com/u/Subbeh)\
**Post date:** [October 19, 2012, 8:36am UTC](https://community.unix.com/t/zero-typing-problem/318510/6 "2012-10-19T08:36:38Z")

</div>

The easiest way would be to do this:

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

```

---

<div class="post-metadata">

**Author:** ![Scott](https://community.unix.com/letter_avatar/scott/32/6_449c533f586939b6e5f246632a46421f.png) [@Scott](https://community.unix.com/u/Scott)\
**Post date:** [October 19, 2012, 8:37am UTC](https://community.unix.com/t/zero-typing-problem/318510/7 "2012-10-19T08:37:56Z")

</div>

> [@subbeh](#):
>
> The easiest way would be to do this:
> 
> ```plaintext
> $ echo ${str##*\\}
> 00000Hgg
> 
> ```

Not in sh in Solaris it wouldn't 🙂

---

<div class="post-metadata">

**Author:** ![nixhead](https://community.unix.com/letter_avatar/nixhead/32/6_449c533f586939b6e5f246632a46421f.png) [@nixhead](https://community.unix.com/u/nixhead)\
**Post date:** [October 19, 2012, 9:34am UTC](https://community.unix.com/t/zero-typing-problem/318510/9 "2012-10-19T09:34:01Z")

</div>

Any solution guys!

---

<div class="post-metadata">

**Author:** ![jim\_mcnamara](https://community.unix.com/letter_avatar/jim_mcnamara/32/6_449c533f586939b6e5f246632a46421f.png) [@jim\_mcnamara](https://community.unix.com/u/jim_mcnamara)\
**Post date:** [October 19, 2012, 10:19am UTC](https://community.unix.com/t/zero-typing-problem/318510/10 "2012-10-19T10:19:53Z")

</div>

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:

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

```

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

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

```

I called my script t.shl

---

<div class="post-metadata">

**Author:** ![nixhead](https://community.unix.com/letter_avatar/nixhead/32/6_449c533f586939b6e5f246632a46421f.png) [@nixhead](https://community.unix.com/u/nixhead)\
**Post date:** [October 21, 2012, 11:42pm UTC](https://community.unix.com/t/zero-typing-problem/318510/11 "2012-10-21T23:42:00Z")

</div>

Hi Jim

You solution has not worked for me,  
I put

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

```

and I got output:

```plaintext
asaks00000asjahsjs vvfivfh.txt 

```

as output  
☹

> [@jim mcnamara](#):
>
> 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:
> 
> ```plaintext
> #!/bin/sh
> str=$@
> echo "$str" | nawk -F '\\' '{ print $NF}'
> exit
> 
> ```
> 
> Just tested it - NOTE: you have to put tics around the filename
> 
> ```plaintext
> ./t.shl 'abc\0000\abc'
> abc
> 
> ```
> 
> I called my script t.shl

---

<div class="post-metadata">

**Author:** ![pamu](https://community.unix.com/letter_avatar/pamu/32/6_449c533f586939b6e5f246632a46421f.png) [@pamu](https://community.unix.com/u/pamu)\
**Post date:** [October 22, 2012, 2:19am UTC](https://community.unix.com/t/zero-typing-problem/318510/12 "2012-10-22T02:19:10Z")

</div>

> [@nixhead](#):
>
> Hi Jim
> 
> You solution has not worked for me,

Please show us what you have tried...

check this .. at least one should work for you...😃

```plaintext
$ 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

```

---

<div class="post-metadata">

**Author:** ![Scrutinizer](https://community.unix.com/user_avatar/community.unix.com/scrutinizer/32/1216_2.png) [@Scrutinizer](https://community.unix.com/u/Scrutinizer)\
**Post date:** [October 22, 2012, 2:32am UTC](https://community.unix.com/t/zero-typing-problem/318510/13 "2012-10-22T02:32:52Z")

</div>

> [@nixhead](#):
>
> Hi Jim
> 
> You solution has not worked for me,  
> I put
> 
> ```plaintext
> ./convert.sh "\\code\abc\asaks\00000asjahsjs vvfivfh.txt
> 
> ```
> 
> and I got output:
> 
> ```plaintext
> 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).
