| Author |
Message |
MinHundHettePerro

Joined: 05 Feb 2009 Posts: 831 Location: SE
|
Posted: Sun 29 Jan 2012, 21:15 Post subject:
Non-BASH lower-to-UPPER case conversion of a-f |
|
Hello !
Is there a simple way to express the bashishm (at least, I think it's a bashism, correct me if I'm wrong) | Code: | | echo "${WHATEVER^^}" | in a non-bashy way?
(Reason for asking: Reading binary files through hexdump and echoing to bc, calls for a lower-to-UPPER case conversion of a-f.)
Cheers / MHHP
_________________ Celeron 2.8 GHz, 1 GiB RAM, i82845 graphics, many partitions, Pupmode 12 (13)
Mostly running Slacko & 214X
Nämen, vaf.... ln -s /dev/null MHHP
|
|
Back to top
|
|
 |
seaside
Joined: 11 Apr 2007 Posts: 836
|
Posted: Sun 29 Jan 2012, 21:45 Post subject:
|
|
MinHundHettePerro,
Here's a snippet from somewhere.
uppers=ABCDEFGHIJKLMNOPQRSTUVWXYZ
lowers=abcdefghijklmnopqrstuvwxyz
| Code: | uc(){ #usage: uc "some string" -> "SOME STRING"
i=0
while ([ $i -lt ${#1} ]) do
CUR=${1:$i:1}
case $lowers in
*$CUR*)CUR=${lowers%$CUR*};OUTPUT="${OUTPUT}${uppers:${#CUR}:1}";;
*)OUTPUT="${OUTPUT}$CUR";;
esac
i=$((i+1))
done
echo "${OUTPUT}"
}
|
cheers,
s
|
|
Back to top
|
|
 |
amigo
Joined: 02 Apr 2007 Posts: 1759
|
Posted: Mon 30 Jan 2012, 02:04 Post subject:
|
|
'echo "${WHATEVER^^}"'
Where did you get that? I've never seen such syntax and it does not work here at all.
'echo $WHATEVER | tr [a-f] [A-F]'
does what you want.
|
|
Back to top
|
|
 |
technosaurus

Joined: 18 May 2008 Posts: 3843
|
Posted: Mon 30 Jan 2012, 03:13 Post subject:
|
|
@amigo - ^^ another undocumented bashism
I was trying to rewrite ndiswrapper in shell and came up with these
this one is to be used in case statements, but haven't really tested it yet
| Code: | anycase(){ #make a string usable by case as case insensitive
#input hello world output [hH][eE][lL][lL][oO]" "[wW][oO][rR][lL][dD]
uppers=ABCDEFGHIJKLMNOPQRSTUVWXYZ
lowers=abcdefghijklmnopqrstuvwxyz
i=0
while ([ $i -lt ${#1} ]) do
CUR=${1:$i:1}
case "$uppers" in
*$CUR*)CUR="${uppers%$CUR*}";OUTPUT="${OUTPUT}[${lowers:${#CUR}:1}${uppers:${#CUR}:1}]";;
*)case "$lowers" in
*$CUR*)CUR="${lowers%$CUR*}";OUTPUT="${OUTPUT}[${lowers:${#CUR}:1}${uppers:${#CUR}:1}]";;
*)OUTPUT="${OUTPUT}\"$CUR\"";;
esac
esac
i=$((i+1))
done
printf "${OUTPUT}"
} |
| Code: | lc(){ #convert string to lower case and print it
uppers=ABCDEFGHIJKLMNOPQRSTUVWXYZ
lowers=abcdefghijklmnopqrstuvwxyz
i=0
while ([ $i -lt ${#1} ]) do
CUR=${1:$i:1}
case $uppers in
*$CUR*)CUR=${uppers%$CUR*};OUTPUT="${OUTPUT}${lowers:${#CUR}:1}";;
*)OUTPUT="${OUTPUT}$CUR";;
esac
i=$((i+1))
done
printf "${OUTPUT}"
} |
which really just does this:
| Code: | tolower() { #change input $@ to lower case
echo "$@" | tr [:upper:] [:lower:]
} |
for upper case just do the reverse
Edit: btw, the reason why I started the anycase function, is because of the case insensitivity in Windows, ... otherwise you would need to convert every single line to lower case for comparison (which could take a while in large files) ... its faster in that case to have one case insensitive case statement
_________________ Puppy Web Desktop Now with pet packages - Pet Packaging 100 & 101
|
|
Back to top
|
|
 |
Karl Godt

Joined: 20 Jun 2010 Posts: 2677 Location: Kiel,Germany
|
Posted: Mon 30 Jan 2012, 11:01 Post subject:
|
|
bash version 3 does not work with ^ on my side .
|
|
Back to top
|
|
 |
amigo
Joined: 02 Apr 2007 Posts: 1759
|
Posted: Mon 30 Jan 2012, 12:34 Post subject:
|
|
Ah Karl, I am using bash3 also -that explains it.
|
|
Back to top
|
|
 |
MinHundHettePerro

Joined: 05 Feb 2009 Posts: 831 Location: SE
|
Posted: Mon 30 Jan 2012, 15:53 Post subject:
|
|
I was in slacko | Code: | # bash --version
GNU bash, version 4.1.0(1)-release (i486-t2-linux-gnu) |
Good thing I asked, as the ^-syntax wouldn't have been too portable ...
Anyway, thanks for the tr reminder ...
Before, I've used constructs like | Code: | # WHATEVER="0123456789abCDef"
# A="${WHATEVER//a/A}";A="${A//b/B}";A="${A//c/C}";A="${A//d/D}";A="${A//e/E}";A="${A//f/F}" ;echo "${A}"
0123456789ABCDEF
#
| and just fell for the simplicity of ^^ .....
... I'd better avoid these bleeding-edge bashisms ...
Cheers / MHHP
_________________ Celeron 2.8 GHz, 1 GiB RAM, i82845 graphics, many partitions, Pupmode 12 (13)
Mostly running Slacko & 214X
Nämen, vaf.... ln -s /dev/null MHHP
|
|
Back to top
|
|
 |
GustavoYz

Joined: 07 Jul 2010 Posts: 867 Location: .ar
|
Posted: Mon 30 Jan 2012, 16:19 Post subject:
Re: Non-BASH lower-to-UPPER case conversion of a-f |
|
| MinHundHettePerro wrote: | | ...non-bashy way? |
If you're passing strings, this Perl way might work:
| Code: | | echo SOMETHING | perl -nle 'print lc' |
_________________

|
|
Back to top
|
|
 |
|