Decoding a string


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Decoding a string
# 8  
Old 03-19-2012
You can use regular expression match

Code:
if [ "${ch}" =~ "[0-9]" ]

# 9  
Old 03-19-2012
the match is not working as expected:
Code:
#!/bin/bash
len=`echo $1|wc -c`
a=$1
for((i=0;i<=$len-1;i++))
do
ch=${a:$i:1}
echo $ch
if [ "${ch}" =~ "[0-9]" ]
then
res=${ch}
else
res=`echo "$res",`
fi
done

Code:
pandeeswaran@ubuntu:~/training$ bash RunLength_decode 3a
3
RunLength_decode: line 14: [: =~: binary operator expected
a
RunLength_decode: line 14: [: =~: binary operator expected

RunLength_decode: line 14: [: =~: binary operator expected

# 10  
Old 03-19-2012
Try with double brackets.. like this:
Code:
if [[ "${ch}" =~ "[0-9]" ]]

# 11  
Old 03-19-2012
Which bash version you are using

there was a change between 3.1 and 3.2

if its 3.2 try

Code:
 
if [[ "${ch}" =~ [0-9] ]]

With out double quotes on regex.

Thanks Balasuri for correction Smilie
# 12  
Old 03-19-2012
@max_hammer: I don't think [ "$x" =~ [0-9] ] would work on bash 3.2 either.

I'm using GNU bash, version 3.2.25 and [ "$x" =~ [0-9] ] doesn't work. Regex matching is not supported by single square brackets.
This User Gave Thanks to balajesuri For This Post:
# 13  
Old 03-19-2012
Code:
if [[ "${ch}" =~ [0-9] ]];

does the trick
# 14  
Old 03-19-2012
Thanks balajesuri for correction and information.

I am using ksh and single bracket works fine for ksh. Smilie
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. OS X (Apple)

Unicode encoding and decoding, OSX 10.13.5.

I am struggling here to understand...... The default encoding. See photo 1. Why does this NOT work? #!/bin/bash # Code for OSX 10.13.5. default UNICODE encoding. echo"" echo "The default UTF-8..." locale echo"" echo "Change to 8 bit ASCII only..." LANG="en_GB.US-ASCII" export... (2 Replies)
Discussion started by: wisecracker
2 Replies

2. Programming

ASN1 decoding error

Hi, fellows i am modifying asn1 schema to be able to decode a file, but i am hitting a error on one of the fields using free online tool asn1-playground. I suspect i need to change type and have tried with IDENTIFIER but it doesn't help...any ideas check the schema and file down , please ... (0 Replies)
Discussion started by: tahchiev01
0 Replies

3. Shell Programming and Scripting

Decoding and pattern matching

Hello, I have a huge file with over 700,00 SNPs with 18 columns. One column is in the format --+-+ ---++ ????? -???? Now i have another list which corresponds to this code in a particular order A-1 B-7 C-11 D-3 E-100 Now I need to match the expression above to the pattern,... (1 Reply)
Discussion started by: nans
1 Replies

4. Shell Programming and Scripting

FTP decoding

I am trying to understand a UNIX script which FTPs certain files from a remote location to the local machine. I understand the basic FTP command but the UNIX script uses the following command: ftp -n -i -v > $logftp_trg 2>&1 <<! open $MFX_FTP_SERVER user $MFX_FTP_LOGIN $MFX_FTP_PWD Can anyone... (5 Replies)
Discussion started by: Bhavesh Sharma
5 Replies

5. Shell Programming and Scripting

[Solved] Decoding a base 64 string

Is it possible to decode a base 64 string in linux or unix. If so, related commands or reference notes would be really helpful. (1 Reply)
Discussion started by: chandu123
1 Replies

6. UNIX for Dummies Questions & Answers

URL decoding with awk

The challenge: Decode URL's, i.e. convert %HEX to the corresponding special characters, using only UNIX base utilities, and without having to type out each special character. I have an anonymous C code snippet where the author assigns each hex digit a number from 0 to 16 and then does some... (2 Replies)
Discussion started by: uiop44
2 Replies

7. IP Networking

Packet decoding

Hi, wondering if anyone can suggest a tool to me that will let me either cut & paste hex or type it in for packet decoding. I want to be able to decode a packet as done with tcpdump or wireshark, but I want to be able to manually input the hex myself. (2 Replies)
Discussion started by: Breakology
2 Replies

8. Solaris

problem when decoding a binary file

I tried to decode a binary script using the command 'uudecode'. but it is giving error as 'No begining line'. 'uudecode -o <outfile name> <binary file>' Please help me in resolving this. (4 Replies)
Discussion started by: vamshikrishnab
4 Replies

9. Shell Programming and Scripting

decoding commands

hi please can anyone help me in decoding shell commands. i need a way to decode the encrypted shell commands. (8 Replies)
Discussion started by: rochitsharma
8 Replies

10. Programming

Decoding of Core Dump

Hi ALL, Is it possible to decode the core dumb file to find the error? I get an Memory Core Dumb error with an core file. Regards, P. Prathaban. (3 Replies)
Discussion started by: p_prathaban
3 Replies
Login or Register to Ask a Question