LinuxQuestions.org
Review your favorite Linux distribution.
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 05-17-2012, 03:04 PM   #1
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Ubuntu
Posts: 1,061

Rep: Reputation: 284Reputation: 284Reputation: 284
awk - sort characters within each line


Some months ago I asked for an awk which would sort all words within each line.
firstfire provided this excellent solution using asort.

Code:
awk -v IGNORECASE=1 '{gsub(/[[:punct:]]/, ""); split($0, w); s=""; for(i=1; i<=asort(w); i++) s=s w[i] " "; print s }' infile.txt
Now I want to do something similar but have been unable to modify it to do the job. I have a file with one character string per line. The characters in this example file are numerics but the question applies to alphanumerics.

Have:
Code:
0100
0101
0102
0103
0104
0110
0111
0112
0113
0114
0120
Want:
Code:
0001
0011
0012
0013
0014
0011
0111
0112
0113
0114
0012
Daniel B. Martin
 
Old 05-17-2012, 03:15 PM   #2
Kustom42
Senior Member
 
Registered: Mar 2012
Distribution: Red Hat
Posts: 1,565

Rep: Reputation: 410Reputation: 410Reputation: 410Reputation: 410Reputation: 410
the sort command will do that for you by itself.
 
Old 05-17-2012, 03:17 PM   #3
Kustom42
Senior Member
 
Registered: Mar 2012
Distribution: Red Hat
Posts: 1,565

Rep: Reputation: 410Reputation: 410Reputation: 410Reputation: 410Reputation: 410
Wait, after looking at your output there is no real sorting method there, how are you trying to sort these?
 
Old 05-17-2012, 03:17 PM   #4
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,458

Rep: Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941
I suggest something similar but simpler:
Code:
awk '{ split($0,array,""); for ( i = 1; i <= asort(array); i++ ) printf array[i]; printf "\n" }' file
The sorting process depends on your locale definitions and specifically from LC_COLLATE. You can use IGNORECASE as well.

Last edited by colucix; 05-17-2012 at 03:25 PM.
 
1 members found this post helpful.
Old 05-17-2012, 03:40 PM   #5
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Ubuntu
Posts: 1,061

Original Poster
Rep: Reputation: 284Reputation: 284Reputation: 284
Quote:
Originally Posted by colucix View Post
Code:
awk '{ split($0,array,""); for ( i = 1; i <= asort(array); i++ ) printf array[i]; printf "\n" }' file
Ooh, that is sweet! Thank you!

Daniel B. Martin
 
Old 05-18-2012, 09:39 AM   #6
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,428

Rep: Reputation: 1876Reputation: 1876Reputation: 1876Reputation: 1876Reputation: 1876Reputation: 1876Reputation: 1876Reputation: 1876Reputation: 1876Reputation: 1876Reputation: 1876
And a bit of ruby:
Code:
ruby -ane 'BEGIN{$; = ""};puts $F[0..-2].sort.join' file

or

ruby -pe '$_ = $_.chomp.split("").sort.join + "\n"' file
 
Old 11-07-2012, 07:20 PM   #7
jachaja
LQ Newbie
 
Registered: Nov 2012
Posts: 2

Rep: Reputation: Disabled
Hi all,

I have a file with a lot of records similar to this:

023456AB2543999210709
022656AB2543999210409
024556AB2543999200709
024556AB2543999200909

I want to sort this file by using two parts of each record, for example: positions 3 to 4 like one field ascending and
positions 16 to 19 like other field descending.

The result:
022656AB2543999210409
023456AB2543999210709
024556AB2543999200909
024556AB2543999200709

It is posible?

Thanks.

Last edited by jachaja; 11-07-2012 at 07:22 PM.
 
Old 11-07-2012, 08:52 PM   #8
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Ubuntu
Posts: 1,061

Original Poster
Rep: Reputation: 284Reputation: 284Reputation: 284
Quote:
Originally Posted by jachaja View Post
Hi all,

I have a file with a lot of records similar to this:

023456AB2543999210709
022656AB2543999210409
024556AB2543999200709
024556AB2543999200909

I want to sort this file by using two parts of each record, for example: positions 3 to 4 like one field ascending and
positions 16 to 19 like other field descending.

The result:
022656AB2543999210409
023456AB2543999210709
024556AB2543999200909
024556AB2543999200709

It is posible?

Thanks.
Try this ...
Code:
echo "Sort on columns 3-4 (ascending) and columns 16-19 (descending)."
sort -k1.3,1.4 -k1.16,1.19r $InFile > $OutFile
Daniel B. Martin
 
  


Reply

Tags
awk, sorta


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
[SOLVED] [bash - sed - awk] Match line with x characters and add string TigerClaw Linux - Newbie 4 02-28-2012 12:22 AM
[SOLVED] awk - sort words within each line danielbmartin Programming 3 02-03-2012 11:17 AM
[SOLVED] Need help in replacing set of characters in a specific line using sed or awk bbachu Programming 15 01-03-2011 01:01 AM
How to sort by line size (number of characters in a line) fast_rizwaan Linux - General 8 01-08-2010 05:53 PM


All times are GMT -5. The time now is 03:40 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration