LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
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, 12: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, 01: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, 01: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, 01:31 AM   #4
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

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


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


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

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

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
So a posix shell script (assuming full Unix environment)? What have you tried? Where are you stuck?
 
Old 10-25-2011, 10:29 AM   #7
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
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, 11:07 AM   #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, 01:13 PM   #9
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
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, 07:59 PM   #10
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,005

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
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, 12:26 AM   #11
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,356

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
@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, 01:49 AM   #12
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
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, 02:01 AM   #13
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
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, 08: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, 11:50 AM   #15
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,005

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
@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



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
Reverse Tunneling / Reverse port forwarding in SSH dynamics Linux - Networking 5 02-07-2017 07:19 AM
Identify and explain the major number, minor number, and revision number in Linux... turbomen Linux - Newbie 1 11-16-2010 02:48 AM
[SOLVED] bind reverse zone; no name with reverse lookup deadeyes Linux - Server 3 10-12-2009 09:54 AM
SOLVED: Trying to "reverse engineer" a calculation: any number hackers out there? drkstr Slackware 22 08-01-2006 01:01 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 12:34 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
Open Source Consulting | Domain Registration