LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 10-25-2011, 01:39 AM   #1
TimothyBrian
LQ Newbie
 
Registered: Oct 2011
Posts: 3

Rep: Reputation: Disabled
reverse number by twos


how to reverse the numbers sir and mam, see below.. thanks guys!

input file:
8360319029018040012
85951209030597537

output:
3806130992100804102
58592190305079357
 
Old 10-25-2011, 02:06 AM   #2
hen770
Member
 
Registered: Oct 2010
Distribution: Arch
Posts: 136

Rep: Reputation: 7
Explain yourself more.
Do you want the output to be as your post, or you want it to be reveresed?
 
Old 10-25-2011, 02:23 AM   #3
TimothyBrian
LQ Newbie
 
Registered: Oct 2011
Posts: 3

Original Poster
Rep: Reputation: Disabled
lik in the output.. thanks hen770
 
Old 10-25-2011, 02:31 AM   #4
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,066
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
Hi, welcome to LQ!


What's the practial application of this, what languages are you
contemplating to use?


Cheers,
Tink
 
Old 10-25-2011, 02:53 AM   #5
TimothyBrian
LQ Newbie
 
Registered: Oct 2011
Posts: 3

Original Poster
Rep: Reputation: Disabled
UNIX scripting.. thanks..
 
Old 10-25-2011, 03:15 AM   #6
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,246

Rep: Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684
So a posix shell script (assuming full Unix environment)? What have you tried? Where are you stuck?
 
Old 10-25-2011, 11:29 AM   #7
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
It usually helps to explain the context of your problem in detail. Otherwise you force people to waste time asking about why you need it and the environment you're working in, and so on. What exactly are you doing, and why?

I have a possible bash solution ready to go, but I'd like to get some clarification before posting it.
 
1 members found this post helpful.
Old 10-25-2011, 12:07 PM   #8
Juako
Member
 
Registered: Mar 2010
Posts: 202

Rep: Reputation: 84
The following sed oneliner might help:

Code:
sed 's/\([0-9]\)\([0-9]\)/\2\1/g'
 
Old 10-27-2011, 02:13 PM   #9
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
Well, the OP never returned to provide the requested details. Why does that seem to happen so often?

I like Juako's sed command. I should've thought of it. It could be simplified a bit though:
Code:
sed -r 's/(.)(.)/\2\1/g'
This will handle strings of any character (except the delimiter you use), not just numbers.

Anyway, here are the shell versions I came up with. First in bash:
Code:
#!/bin/bash

while read -n 2 line ; do

     printf "%s%s" "${line:1:1}" "${line:0:1}"

done <<<"$1"

echo

exit 0
Now for a posix-compliant shell. It requires a little more work, and an external tool like cut or expr.

Code:
#!/bin/sh

line="$1"

n1=1 n2=2
while [ "$n1" -le "${#line}" ]; do

	printf '%s%s' "$( expr substr "$line" $n2 1 )" "$( expr substr "$line" $n1 1 )"
	n1=$(( n1 + 2 )) n2=$(( n2 + 2 ))

done

echo

exit 0
 
Old 10-27-2011, 08:59 PM   #10
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,246

Rep: Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684
Only cause David started it
Code:
#!/bin/bash

line=$1

for (( i = 0; i <= ${#line}; i += 2 ))
do
    ret+=${line:i+1:1}${line:i:1}
done

echo $ret
 
Old 10-28-2011, 01:26 AM   #11
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,240

Rep: Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324
@David the H.: sounds like a very arbitrary requirement (ie homework ..) and it's new Academic year time in Northern Hemisphere...
I could be wrong of course
 
Old 10-28-2011, 02:49 AM   #12
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
Yeah, that's one reason I held off. But after a couple of days of no-show, and someone else posting a (better) solution first, I figured the damage was done and it wouldn't hurt to go ahead and put it up.
 
Old 10-28-2011, 03:01 AM   #13
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,066
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
Quote:
Originally Posted by Juako View Post
The following sed oneliner might help:

Code:
sed 's/\([0-9]\)\([0-9]\)/\2\1/g'
Indeed ... as others pointed out: this smacks of homework, and
handing a solution on a silver platter isn't really what LQ is
about. We're trying to encourage pupils/students to find their
own solution; guidance is OK.


Cheers,
Tink
 
Old 10-28-2011, 09:00 AM   #14
Juako
Member
 
Registered: Mar 2010
Posts: 202

Rep: Reputation: 84
Sorry, I know what Tinkster says is true, and that the OP disappeared but anyway bash is very fun and Grail/David posts have been inspiring

So here is "Bash, functional style"

Code:
#!/bin/bash
(( ${#1} < 3 )) && echo ${1:1:1}${1:0:1} && exit
echo ${1:1:1}${1:0:1}$($0 ${1:2})
Somehow i feel it can be optimized to one line only though ...
 
Old 10-28-2011, 12:50 PM   #15
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,246

Rep: Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684
@Juako - good thinking and you were almost there:
Code:
#!/bin/bash

(( ${#1} < 1 )) && exit || echo ${1:1:1}${1:0:1}$($0 ${1:2})
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Identify and explain the major number, minor number, and revision number in Linux... turbomen Linux - Newbie 1 11-16-2010 03:48 AM
Reverse Tunneling / Reverse port forwarding in SSH dynamics Linux - Networking 3 11-23-2009 11:31 AM
[SOLVED] bind reverse zone; no name with reverse lookup deadeyes Linux - Server 3 10-12-2009 10:54 AM
SOLVED: Trying to "reverse engineer" a calculation: any number hackers out there? drkstr Slackware 22 08-01-2006 02:01 AM


All times are GMT -5. The time now is 06:48 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration