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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Sorting file except 1st line

Status
Not open for further replies.

fabien

Technical User
Sep 25, 2001
299
AU
Hi!

Is there a way to use sort -u to sort a file and remove all duplicate lines but the sorting should only apply after the first line (i.e, the first line will always remain the first line whatever sorting is done below).

Thanks,
 
Try this :

head -1 input_file > output_file
tail +2 input_file | sort -u >> output_file


Jean Pierre.
 
ex -s <file_to_modify> <<EOF
2,\$!sort -r
w
q
EOF

# or as

ex -s <file_to_modify> <<EOF^J2,\$!sort -u^Jw^Jq^JEOF

# or as

echo &quot;2,\$!sort -r^Jw^Jq&quot; | ex -s <file_to_modify>

Cheers,
ND [smile]

bigoldbulldog@hotmail.com
 
A variation on aigles solution (without the output_file):

head -1 input_file; tail +2 input_file | sort -u

or put this in a script (called, say, nsort)

head -1 $1; tail +2 $1 | sort -u

then run

nsort input_file
 
Or ..
[tt]
( head -1 file1
tail +2 file1 | sort -u ) > file2[/tt]
 
stefanwagner, uniq is not needed when the -u option of sort is used.

Hope This Help, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top