Sorting a flat file based on multiple colums(using character position)


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Sorting a flat file based on multiple colums(using character position)
# 1  
Old 07-24-2006
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  
Old 07-24-2006
Can you post some sample data here.? Thanks.
# 3  
Old 07-24-2006
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  
Old 07-24-2006
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 07:45 AM.. Reason: Remove -n from sort
# 5  
Old 07-24-2006
Try this (assume numbers 19 and 20 starting from column 15):
Code:
sort -k1.15n inputfile


Jean-Pierre.
# 6  
Old 07-24-2006
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  
Old 07-24-2006
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?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Splitting based on occurence of a Character at fixed position

I have a requirement where i need to split a file based on occurence of a character which is present at a fixed position. Description is as below: 1. The file will be more than 1 Lakh records. 2. Each line will be of fixed length of 987 characters. 3. At position 28 in each line either 'C' or... (9 Replies)
Discussion started by: Neelkanth
9 Replies

2. UNIX for Dummies Questions & Answers

Change a character based on its position number

Hi I have a text file that I want to change some of the characters based on their position. My file contain multiple lines and characters should be counted continuously line by line. For example, I want to convert the 150th T to C. What can I do? Here is a portion of my file:... (10 Replies)
Discussion started by: a_bahreini
10 Replies

3. Shell Programming and Scripting

Find position of character in multiple strings in a file

Greetings. I have a file with information like this: AMNDHRKEOEU?AMNDHRKEOEU?AMNDHRKEOEU?AMNDHRKEOEU? AMNDHRKEEU?AMNDHREOEU? AMNDHREU?AHRKEOEU?AMNDHRKEU?AMNDKEOEU? What I need to extract is the position, in every line, of every occurrence of '?' A desired output would be something... (6 Replies)
Discussion started by: Twinklefingers
6 Replies

4. Linux

Linux script to remove a character in a file based on position.

Greetings, We have a requirement where we need to loop in a fixed width file in linux and remove a character based on a position for every record. It would highly appreciate if someone can help to automate this. Appreciate your time and help! Regards (3 Replies)
Discussion started by: mailme0205
3 Replies

5. Shell Programming and Scripting

Find the position of a field/column in a flat file

Hi, Let say I have a file which has around 400 fields. SampleFile ========= PATIENTID|FACILITY|................|TIME_LAST_VISITED_BY_MD|.....|STATUS| How is it possible to find out which field is TIME_LAST_VISITED_BY_MD?fro example by seeing the above structure we can saw FACILITY... (5 Replies)
Discussion started by: machomaddy
5 Replies

6. Shell Programming and Scripting

Read from Multiple character delimited flat file

Hello Guys I have a multiple character delimited flat file "|~|". when I tried to read the data the "|" character also coming Example I/P file 9882590|~|20111207|~|K03501000063005574033961|~|K|~| Command to get the second column I used awk -F"|~|" ' {print $2}' ... (2 Replies)
Discussion started by: Pratik4891
2 Replies

7. Shell Programming and Scripting

Cut multiple data based on character position

How to extract multiple data based on character position. I need to fetch from 7-9 and 22-26 and there is no delimiter for 22-26 since it is part of the column. The file may have more than 1000 character long.I managed to pull any one but not both for example test data 12345 zxc vbnmlk... (1 Reply)
Discussion started by: zooby
1 Replies

8. Shell Programming and Scripting

Sorting the records in the Flat file

Hi all, I am using this command "sort -d -u -k1 IMSTEST.74E -o tmp.txt" to the records in the flat. Can any tell me how to sort the file except first line in the file For ex: i/p First line: DXYZ Second line : jumy third : cmhk fourth : andy Output should... (5 Replies)
Discussion started by: sudhir_barker
5 Replies

9. Shell Programming and Scripting

Convert case on specified position of flat file

Please help Need a script which will do the following : Search on fixed width file , go to position (25,2) which means 25th and 26th position, Find if there are any char in lower case: For example 25,2 can be (9T) or (9w) or (Ww) or (wW)....The two positions can be numeric or alpha...no... (13 Replies)
Discussion started by: ssantoshss
13 Replies

10. Shell Programming and Scripting

How to insert at a particular position in flat file

Hi All, I have a flat file with ~ as de-limiter (e.g: aaa~ba a~caa~0~d~e) What I want is check if the 4th character is 0 and replace it with say 4. So now it becomes : aaa~ba a~caa~4~d~e. I have to do this for the whole file, but the delimiter position remains the same, not the... (10 Replies)
Discussion started by: akdwivedi
10 Replies
Login or Register to Ask a Question