How can i sort a .txt file without loosing the header information?


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers How can i sort a .txt file without loosing the header information?
# 1  
Old 05-21-2013
How can i sort a .txt file without loosing the header information?

Hi,

I'm trying to sort 2 different .txt tab delimited files with the command line:

Code:
sort -k 1b,1 inputfile > outputfile

But doing that i'm also sorting the header (that ends at the end of my file).
How can i sort a .txt file without sorting the header but conserving the header in the output file?

My file looks like:

Code:
transcript_id	gene_id	length	effective_length	expected_count	TPM	FPKM	IsoPct
comp1000201_c0_seq1	comp1000201_c0	337	183.51	0.00	0.00	0.00	0.00
comp1000297_c0_seq1	comp1000297_c0	612	458.50	0.00	0.00	0.00	0.00
comp100036_c0_seq1	comp100036_c0	333	179.51	1.00	6.72	6.17	100.00
comp10003_c1_seq1	comp10003_c1	328	174.51	0.00	0.00	0.00	0.00
comp100041_c0_seq1	comp100041_c0	338	184.51	0.00	0.00	0.00	0.00

Thanks!

---------- Post updated at 05:06 PM ---------- Previous update was at 04:55 PM ----------

I also tried to use:

Code:
awk 'NR==1; NR > 1 {print $0 | "sort -t\| -n -k 1,1"}' file

and this is what i'm receiving:

Code:
awk: warning: escape sequence `\|' treated as plain `|'
transcript_id	gene_id	length	effective_length	expected_count	TPM	FPKM	IsoPct
sort: option requires an argument -- t
Try `sort --help' for more information.
sh: -n: command not found


Last edited by alisrpp; 05-21-2013 at 07:46 PM..
# 2  
Old 05-21-2013
Code:
awk '{if(NR>1) print | "sort -k1b,1"; else print}' file

# 3  
Old 05-21-2013
Thanks, and how can i do to not print the result but keep it in an output file?
# 4  
Old 05-21-2013
redirect output into a file - call it newfile

Code:
awk '{if(NR>1) print | "sort -k1b,1"; else print}' file > newfile

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Extract information from txt file

Hello! I need help :) I have a file like this: AA BC FG RF TT GH DD FF HH (a few number of rows and three columns) and I want to put the letters of each column in a variable step by step in order to give them as input in another script. So I would like to obtain: for the 1° loop:... (11 Replies)
Discussion started by: edekP
11 Replies

2. Shell Programming and Scripting

Sort and Split file with header and custom name

Hi, I am using SUN SOLARIS (SunOS sun4v sparc SUNW, T5240). I have a huge data file with header and trailer. This file gets used into an ETL process. ETL skips the header record (which is the first record of the file) and loads the rest of the record. The file can be delimited (comma,... (5 Replies)
Discussion started by: Saanvi1
5 Replies

3. UNIX for Dummies Questions & Answers

Sort a las file keep the header as it is

I have several las files with a header and each file start Version and text and before the data starts end up with ~Ascii, then the numbers starts: ------------------------------------------------------------------------- ~Version .....text.... ~Ascii 2 abc 230 1 name 1 abc 400 1... (17 Replies)
Discussion started by: tk2000
17 Replies

4. UNIX for Dummies Questions & Answers

Sort a las file keep the header as it is

I have several las files with a header and each file start Version and text and before the data starts end up with ~Ascii, then the numbers starts: ------------------------------------------------------------------------- Code: ~Version .....text.... ~Ascii 2 abc 230 1 name 1 abc ... (1 Reply)
Discussion started by: tk2000
1 Replies

5. UNIX for Dummies Questions & Answers

How to add a header to a tab delimited .txt file?

Hi, I have a tab delimited document with 18 columns. My file looks like: comp1000201_c0_seq1 comp1000201_c0 337 183.51 0.00 0.00 0.00 0.00 ---NA--- 337 0 0 - comp1000297_c0_seq1 comp1000297_c0 612 458.50 ... (1 Reply)
Discussion started by: alisrpp
1 Replies

6. 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

7. Shell Programming and Scripting

How to read userid and password information from txt file

Hi Experts, I am writing a shell script (for displaying disk space details) which is logging to 15 different servers using following command. ssh userid@servername It is prompting me for password for all 15 servers when I manually run it. However , soon I would like to schedule this script... (4 Replies)
Discussion started by: ajaypatil_am
4 Replies

8. Shell Programming and Scripting

sort a report file having header and footer

I am having report file with header and footer . The details in between header and footer are separated by a pipe charater. I want to sort the file by considering multiple columns in between header and footer. pls help (4 Replies)
Discussion started by: suryanarayana
4 Replies

9. UNIX for Dummies Questions & Answers

Add a header to a sort file instruction

Hello, I have a header which I have to add to a sorted file, however if I use cat header sortedfile > newfile, the operation takes 2 minutes as the sorted file is over 400mb. I have noticed that when I sort the 400mb unsorted file, this only takes 14 seconds to create the output. As... (2 Replies)
Discussion started by: clarcombe
2 Replies

10. UNIX for Dummies Questions & Answers

How to retain the header information of a file

Hi, I am using Bash shell to create some data and these data would be piped out to a file, let say output.txt. This output.txt I would like to add some extra header information such as comments, descriptions and general information on the text. I would like to know how could I maintain... (0 Replies)
Discussion started by: ahjiefreak
0 Replies
Login or Register to Ask a Question