I have a file in the following format
Code:
# comment 1
# comment 2
0 0 BBAAC 16
0 16 AACAD 16
0 32 ACDAC 16
...
I want to sort(1) it by the third column as string, which can be easily done by
Code:
sort --key=3 my_file
However, after sort, the first two comment lines may not be still at the head of the file. Is there a way to tell sort(1) to ignore the comment lines?
I assume there is no such convenient way. Then what I did is to
1) grep the comment lines to my_comments
2) delete the comment lines from my_file
3) sort my_file
4) concatenate my_comments with my_file
Step 1 can be easily done by
Code:
head -n 20 my_file | grep \# > my_comments
assuming there is always no more than 20 comment lines.
Step 2 is the source of headache. There seems no way to truncate file at head. I need to grep or sed my_file to a temporary new file, and sort the temporary file instead in step 3, right? If my_file is large, say 1GB, this will exhibit high performance and storage overhead. Is there in-place way?
Step 4, how to do it quickly without commands like
Code:
cat my_comments temp_file > my_file
?
In one word, is there a way to do in-place sort by taking advantage of knowing that the comment lines are few and only appear at head?
Thank you very much!