Swapping a string of numbers between higher and lower order values(HEX) | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Swapping a string of numbers between higher and lower order values(HEX)

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 02-05-2013
vivek d r's Avatar
vivek d r vivek d r is offline
Registered User
 
Join Date: Nov 2011
Last Activity: 2 April 2014, 1:16 AM EDT
Location: Bangalore, INDIA
Posts: 277
Thanks: 124
Thanked 3 Times in 3 Posts
Swapping a string of numbers between higher and lower order values(HEX)

I have this below string in a variable


Code:
cutString=21222222222222222122222222222222

this string is nothing but hex values depicted as below


Code:
 
21:22:22:22:22:22:22:22:21:22:22:22:22:22:22:22


so what i want to achieve is swap the lower order with higher order values in the string

Code:
21222222222222222122222222222222
 
output: 
12222222222222221222222222222222

let me give you another exaample


Code:
12345678901234567890123456789012
 
output:
21436587092143658709214365870921

also i need to do one more implementation

first i need to store this convertted output in one variable say nextString

later i need to introduce a space between each byte as below so that i can loop it in for loop and perform some operation..(PS: there shouldnt be an extra space at the end of string)


Code:
2 1 4 3 6 5 8 7 0 9 2 1 4 3 6 5 8 7 0 9 2 1 4 3 6 5 8 7 0 9 2 1



any help is deeply appreciated. thanks.

even if i can just learn how to introucde spaces, i can handle swapping in for loop by myself. :-)
Sponsored Links
    #2  
Old 02-05-2013
Scrutinizer's Avatar
Scrutinizer Scrutinizer is offline Forum Staff  
Moderator
 
Join Date: Nov 2008
Last Activity: 17 April 2014, 10:03 PM EDT
Location: Amsterdam
Posts: 8,874
Thanks: 230
Thanked 2,141 Times in 1,922 Posts
Like so?

Code:
sed 's/\(.\)\(.\)/\2 \1 /g'

The Following User Says Thank You to Scrutinizer For This Useful Post:
vivek d r (02-05-2013)
Sponsored Links
    #3  
Old 02-05-2013
pamu pamu is offline
Registered User
 
Join Date: Mar 2012
Last Activity: 14 April 2014, 6:10 AM EDT
Posts: 1,640
Thanks: 58
Thanked 476 Times in 472 Posts
or sth like this..


Code:
 
echo "12345678901234567890123456789012" | rev | sed 's/./& /g'

    #4  
Old 02-05-2013
vivek d r's Avatar
vivek d r vivek d r is offline
Registered User
 
Join Date: Nov 2011
Last Activity: 2 April 2014, 1:16 AM EDT
Location: Bangalore, INDIA
Posts: 277
Thanks: 124
Thanked 3 Times in 3 Posts
Thanks a lot Scrutinizer.. the command works good :-)

@pamu.. thanks but that code is reversing the whole string and not higher value to lower value. but thanks anyway :-)
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
swapping the values of variable! nikki1200 Shell Programming and Scripting 5 12-22-2011 11:23 AM
Changing and swapping the Values in the files polineni Shell Programming and Scripting 2 11-03-2011 08:22 AM
if test for higher value between 2 decimal numbers emjs Shell Programming and Scripting 5 07-29-2011 09:05 AM
How to convert hex numbers to decimal ? jack2 Shell Programming and Scripting 15 04-07-2009 09:24 AM
ksh and hex numbers JamesByars Shell Programming and Scripting 2 01-15-2008 02:36 PM



All times are GMT -4. The time now is 01:52 AM.