sort

Miner, Jonathan W (CSC) (US SSA) jonathan.w.miner at baesystems.com
Mon Jun 19 12:50:46 UTC 2006


 

	-----Original Message----- 
	From: redhat-list-bounces at redhat.com on behalf of Aimin Yan 
	Sent: Sat 06/17/2006 02:23 PM 
	To: redhat-list at redhat.com 
	Cc: 
	Subject: sort
	
	

	i have a file, I want to sort this file based on the 2nd column, and start
	from 1st row.how can
	I do use sort command to do in redhat linux.
	for example, this is file I want to sort
	
	
	name  age  income
	trorry   34    344
	aimin   36    345
	shihe   23    3667
	
	Aimin

	-----------------------------

	Lots of possible answers to this, here is one that preserves the header line, and the rest of the file contents:

	f={filename}

	(head -1 $f; awk 'BEGIN {getline} {print $0}' $f | sort -k2 -n) > $f.new

	mv $f.new $f

	 

	This depends on having a single title line in the field, a better method, in my opinion, is to use the pound sign (#) to mark comments.  The code then becomes:

	f={filename}

	(grep '^#' $f; grep -v '^#' $f | sort -k2 -n) > $f.new

	mv $f.new $f

	 

	Enjoy



More information about the redhat-list mailing list