UNix one-liner to sort a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting UNix one-liner to sort a file
# 1  
Old 07-13-2010
UNix one-liner to sort a file

Hi everyone,

I have a txt file as:

Code:
>001.b1
GCTAGTGCTAGCTAGCTAGCATCGATCGAT
>002.b1
CAGTCAGTCGTAGTGCTAGCTGATGCTCGT
>003.b1
CGATCGTAGTCGTATCGATGCTGACGTAGG
>002.g1
ATGCTGATCGACTAGCTAGTCGT
>015.b1
CGATCTAGTAGTGCTAGTCGTTT
>001.g1
ATGCTGATCGACTAGCTAGTCGT
>003.g1
CGATGCTAGTCGATGCTGACGGG

I am trying to sort the above file according to the header (starting with '>') for every record. I am using something like this:

Code:
sort -n file.txt

But it is sorting the file like dis:

Code:
>001.b1
>001.g1
>002.b1
>002.g1
>003.b1
>003.g1
>015.b1
ATGCTGATCGACTAGCTAGTCGT
ATGCTGATCGACTAGCTAGTCGT
CAGTCAGTCGTAGTGCTAGCTGATGCTCGT
CGATCGTAGTCGTATCGATGCTGACGTAGG
CGATCTAGTAGTGCTAGTCGTTT
CGATGCTAGTCGATGCTGACGGG
GCTAGTGCTAGCTAGCTAGCATCGATCGAT

Any suggestions??
# 2  
Old 07-13-2010
This will probably fall apart at some point, but:

Code:
$ paste - - < file.txt | sort | xargs -n1
>001.b1
GCTAGTGCTAGCTAGCTAGCATCGATCGAT
>001.g1
ATGCTGATCGACTAGCTAGTCGT
>002.b1
CAGTCAGTCGTAGTGCTAGCTGATGCTCGT
>002.g1
ATGCTGATCGACTAGCTAGTCGT
>003.b1
CGATCGTAGTCGTATCGATGCTGACGTAGG
>003.g1
CGATGCTAGTCGATGCTGACGGG
>015.b1
CGATCTAGTAGTGCTAGTCGTTT

# 3  
Old 07-13-2010
Thanks for your reply!

However my text files are huge data files, which I need to sort.
This works perfectly for small test data files.
So any other suggestions, what I can use ?
Smilie
# 4  
Old 07-13-2010
Which of those 3 commands does not work with large files and how does it manifest itself?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to Modify a file content in UNIX and sort for only required fields ?

I have the below contents in a file after making the below curl call curl ... | grep -E "state|Rno" | paste -sd',\n' | grep "Disconnected" > test "state" : "Disconnected",, "Rno" : "5554f1d2" "state" : "Disconnected",, "Rno" : "10587563" "state" : "Disconnected",, "Rno" :... (2 Replies)
Discussion started by: Vaibhav H
2 Replies

2. Shell Programming and Scripting

Sort help: How to sort collected 'file list' by date stamp :

Hi Experts, I have a filelist collected from another server , now want to sort the output using date/time stamp filed. - Filed 6, 7,8 are showing the date/time/stamp. Here is the input: #---------------------------------------------------------------------- -rw------- 1 root ... (3 Replies)
Discussion started by: rveri
3 Replies

3. UNIX for Dummies Questions & Answers

sort a unix file by 3rd column

Hi, Can anybody tell me how to sort a unix file by 3rd column and not by ltr? Please help Thanks in advance (2 Replies)
Discussion started by: vinnyvirk
2 Replies

4. Shell Programming and Scripting

Unix Scripting : Sort a Portion of a File and not the complete file

Need to sort a portion of a file in a Alphabetical Order. Example : The user adam is not sorted and the user should get sorted. I don't want the complete file to get sorted. Currently All_users.txt contains the following lines. ############## # ARS USERS ############## mike, Mike... (6 Replies)
Discussion started by: evrurs
6 Replies

5. UNIX for Dummies Questions & Answers

Unable to sort file name in Unix

Hi, I'm unable to sort the files with the required names on Unix. Details have been enclosed:- Below are the fle name inside a folder, now i need to sort them in a way that they are group with the seconds occurance in the file name (E.g 01954,07947,06794) 01.01954.MXRS1.rcv... (3 Replies)
Discussion started by: kkkk_ssss
3 Replies

6. Shell Programming and Scripting

Search & Replace regex Perl one liner to AWK one liner

Thanks for giving your time and effort to answer questions and helping newbies like me understand awk. I have a huge file, millions of lines, so perl takes quite a bit of time, I'd like to convert these perl one liners to awk. Basically I'd like all lines with ISA sandwiched between... (9 Replies)
Discussion started by: verge
9 Replies

7. UNIX for Advanced & Expert Users

Script to sort the files and append the extension .sort to the sorted version of the file

Hello all - I am to this forum and fairly new in learning unix and finding some difficulty in preparing a small shell script. I am trying to make script to sort all the files given by user as input (either the exact full name of the file or say the files matching the criteria like all files... (3 Replies)
Discussion started by: pankaj80
3 Replies

8. Shell Programming and Scripting

creating a csv file from this 1 liner?

I'm trying to create a csv file by running awk and sed on a number of xml files in a directory; I'm using this below: hostname; grep "BuildDate" /dir/ABCD/configuration/*/*.xml | awk -F"/" '{ print $5 }' > /tmp/tempfile.txt; grep "BuildDate" /dir/ABCD/configuration/*/*.xml | awk -F\" '{ print $2... (2 Replies)
Discussion started by: rich@ardz
2 Replies

9. Shell Programming and Scripting

Need a script or one-liner to purge lines from a file.

i all. This one sounds so simple, but I can't get it to work. I need to delete lines with certain keywords from a file. I have a file called defaultRules, with keywords: IPSEC_AH IKE_UDP IPMP_TEST_IFACE2 Then, I have another file called rules.txt with some rules: ... (10 Replies)
Discussion started by: BRH
10 Replies

10. Shell Programming and Scripting

File size limitation of unix sort command.

hi , iam trying to sort millions of records which is delimited and i cant able to use sort command more than 60 million..if i try to do so i got an message stating that "File size limit exceeded",Is there any file size limit for using sort command.. How can i solve this problem. thanks ... (7 Replies)
Discussion started by: cskumar
7 Replies
Login or Register to Ask a Question