sorting

Hii guys,
I need to sort my file and remove duplicates before writing to another file. The first line in the file are column names. I dont want this line to be sorted and should always be the first line in the output.

sort -u file.txt > file1.txt. is the command that i am using because of which the column name is comming last.

sorting by descending order might help. But i dont know which command to use

thanks for your help

Alas, sort doesn't know how to skip lines. You must extract the first line separately, then sort the rest, then add it back later:

{ head -1 file.txt ; tail +2 file.txt | sort -u ; } >file1.txt

The tail +2 extracts the file starting from the 2nd line. You can "group" the output of all the commands contained within curly-braces.

1 Like

There's no need for head or tail:

{
 IFS= read -r line
 printf "%s\n" "$line"
 sort -u
} < "$file" > "$newfile"
1 Like
awk 'NR==1{print;next} {print |"sort -u"}' infile
 $ ruby -e 'a=open("file").read.split("\n"); puts a[0];puts a[1..-1].sort.uniq' > newfile