sort by keeping the headings intact?

Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers sort by keeping the headings intact?
# 1  
Old 08-08-2011
sort by keeping the headings intact?

Hi all,

I have a file with 3 columns separated by space. Each column has a heading. I want to sort according to the values in the 2nd column (ascending order).

Name rank direction
goory 0.05 --+
laby 0.0006 ---
namy 0.31 -+-


Output should be
Name rank direction
laby 0.0006 ---
goory 0.05 --+
namy 0.31 -+-

Can somebody help me with this?

Last edited by pludi; 08-08-2011 at 09:49 AM..
# 2  
Old 08-08-2011
sort -nk2 file

This User Gave Thanks to bartus11 For This Post:
# 3  
Old 08-09-2011
Originally Posted by bartus11
sort -nk2 file

Thanks bartus!! What is the difference between sort -nk2,2 and your code?
# 4  
Old 08-09-2011
From the man pages:
-k, --key=POS1[,POS2] start a key at POS1, end it at POS2 (origin 1)

Pos2 is when you want give it a range. You can skip pos2 if your key is only col2 like in this case.

But to answer your question, sort -nk2 is equivalent to sort -nk 2,2
This User Gave Thanks to dude2cool For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help with awk command on report with headings

Hi all, I have a report that looks like this: ------------------------------------------------- -- AOC - XXXXXXX -- ------------------------------------------------- Thread Last Sequence Received Last Sequence Applied Difference ----------... (12 Replies)
Discussion started by: cougartrace
12 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

Remove duplicates by keeping the order intact

Hello friends, I have a file with duplicate lines. I could eliminate duplicate lines by running sort <file> |uniq >uniq_file and it works fine BUT it changes the order of the entries as it we did "sort". I need to remove duplicates and also need to keep the order/sequence of entries. I... (1 Reply)
Discussion started by: magnus29
1 Replies

4. Shell Programming and Scripting

Remove the headings

HI I am executing faloowing commands. mr batch_1 > my_temp.txt ; mr batch_2 >>my_temp.txt; mr batch_3 >> my_temp.txt; mr batch_4 >> my_temp.txt; and the out put file is as this cat my_temp.txt Machine Name Max Load Current Load Factor O/S Status... (3 Replies)
Discussion started by: ptappeta
3 Replies

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

6. Shell Programming and Scripting

Keeping the number intact

Currently I have the following to separate the numeric values. However the decimal point get separated. ls -lrt *smp*.cmd | awk '{print $NF}' | sed 's/^.*\///' | sed 's/\(*\)/ & /g' As an example on the files n02-z30-dsr65-terr0.50-dc0.05-4x3smp.cmd... (8 Replies)
Discussion started by: kristinu
8 Replies

7. Shell Programming and Scripting

Help with file editing while keeping file format intact

Hi, I am having a file which is fix length and comma seperated. And I want to replace values for one column. I am reading file line by line in variable $LINE and then replacing the string. Problem is after changing value and writing new file temp5.txt, formating of original file is getting... (8 Replies)
Discussion started by: Mruda
8 Replies

8. UNIX for Dummies Questions & Answers

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... (1 Reply)
Discussion started by: Digby
1 Replies

9. AIX

split a filename and print to 2 different headings

I need help to split a filename 'a0crk_user:A0-B0123$#%test' into a0crk_user and A0-B0123 and print the output under 2 different columns namely User and Status. for eg. the output should like below: User Status ---- ------ a0crk_user A0-B0123 (3 Replies)
Discussion started by: mbak
3 Replies

10. UNIX for Dummies Questions & Answers

ls -l column headings

I'm trying to see if there's a way to see column headings when I type the ls -l command. I know what some of the fields mean for example in the following listing: total 136 drwxr-xr-x 2 root root 4096 Jun 5 15:16 bin drwxr-xr-x 3 root root 4096 Jul 9 15:25 boot drwxr-xr-x 9... (6 Replies)
Discussion started by: thoughts
6 Replies
Login or Register to Ask a Question