LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-26-2017, 06:15 AM   #1
pedropt
Member
 
Registered: Aug 2014
Distribution: Devuan
Posts: 345

Rep: Reputation: Disabled
Arrange numbers in sequence from a variable


This is probably a simple code , but i was looking in sort command and none of the switches work for this case .

for example :

Code:
#!/bin/bash
echo -n "Write a number not in order : "
read -r mynmb
varout=$(echo $mynmb | sort -n)

echo""
echo "You wrote : $mynmb  and was arranget in order by this : $varout"
basically i want to arrange :
example :

from : 283741 to 123478

This is probably simple , but i cant get into it directly from a variable .

Last edited by pedropt; 12-29-2017 at 02:11 AM.
 
Old 12-26-2017, 07:11 AM   #2
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,552

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
Probably the ugliest way
Code:
varout=$(echo -n $mynmb | fold -w1 | sort -n | tr -d "\n")

Last edited by keefaz; 12-26-2017 at 07:12 AM.
 
Old 12-26-2017, 07:18 AM   #3
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,784

Rep: Reputation: 5937Reputation: 5937Reputation: 5937Reputation: 5937Reputation: 5937Reputation: 5937Reputation: 5937Reputation: 5937Reputation: 5937Reputation: 5937Reputation: 5937
Sort works on a data set or list not a single string.
Along the same line of thought... grep -o . separates the number before it gets piped to sort and recombined by tr.
Code:
$varout=$(echo $mynmb | grep -o . | sort | tr -d "\n")
Or perl
Code:
$varout=$(echo $mynmb | perl -F -lane 'print sort @F')
 
Old 12-26-2017, 08:01 AM   #4
pedropt
Member
 
Registered: Aug 2014
Distribution: Devuan
Posts: 345

Original Poster
Rep: Reputation: Disabled
Thanks guys , it works perfectly .

Let me just leave the correct code here finished for others to use :

Code:
#!/bin/bash
echo -n "Write a number not in order : "
read -r mynmb
varout=$(echo $mynmb | grep -o . | sort | tr -d "\n")

echo""
echo "You wrote : $mynmb  and was arranged in order by this : $varout"
 
Old 12-26-2017, 08:28 AM   #5
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,552

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
If the user doesn't know, I wonder what he/she will understand with:
"Write a number not in order"

I know I would be stuck at this point
 
  


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
[SOLVED] sequential : how to find the missing numbers within a sequence of files that have sequential numbers attached to them? BW-userx Programming 71 07-15-2017 08:52 PM
next sequence of numbers ????? fachamix General 11 11-30-2009 06:52 AM
[SOLVED] regex with numbers in sequence schneidz Programming 6 04-28-2009 06:52 PM
How do I check sequence of and re-arrange modules loaded at startup? SP7 Linux - Software 5 05-24-2008 01:41 AM
sequence of numbers, how to extract which numbers are missing jonlake Programming 13 06-26-2006 03:28 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:40 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