Unix/Linux Go Back    


Shell Programming and Scripting Unix shell scripting - KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and shell scripts and shell scripting languages here.

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

Shell Programming and Scripting


Closed Linux or Unix Question    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 02-05-2013
vivek d r's Unix or Linux Image
vivek d r vivek d r is offline
Registered User
 
Join Date: Nov 2011
Last Activity: 8 April 2015, 10:02 AM EDT
Location: Bangalore, INDIA
Posts: 285
Thanks: 126
Thanked 4 Times in 4 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 Unix and Linux 02-05-2013
Scrutinizer's Unix or Linux Image
Scrutinizer Scrutinizer is online now Forum Staff  
Moderator
 
Join Date: Nov 2008
Last Activity: 23 May 2015, 10:47 AM EDT
Location: Amsterdam
Posts: 10,202
Thanks: 332
Thanked 2,692 Times in 2,408 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 Unix and Linux 02-05-2013
pamu pamu is offline
Registered User
 
Join Date: Mar 2012
Last Activity: 7 November 2014, 10:50 AM EST
Posts: 1,650
Thanks: 58
Thanked 478 Times in 474 Posts
or sth like this..


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

    #4  
Old Unix and Linux 02-05-2013
vivek d r's Unix or Linux Image
vivek d r vivek d r is offline
Registered User
 
Join Date: Nov 2011
Last Activity: 8 April 2015, 10:02 AM EDT
Location: Bangalore, INDIA
Posts: 285
Thanks: 126
Thanked 4 Times in 4 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 Linux or Unix Question

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux 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 10:53 AM.