Sort and uniq lines of a file while keeping a header line


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Sort and uniq lines of a file while keeping a header line
# 1  
Old 05-11-2009
Sort and uniq lines of a file while keeping a header line

So, I have a file that has some duplicate lines. The file has a header line that I would like to keep at the top.

I could do this by extracting the header from the file, 'sort -u' the remaining lines, and recombine them. But they are quite big, so if there is a way to do it with a single command, that would be great.

Anyone any ideas? Thanks in advance.

Here's an example file

Code:
MAKEBITS 1.0 574331 /home/woodd/workspace2/ibis_3d/scripts/pdblist2fps.py
a139_371_frag1 1 2 3 4 5 6 10 11
a139_371_frag2 2 4 5 158 159 160 161 162
a139_371_frag3 2 6 159 160 161 258 259 260
a139_371_frag4 1 2 3 4 5 6 10 11
a139_371_frag5 1 2 3 4 5 6 10 11
a139_371_frag6 1 2 3 4 5 6 10 11
a139_IMD_frag1 1 3 4 6 57 58 59 89
a139_371_frag1 1 2 3 4 5 6 10 11
a139_371_frag2 2 4 5 158 159 160 161 162
a139_371_frag3 2 6 159 160 161 258 259 260
a139_371_frag4 1 2 3 4 5 6 10 11
a139_371_frag5 1 2 3 4 5 6 10 11
a139_371_frag6 1 2 3 4 5 6 10 11
a139_IMD_frag1 1 3 4 6 57 58 59 89

# 2  
Old 05-11-2009
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Find header in a text file and prepend it to all lines until another header is found

I've been struggling with this one for quite a while and cannot seem to find a solution for this find/replace scenario. Perhaps I'm getting rusty. I have a file that contains a number of metrics (exactly 3 fields per line) from a few appliances that are collected in parallel. To identify the... (3 Replies)
Discussion started by: verdepollo
3 Replies

2. Shell Programming and Scripting

Need to sort text keeping first line always first

I have a file is created from standard output. I have put a leading space to force the first line to collate low vis a vis the rest of the lines. If I pass the entire file to the Linux sort, it ignores the leading space and the first line appears in somewhere in the list. If I add lots of... (15 Replies)
Discussion started by: lsatenstein
15 Replies

3. Shell Programming and Scripting

How do a distinct from a file using sort uniq in bash?

I have an output file .dat. From this file i have to do a distinct of the ID using the sort uniq command in bash script. How can i do it? i found : sort -u ${FILEOUT_DAT} but i don't think is my solution because the id isn't specified.. is there other solution? (7 Replies)
Discussion started by: punticci
7 Replies

4. Shell Programming and Scripting

Remove last few characters in a file but keeping Header and trailer intact

Hi All, I am trying write a simple command using AWK and SED to this but without any success. Here is what I am using: head -1 test1.txt>test2.txt|sed '1d;$d' test1.txt|awk '{print substr($0,0,(length($0)-2))}' >>test2.txt|tail -1 test1.txt>>test2.txt Input: Header 1234567 abcdefgh... (2 Replies)
Discussion started by: nvuradi
2 Replies

5. UNIX for Dummies Questions & Answers

Sort a tab file with header.

How to sort a tab delimited file first on col1 and then on col2. Also I need to keep the header intact. file.txt val1 val2 val3 val4 a b c d m n o p e f g h i j k l ... (3 Replies)
Discussion started by: mary271
3 Replies

6. UNIX for Advanced & Expert Users

$cat file|sort|uniq

I have a file with list of redundant server names and want only unique ones of those. I have used following command but still redudant ones are listing $cat file|sort|uniq where could be the problem. Thanks, Srinivas (3 Replies)
Discussion started by: srinivas Juluri
3 Replies

7. Shell Programming and Scripting

unix sort according to a header line

Hi, I have a file with a header line, followed by some contents. How can I sort the file according to header lines? eg. /* abcd_005*/ a bc /* abcd_001*/ d e /* abcd_002*/ x y desired output: /*abcd_001*/ (0 Replies)
Discussion started by: neil.0412
0 Replies

8. UNIX for Dummies Questions & Answers

find uniq lines in file, using the first field of line

Hello all, new to unix and have just found the forum. I think I will be here quite often, and hope that in time i will be able to provide soem help, role on not being a newbie anymore :) I have a question which iI am hoping someone could help me with. If i have a file with lines in in thus... (8 Replies)
Discussion started by: grom
8 Replies

9. UNIX for Dummies Questions & Answers

duplicated lines not recognized by sort and uniq

Hello all, I've got a strange behaviour of sort and uniq commands: they do not recognise apparently duplicated lines in a file (already sorted). The lines are identical by eye, but they must differ in smth, because when they are put in two files, those have slightly different size. What can make... (8 Replies)
Discussion started by: roussine
8 Replies

10. Shell Programming and Scripting

How to sort standard input without first line < Header >

Do somebody have idea How to sort standard input without first line which in my case it's header Example: Cnt|VT |STAT|Date |Time |From |Alert Message |Instance | 125| | | 260308 |160026 |ZAMUAT2|ifpollq... (8 Replies)
Discussion started by: pp56825
8 Replies
Login or Register to Ask a Question