Operating System - HP-UX
1752790 Members
5753 Online
108789 Solutions
New Discussion юеВ

Escaping the % from values with any number of digits

 
SOLVED
Go to solution
RamkumarA
Respected Contributor

Escaping the % from values with any number of digits

Hi Experts,

I want to escape only the % from the below values irrespective of the number of digits.
I tried echo "10%" | grep -v %, etc but its suppressing the whole. Also i tried cut -c, but its used to cut based on the list of characters like cut -c 1-2 extracts first 2, etc..

10%
120%
1234%
12345%
.....
.....
and so on

Is there any single command to escape the % irrespective of the number of digits?

Thanks,
Ramkumar A.
6 REPLIES 6
Patrick Wallek
Honored Contributor
Solution

Re: Escaping the % from values with any number of digits

Are these values in a file?

For example:

# cat file
10%
100%
1000%
10000%

# sed 's/%//g' file
10
100
1000
10000
Michal Kapalka (mikap)
Honored Contributor

Re: Escaping the % from values with any number of digits

hi,

actually i don;t know if you like to remove only this character % from the lines ??? if yes use this :

this is your example :

echo "10%" | sed -e 's/%//g'

if you number with your character % are in a file you could use this.

sed -e 's/%//g' file.name

mikap
Bob E Campbell
Honored Contributor

Re: Escaping the % from values with any number of digits

and if you do not want to lose any % chars not associated with something other than a number you could tweak what Patrick showed with:

sed -e "s/\([0-9][0-9]*\)%/\1/"
RamkumarA
Respected Contributor

Re: Escaping the % from values with any number of digits

Hi Patrick,

That's what i was looking for and your solution is perfect. Thank you very much.

Thanks,
Ramkumar A.
RamkumarA
Respected Contributor

Re: Escaping the % from values with any number of digits

Thank you very much guys...
RamkumarA
Respected Contributor

Re: Escaping the % from values with any number of digits

Got solution from Patrick.