How to sort the 6th field of tab delimited files?


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers How to sort the 6th field of tab delimited files?
# 1  
Old 02-25-2014
Wrench How to sort the 6th field of tab delimited files?

Here's a sample of the data:

Code:
NAME   BIRTHDAY    SEX    LOCATION    AGE    ID
Jim       05/11/1986  M       Japan          27      86
Rei       08/25/1990  F        Korea          24     33
Jane     02/24/1985  F        India           29     78

I've been trying to sort files using the 6th field of the file using this command:

Code:
sort -t '\t' -k 6 -n test.txt > Sort.txt

how ever the result is not what i've been expecting. Smilie
I want to sort the IDs in ascending order Smilie

THanks!

Last edited by maihani; 02-25-2014 at 02:18 AM.. Reason: code tags
# 2  
Old 02-25-2014
Try this one :-
Code:
# sort -k 6 -n sort.unl 
NAME BIRTHDAY SEX LOCATION AGE ID
Rei 08/25/1990 F Korea 24 33
Jane 02/24/1985 F India 29 78
Jim 05/11/1986 M Japan 27 86


Last edited by Scrutinizer; 02-25-2014 at 01:34 AM.. Reason: code tags
# 3  
Old 02-25-2014
@maihani:
To use TAB as a field separator with sort, an actual TAB character needs to be entered using CTRL-V <TAB>
Code:
sort -t '    ' -nk6 file

or
Code:
sort -t "$(printf "\t")" -nk6 file

or if you are using bash or ksh93:
Code:
sort -t $'\t' -nk6 file

--
If there are no space characters in the fields, you could also leave it out, like sayami00 suggested..
# 4  
Old 02-25-2014
I don't know why but none of these are working for me Smilie

Thanks anyway...
# 5  
Old 02-25-2014
  • What isn't working for you?
  • What result were you expecting?
  • Are the fields in your file separated by actual TAB characters (I cannot tell from your sample)?
  • Could you attach the sample otherwise?
# 6  
Old 02-25-2014
With Attachment

Attached is my sample document.
I need to sort using the 6th field in ascending orders.

Thanks!

Last edited by maihani; 02-25-2014 at 02:34 AM..
# 7  
Old 02-25-2014
Since column 6 is not the last column, try:
Code:
sort -t "$(printf "\t")" -nk6,6 file

What isn't working for you? What result were you expecting?

That document appears to be already sorted on the six column.
Code:
awk -F'\t' '{print $6}' ../Downloads/TEST_INT_test.txt 
CO_ENT_NBR
972
972
1521
1521
2863
2946
2946
2946
2946
10879
17950
17950
17950


When I jumble the lines first before sorting, the sort appears to work well

Last edited by Scrutinizer; 02-25-2014 at 05:50 AM..
This User Gave Thanks to Scrutinizer For This Post:
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Match tab-delimited files based on key

I thought I had this figured out but was wrong so am humbly asking for help. The task is to add an additional column to FILE 1 based on records in FILE 2. The key is in COLUMN 1 for FILE 1 and in COLUMN 1 OR COLUMN 2 for FILE 2. I want to add the third column from FILE 2 to the beginning of... (8 Replies)
Discussion started by: andmal
8 Replies

2. UNIX for Beginners Questions & Answers

UNIX - 2 tab delimited files, conditional column extraction

Please know that I am very new to unix and trying to learn 'on the job'. I'm only manipulating large tab-delimited files (millions of rows), but I'm stuck and don't know how to proceed with the following. Hoping for some friendly advice :) I have 2 tab-delimited files - with differing column &... (10 Replies)
Discussion started by: GTed
10 Replies

3. Shell Programming and Scripting

Display combination of 4 field uniqe record and along with concatenate 5th and 6th field.

Table ACN|NAME|CITY|CTY|NO1|NO2 115|AKKK|ASH|IND|10|15 115|AKKK|ASH|IND|20|20 115|AKKK|ASH|IND|30|35 115|AKKK|ASH|IND|30|35 112|ABC|FL|USA|15|15 112|ABC|FL|USA|25|20 112|ABC|FL|USA|25|45 i have written shell script using cut command and awk programming getting error correct it and add... (5 Replies)
Discussion started by: udhal
5 Replies

4. UNIX for Dummies Questions & Answers

Sort tab delimited file according to which rows have missing values

Hello! I have a tab delimited file with values in three columns. Some values occur in all three columns, other values are present in only one or two columns. I would like to sort the file so that rows with no missing values come first, rows with one missing values come next, and rows with two... (9 Replies)
Discussion started by: MBarrett1213
9 Replies

5. Shell Programming and Scripting

How to remove alphabets/special characters/space in the 5th field of a tab delimited file?

Thank you for 4 looking this post. We have a tab delimited file where we are facing problem in a lot of funny character. I have tried using awk but failed that is not working. In the 5th field ID which is supposed to be a integer only of that file, we are getting corrupted data as below. I... (12 Replies)
Discussion started by: Srithar
12 Replies

6. Shell Programming and Scripting

Insert a header record (tab delimited) in multiple files

Hi Forum. I'm struggling to find a solution for the following issue. I have multiple files a1.txt, a2.txt, a3.txt, etc. and I would like to insert a tab-delimited header record at the beginning of each of the files. This is my code so far but it's not working as expected. for i in... (2 Replies)
Discussion started by: pchang
2 Replies

7. UNIX for Dummies Questions & Answers

Insert Field into a tab-delimited file

Hello, I have about 100 files in a directory with fields which are tab delimited. I would like to append the file name as the first field and it has to be done as many times as the total lines in the file. For example, myFile1.txt has the following data: 1 x y z 2 a b ... (5 Replies)
Discussion started by: Gussifinknottle
5 Replies

8. Shell Programming and Scripting

insert a field into a tab delimited file

Hello, Can someone help me to do this with awk or sed? I have a file with multiple lines, each line has many fields separated with a tab. I would like to add one more field holding 'na' in between the first and second fields. old file looks like, 1, field1 field2 field3 ... 2, field1... (7 Replies)
Discussion started by: ssshen
7 Replies

9. Shell Programming and Scripting

Working with Tab-Delimited files

I have a tab-Delimited file: Eg: 'test' file contains: a<tab>b<tab>c<tab>.... Based on certain condition, I wanna increase the number of lines of this file.How do I do that Eg: If some value in the database is 1 then one line in 'test' file is fine.. If some value in the database is 2... (1 Reply)
Discussion started by: shiroh_1982
1 Replies

10. UNIX for Dummies Questions & Answers

How do I specify tab as field separator for sort?

I'm trying to use sort on a file with tab-delimited fields. I can't figure out how to tell sort to use the tab character as the field separator. I'm trying this on both an HP Unix system and on OS X (using bash on both). Things I've tried: sort -t\t sort -t"\t" sort -t\"\t\" I've tried... (8 Replies)
Discussion started by: SSteve
8 Replies
Login or Register to Ask a Question