RE: sort

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



 

	-----Original Message----- 
	From: redhat-list-bounces@xxxxxxxxxx on behalf of Aimin Yan 
	Sent: Sat 06/17/2006 02:23 PM 
	To: redhat-list@xxxxxxxxxx 
	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

-- 
redhat-list mailing list
unsubscribe mailto:redhat-list-request@xxxxxxxxxx?subject=unsubscribe
https://www.redhat.com/mailman/listinfo/redhat-list

[Index of Archives]     [CentOS]     [Kernel Development]     [PAM]     [Fedora Users]     [Red Hat Development]     [Big List of Linux Books]     [Linux Admin]     [Gimp]     [Asterisk PBX]     [Yosemite News]     [Red Hat Crash Utility]


  Powered by Linux