The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Sorting based on Multiple columns dharmesht Shell Programming and Scripting 15 05-13-2009 10:44 AM
How to insert at a particular position in flat file akdwivedi Shell Programming and Scripting 10 08-07-2007 08:47 PM
merge multiple lines from flat file hnhegde Shell Programming and Scripting 4 12-05-2006 07:13 PM
sorting a file with multiple columns outtacontrol UNIX for Dummies Questions & Answers 3 07-26-2006 01:51 PM
Finding character position in file dhananjaysk Shell Programming and Scripting 5 03-23-2006 11:49 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 07-24-2006
cucubird cucubird is offline
Registered User
  
 

Join Date: Jul 2006
Posts: 4
Sorting a flat file based on multiple colums(using character position)

Hi,

I have an urgent task here. I am required to sort a flat file based on multiple columns which are based on the character position in that line. I am restricted to use the character position instead of the space and sort +1 +2 etc to do the sorting.

I understand that there is a previous post which is similar to my problem but for that case, the sorting can be done using sort +1 +2 etc.

For my case, I am unable to do it as each column may be made up of some words separated by a space etc and as such spacing is not a correct delimiter to define the column.

I need help regarding this matter, either using unix shell scripting or awk. Thanks a lot
  #2 (permalink)  
Old 07-24-2006
tayyabq8's Avatar
tayyabq8 tayyabq8 is offline Forum Advisor  
Moderator
  
 

Join Date: Nov 2004
Location: Bahrain
Posts: 578
Can you post some sample data here.? Thanks.
  #3 (permalink)  
Old 07-24-2006
cucubird cucubird is offline
Registered User
  
 

Join Date: Jul 2006
Posts: 4
Faroe Island 20 island
japan 19 airline

The above is the sample data. As you can see, the intended first colum is actually Faroe Island instead of just Faroe. As such, It is necessary to use the char position to distinguish the columns.

Appreciate if you could provide me with some advise on how to do the sorting...For example I wanna sort according to the first column and last column

PS: Assume the 20 and 19, island and airline are all at the same char position
  #4 (permalink)  
Old 07-24-2006
vish_indian vish_indian is offline
Registered User
  
 

Join Date: Jun 2006
Location: Delhi, India
Posts: 92
Quote:
PS: Assume the 20 and 19, island and airline are all at the same char position

cat test
Faroe Island 20 island
japan 19 test
japan 19 airline
Alpha Zulu 21 island
Alpha 121 island

Try this:
sed '
s/ \([0-9]\)/,\1/
s/\([0-9]\) /\1,/
' test | sort -k1,3


Alpha,121,island
Alpha Zulu,21,island
Faroe Island,20,island
japan,19,airline
japan,19,test


if you want to change "," back to space

sed '
s/ \([0-9]\)/,\1/
s/\([0-9]\) /\1,/
' test | sort -k1,3 | sed 's/,/ /g'


Alpha 121 island
Alpha Zulu 21 island
Faroe Island 20 island
japan 19 airline
japan 19 test

Last edited by vish_indian; 07-24-2006 at 06:45 AM.. Reason: Remove -n from sort
  #5 (permalink)  
Old 07-24-2006
aigles's Avatar
aigles aigles is offline Forum Advisor  
Registered User
  
 

Join Date: Apr 2004
Location: Bordeaux, France
Posts: 1,414
Try this (assume numbers 19 and 20 starting from column 15):
Code:
sort -k1.15n inputfile

Jean-Pierre.
  #6 (permalink)  
Old 07-24-2006
cucubird cucubird is offline
Registered User
  
 

Join Date: Jul 2006
Posts: 4
But how u sort using 2 columns? If the first cloum compared is a tie, then compare the other column?

In general, how to sort(order by) multiple columns?
  #7 (permalink)  
Old 07-24-2006
vish_indian vish_indian is offline
Registered User
  
 

Join Date: Jun 2006
Location: Delhi, India
Posts: 92
Quote:
In general, how to sort(order by) multiple columns?
sort -k 1,3 is doing just that. eg, for input
Faroe Island 20 island
japan 19 test
japan 19 airline
Alpha Zulu 21 island
Alpha 121 island

Japan occurs twice in first column, so while sorting, 3rd column is considered and the output is:

Alpha 121 island
Alpha Zulu 21 island
Faroe Island 20 island
japan 19 airline
japan 19 test

isn't that what you wanted?
Sponsored Links
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 02:40 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0