![]() |
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| 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 |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
||||
|
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 |
|
||||
|
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 |
|
||||
|
Quote:
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 |
|
||||
|
Quote:
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 | ||
|
|
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|