sorting null values


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sorting null values
# 1  
Old 03-18-2010
sorting null values

Hi
I have a file with the values

Code:
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
Code:
abc
kls
lmn
ops
res
set
then the three NULL


Last edited by Franklin52; 03-19-2010 at 06:53 AM.. Reason: Please use code tags!
# 2  
Old 03-18-2010
Hello and welcome to the forums.

I cant be certain, because you didn't use code tags around you sample data, but the following solution assumes that those are empty lines (no spaces or tabs or other non-printing characters):
Code:
sort file | sed '/^$/{H;d;}; ${G;s/\n$//;}'

Regards,
Alister
# 3  
Old 03-19-2010
Thanks Alister !
those are empty lines lets say that empty line contain tabs or space.
but i need those tab/space at the EOF.


Regards,
Vicky
# 4  
Old 03-19-2010
Code:
sort myFile | nawk '!NF{e[++nf]=$0}; NF; END{while(++i in e) print e[i]}'

# 5  
Old 03-19-2010
Thanks
That works perfectly
# 6  
Old 03-19-2010
Code:
sort  file   | sed '/^$/d' | sed -re '$s/(.*)/\1\n\n\n/'

# 7  
Old 03-19-2010
Quote:
Originally Posted by vickyhere
Thanks Alister !
those are empty lines lets say that empty line contain tabs or space.
but i need those tab/space at the EOF.


Regards,
Vicky
Pardon the nitpick, but a line with spaces and tabs is not an empty line nor a null line. I mention it not to be pedantic but because using that wording to describe them is imprecise and may interfere with our abilitiy to deliver a quick and accurate solution. Non-empty blank lines is probably the most accurate term (isblank(3) returns true only for tab and space characters when using the standard C locale). The following solution handles empty lines (lines containing zero bytes) and lines consisting solely of any combination of blank characters.
Code:
sort file | sed '/^[[:blank:]]*$/{H;d;}; ${x;s/^\n//;x;G;}'

Cheers,
Alister

Last edited by alister; 03-19-2010 at 11:23 AM..
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

Handle null values-awk

I am using below code to validate the source file,code working fine but if any column contains null value then below code throwing error actually it should not.how to customize the below code to handle null null values also. When I run the script with below source data getting “date error”, as... (2 Replies)
Discussion started by: srivalli
2 Replies

4. Shell Programming and Scripting

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... (4 Replies)
Discussion started by: viktor1985
4 Replies

5. 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

6. 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

7. UNIX for Dummies Questions & Answers

Check for null values in a column

Hi All, I have a file with 10 columns and get the required data for nine columns properly except 8th. In 8th column i have both NULL and NON NULL values...i.e certain records have values for all the columns including 8th column and certain records have 8th column as NULL.My requisite is,without... (20 Replies)
Discussion started by: ganesh_248
20 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