LinuxQuestions.org
Review your favorite Linux distribution.
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 07-26-2013, 05:56 AM   #1
deepakdeore2004
LQ Newbie
 
Registered: Apr 2010
Posts: 26

Rep: Reputation: 0
Question Bash - sort data which is on one line


How do I sort the data which is on line line.

eg. one line contains below data

web2 web5 web3 web1 web6

I want it to be sorted like

web1 web2 web3 web5 web6

sort command doesnt have the option for this requirement.
 
Old 07-26-2013, 05:59 AM   #2
H_TeXMeX_H
LQ Guru
 
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
Blog Entries: 2

Rep: Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301
Indeed, sort can only sort lines, so you have to arrange them as lines:

Code:
bash-4.2$ echo 'web2 web5 web3 web1 web6' | tr ' ' '\n'
web2
web5
web3
web1
web6
bash-4.2$ echo 'web2 web5 web3 web1 web6' | tr ' ' '\n' | sort
web1
web2
web3
web5
web6
bash-4.2$ echo 'web2 web5 web3 web1 web6' | tr ' ' '\n' | sort | tr '\n' ' '
web1 web2 web3 web5 web6 bash-4.2$
 
3 members found this post helpful.
Old 07-26-2013, 06:20 AM   #3
deepakdeore2004
LQ Newbie
 
Registered: Apr 2010
Posts: 26

Original Poster
Rep: Reputation: 0
Great, I didn't think about this way.
Thx.
 
Old 07-26-2013, 07:28 AM   #4
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,881

Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
With this InFile ...
Code:
now is the time
for all good men
to come to the aid
of their party
... this code ...
Code:
awk '{split($0,w); s="";
  for (i=1;i<=asort(w);i++) s=s w[i] " "; print s}'  \
  $InFile >$OutFile
...produced this OutFile ...
Code:
is now the time 
all for good men 
aid come the to to 
of party their
Note this minor glitch: each line has a trailing blank.

Daniel B. Martin
 
1 members found this post helpful.
Old 07-26-2013, 09:19 AM   #5
reza_zah1991
Member
 
Registered: Mar 2013
Location: Qom , Iran
Distribution: opensuse,lubuntu
Posts: 32

Rep: Reputation: Disabled
awk is huge , heavy

it seems to ride to home with tractor,,

tractor & bicycle do that work for you but tractor have more price

you can do everything awk can with ' cut+sed '
 
Old 07-26-2013, 09:46 AM   #6
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,881

Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
Quote:
Originally Posted by reza_zah1991 View Post
you can do everything awk can with 'cut+sed '
The task presented by OP is to sort words within each line. If you can do this with cut and sed please post the code.

Daniel B. Martin
 
Old 07-26-2013, 09:56 AM   #7
H_TeXMeX_H
LQ Guru
 
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
Blog Entries: 2

Rep: Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301
The awk solution is a good alternative. There are many ways to do it.
 
Old 07-26-2013, 02:32 PM   #8
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,780

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
Quote:
Originally Posted by reza_zah1991 View Post
you can do everything awk can with ' cut+sed '
Quote:
Originally Posted by Alan Perlis
Beware of the Turing tar-pit in which everything is possible but nothing of interest is easy.
I think sorting data in sed would be ridiculously difficult. Sorting is not part of standard awk (the given awk solution requires GNU awk), but if you felt like restricting yourself to basic tools, it would be not be nearly as hard to implement in awk as compared to sed.

@danielbmartin: I think your solution calls asort() once for every word in the line, for efficiency (only relevant if you have really long lines) one might want to move that call before the loop.
 
Old 07-26-2013, 05:18 PM   #9
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,881

Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
Quote:
Originally Posted by ntubski View Post
@danielbmartin: I think your solution calls asort() once for every word in the line, for efficiency (only relevant if you have really long lines) one might want to move that call before the loop.
My earlier post failed to give a deserved attribution. The awk solution was originally posted by firstfire on 02-02-12. I simplified it, only a bit.

http://www.linuxquestions.org/questi...h-line-927306/

If you can improve the efficiency please post your version of the code.

Daniel B. Martin
 
Old 07-26-2013, 06:19 PM   #10
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,780

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
To demonstrate the efficiency difference, I will pass input with a pathologically long line:

Code:
~/tmp$ time yes | head -1000 | tr '\n' ' ' | gawk '{ split($0, w); s = "";
  for (i=1; i <= asort(w); i++) 
     s = (s w[i] " ");
  print s}' >/dev/null
> > > 
real	0m2.593s
user	0m2.568s
sys	0m0.008s
~/tmp$ time yes | head -1000 | tr '\n' ' ' | gawk '{ split($0, w); s = ""; wc = asort(w);
  for (i=1; i <= wc; i++) 
     s = (s w[i] " ");
  print s}' >/dev/null
> > > 
real	0m0.037s
user	0m0.028s
sys	0m0.004s
 
1 members found this post helpful.
Old 07-26-2013, 09:52 PM   #11
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
First time I actually tried to read the manual of gawk, one of the things I always remember is how asort is not implemented in ordinary awk. The new variation of it asorti is even also a newcomer. Still sorting in awk is easy to implement like this:
http://code.google.com/p/lawker/sour.../quicksort.awk
And mergesort fans could have a little more complex solutions: http://code.google.com/p/andorian-bl...merge_sort.awk
 
Old 07-27-2013, 10:10 AM   #12
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,881

Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
Quote:
Originally Posted by ntubski View Post
Code:
gawk '{ split($0, w); s = ""; wc = asort(w);
  for (i=1; i <= wc; i++) 
     s = (s w[i] " ");
  print s}' >/dev/null
Excellent! Thank you!

Daniel B. Martin
 
  


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] Bash Script; Sort files into directory based on data in the file name MTAS Programming 31 10-06-2010 11:47 AM
BASH Sort list by end of line to x position in each line? SilversleevesX Programming 14 08-19-2010 08:30 PM
bash script to sort data by field lothario Linux - Newbie 4 08-26-2009 02:23 AM

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

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