How to use sort with null values?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to use sort with null values?
# 1  
Old 05-31-2012
Bug How to use sort with null values?

Hello everyone

I am doing a join command. Obviously, before I need two files sorted first. ( Both files have headers and have about 2 million lines each one )

The problem is, one of the files has null values in the key to sort (which is the first filed ). For example I have the original file1.csv

Code:
imsi;hlr;subIndex;customer
700309879;2;66;tmobile
300123456;1;55;americamovil
;3;77;telefonica

I need the output to be ( first the header, then null values , then numeric values ) or ( header, numeric values, null values):

Code:
imsi;hlr;subIndex;customer
;3;77;telefonica
300123456;1;55;americamovil
700309879;2;66;tmobile


I have tried many combinations of sort flags using -b -t -d -n -g -k1 , but without success.

I am using a fedora 16

Appreciate your comments.

Best Regards
# 2  
Old 05-31-2012
Since it looks like you want this sorted as a zero rather than a blank...
why not replace all lines that begin with
Quote:
;
with
Quote:
0;
??
This User Gave Thanks to joeyg For This Post:
# 3  
Old 05-31-2012
Code:
total_rec=`wc -l $file`
#subtract 1 for header record 
total_rec=`expr $total_rec - 1`
#put the header record first in output file
head -1 $file > somefile
#sort based on first filed except header and append to output file
tail -$total_rec $file|awk '{print $0 | "sort -t\; -k 1,1"} ' >>somefile


Last edited by donadarsh; 05-31-2012 at 02:51 PM..
This User Gave Thanks to donadarsh For This Post:
# 4  
Old 05-31-2012
Try this...
Code:
sort -t\; -k1,1n -r file

This User Gave Thanks to shamrock For This Post:
# 5  
Old 05-31-2012
Thank you. I think it will work it out, but I need something faster. my files are about 1.5 GB. I am using process substitution with 2 sort and 1 join.

---------- Post updated at 09:48 PM ---------- Previous update was at 09:46 PM ----------

Thank you very much shamrock !! This is what I was looking for !!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Printing null values in awk

Hi, I have a csv file with given details abc.txt 123,ra,point,,there 232,ba,points,home,pheer I want to get those values and store them in different variables: Code: while read line do echo $line |awk -F"," '{print $1" "$2" "$3" "$4" "$5"}'|read dbt_acct val_dt crncy AMT... (11 Replies)
Discussion started by: rahulsk
11 Replies

2. Shell Programming and Scripting

Check null values column

hi, I had a small question.I had a file from which i need to extract data. I have written the below script to check if the file exists and if it exists extract requierd columns from the file. IFILE=/home/home01/Report_1.csv OFILE=/home/home01/name.csv.out1 if #Checks if file exists... (1 Reply)
Discussion started by: Vivekit82
1 Replies

3. Shell Programming and Scripting

Check for null values in columns

Hi , I have below data with fixed with of 52 bytes having three columns value data. 01930 MA GLOUCESTER 02033 02025 COHASSET 01960 MA ... (3 Replies)
Discussion started by: sonu_pal
3 Replies

4. Shell Programming and Scripting

Averaging each row with null values

Hi all, I want to compute for the average of a file with null values (NaN) for each row. any help on how to do it. the sample file looks like this. 1.4 1.2 1.5 NaN 1.6 1.3 1.1 NaN 1.3 NaN 2.4 1.3 1.5 NaN 1.5 NaN 1.2 NaN 1.4 NaN I need to do a row-wise averaging such that it will sum only... (14 Replies)
Discussion started by: ida1215
14 Replies

5. Shell Programming and Scripting

sorting null values

Hi I have a file with the values abc res set kls lmn ops i want to sort this file with the null values at the bottom of the file OUTPUT should look like this abc kls lmn ops (6 Replies)
Discussion started by: vickyhere
6 Replies

6. Shell Programming and Scripting

Sort command which ignores NULL

Dear all, I have a file as below 100||kjhkjhkjhkjhk 200|TR|jvsjfhskfhskfhsdh 300|BH|kjlkjljlkj 600||dadsadasdada ||ffsdfsf |YU|popopop 900||mlml when I apply the below sort command, results obtained displayed below- cut -f1,2 -d"|" test.txt|sort -u 100| 200|TR 300|BH 600| 900|... (9 Replies)
Discussion started by: sureshg_sampat
9 Replies

7. UNIX for Advanced & Expert Users

How to Compare Null values??

Hi, Can someone help me comparing Null values. Scenario is as follows: I have a variable which "cache_prd" which can have either some integer or nothing(Null) if it is integer I have to again do some comparision but these comparisons give me this error:( "line 32: [: 95: unary operator... (3 Replies)
Discussion started by: Yagami
3 Replies

8. Shell Programming and Scripting

identifying null values in a file

I have a huge file with 20 fileds in each record and each field is seperated by "|". If i want to get all the reocrds that have 18th or for that matter any filed as null how can i do it? Please let me know (3 Replies)
Discussion started by: dsravan
3 Replies

9. Shell Programming and Scripting

comparing the null values in the unix

hi all, iam new to this forum.i have to submit the script EOD.so please help me. my requirement is to compare two null values..iam trying to compare two null values :One null value output of the storedprocedure and another iam giving spaces in script. it is giving the error... (11 Replies)
Discussion started by: bbc17484
11 Replies

10. Shell Programming and Scripting

handling null values in files

Hi , I have a script where i will remove header and trailer record and ftp to another server. I'm using the code: latestfilename=`ls filename_* | tail -1` echo "Latest filename = $latestfilename" sed '1d;$d' $latestfilename > a.ftpedfile I have an issue if input data is having null... (1 Reply)
Discussion started by: ammu
1 Replies
Login or Register to Ask a Question