Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Awk command needed to sort and group 1

Status
Not open for further replies.

codesearcher

Technical User
Feb 23, 2009
7
US
Hi
I need a unix script to sort & group the below record based on the last value of the line
eg: In below file, i should sort & group all 1's and next 2's

P0001|Jim|1001|1
P0002||1002|abcdne|2
P0003|Jack|1001|test|csx|1
P0003|Till|1003|dlf|2
P0010|Tusk|1011|xyz|unites|shift|1

I am struck after using this statement awk -F'|' '{ printf $0}'
Any help is welcome
 
The difficulty is that the sort field isn't always in the same field number, so you can't just use the sort command.

Alternatively you can use awk to create a new field on the beginning of the line, sort it by the first field, and then use sed to strip it off again:

Code:
awk -F'|' '{print $NF FS $0}' inputfile | sort -t'|' -n -k 1,1 | sed 's/[0-9]*\|//'

Annihilannic.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top