Execution problem with sort the file based on contents


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Execution problem with sort the file based on contents
# 1  
Old 10-08-2009
Execution problem with sort the file based on contents

My input file:
>ali
ASSDDGHFHFHJFJHJDSDGSDGSDGSDGSDGSDGSDGDSGDSGSDGDSGSDGSDGDSGSDGGDSG
>zzz
ASdASDASDSADSADDSADJKHJDSADKLJADKLSAJDLJLKJLDASDDSADd
>abu
ASDASDFSAFASFSADFASDASDSADSADSADSADSADSADASDASdSADSADSADA
>aaa
MMMMASDSADSAMDASDSADSADSADSADSADDDDDDDDDDDDDDASDOIIEOUWIOEUQWIOEWQOEUWQIOEUWQIEOWQE

My desired output:
>aaa
MMMMASDSADSAMDASDSADSADSADSADSADDDDDDDDDDDDDDASDOIIEOUWIOEUQWIOEWQOEUWQIOEUWQIEOWQE
>ali
ASSDDGHFHFHJFJHJDSDGSDGSDGSDGSDGSDGSDGDSGDSGSDGDSGSDGSDGDSGSDGGDSG
>abu
ASDASDFSAFASFSADFASDASDSADSADSADSADSADSADASDASdSADSADSADA
>zzz
ASdASDASDSADSADDSADJKHJDSADKLJADKLSAJDLJLKJLDASDDSADd

How can I do to sort (descending order or ascending order) the file based on the contents inside each header?
# 2  
Old 10-08-2009
Hi,... substitute the new line char for a escape char.... after that, substitute the combination escape char + new record char.... after that sort... after that go back to where we start... substitute the escape char for a new line....

That is:
Code:
tr '\n' '#' < INPUT_FILE | awk '{ aux=$0; gsub("#>","\n>",aux); print aux; }' | sort | tr '#' '\n'


Last edited by laurovalente; 10-08-2009 at 02:24 AM..
# 3  
Old 10-08-2009
Hi,

After I trying this code:
Code:
tr '\n' '#' < INPUT_FILE | awk '{ aux=$0; gsub("#>","\n>",aux); print aux; }' | sort | tr '#' '\n'

I get the output like:
>aaa
MMMMASDSADSAMDASDSADSADSADSADSADDDDDDDDDDDDDDASDOIIEOUWIOEUQWIOEWQOEUWQIOEUWQIEOWQE

>abu
ASDASDFSAFASFSADFASDASDSADSADSADSADSADSADASDASdSADSADSADA
>ali
ASSDDGHFHFHJFJHJDSDGSDGSDGSDGSDGSDGSDGDSGDSGSDGDSGSDGSDGDSGSDGGDSG
>zzz
ASdASDASDSADSADDSADJKHJDSADKLJADKLSAJDLJLKJLDASDDSADd

The output result is a bit different with what I'm desired. Do you know what is the main reason?
Thanks a lot Smilie

Quote:
Originally Posted by laurovalente
Hi,... substitute the new line char for a escape char.... after that, substitute the combination escape char + new record char.... after that sort... after that go back to where we start... substitute the escape char for a new line....

That is:
Code:
tr '\n' '#' < INPUT_FILE | awk '{ aux=$0; gsub("#>","\n>",aux); print aux; }' | sort | tr '#' '\n'



---------- Post updated at 09:54 PM ---------- Previous update was at 01:14 AM ----------

Nobody can help me solve my problem?
Too complicated?! Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to change contents of field based on condition in same file

In the awk below I am trying to copy the entire contents of $6 there may be multiple values seperated by a ;, to $8, if $8 is . (lines 1 and 3 are examples). If that condition $8 is not . (line2 is an example) then that line is skipped and printed as is. The awk does execute but prints the output... (3 Replies)
Discussion started by: cmccabe
3 Replies

2. Shell Programming and Scripting

Remove or rename based on contents of file

I am trying to use the two files shown below to either remove or rename contents in one of those files. If in file1.txt $5 matches $5 of file2.txt and the value in $1 of file1.txt is not "No Match" then that value is substituted for all values in $5 and $1 of file2.txt. If however in $1 ... (5 Replies)
Discussion started by: cmccabe
5 Replies

3. Shell Programming and Scripting

File comparison based on contents

Hi I have 2 files 1.del ---- 1,2,3,4,5 1,2,3,4,4 1,1,1,1,2 2.del ---- 1,2,3,4,5 1, 1,2,3,4,4 1,1,1,1,2 I need to compare the above two files in unix, as in the output should only tell the difference in contents as I should get only the line 1 ( from 2.del) , rest all lines are... (4 Replies)
Discussion started by: Ethen561
4 Replies

4. UNIX for Dummies Questions & Answers

find lines in another file based on contents in a second file

Hello, I have a file with tab delimited columns like: File1 A 2 C R F 4 D Q C 9 A B ...... I want to grep out the lines in a second file, File2, corresponding to each line in File1 Can I do this: while read a b c d do grep '$a\t$b\t$c\t$d' File2 >>... (2 Replies)
Discussion started by: Gussifinknottle
2 Replies

5. Shell Programming and Scripting

Help with sort data based on descending order problem

Input file 9.99331e-13 8.98451e-65 9.98418e-34 7.98319e-08 365592 111669 74942.9 0 Desired output 365592 111669 74942.9 7.98319e-08 1.99331e-13 6.98418e-34 (2 Replies)
Discussion started by: perl_beginner
2 Replies

6. UNIX for Dummies Questions & Answers

count values based on contents of another file

Hello, I have two files as shown below: test1 678 679 689 690 710 test2 1 678 654 800 676 791 689 900 I want to get a count of lines from test2 whose columns bound the values in test1 I tried running the code below; however am getting wrong results. (3 Replies)
Discussion started by: Gussifinknottle
3 Replies

7. Shell Programming and Scripting

move contents from one file to another based on line number or content

I want a script that will move everything beyond a certain line number or beyond a certain content word into another file. For example, if file A has this: first line second line third line forth line fifth line sixth line I want to run a script that will move everything beyond the third... (4 Replies)
Discussion started by: robp2175
4 Replies

8. Shell Programming and Scripting

Remove lines based on contents of another file

So, this issue is driving me nuts! I was hoping to get a lending hand here... I have 2 files: file1.txt contains: this is example1 this is example2 this is example3 this is example4 this is example5 file2.txt contains: example3 example5 Basically, I need a script or command to... (4 Replies)
Discussion started by: bashshadow1979
4 Replies

9. Shell Programming and Scripting

sh script that reads/writes based upon contents of a file

Hi everyone, Ive got a quick question about the feasibility and any suggestions for a shell script. I can use sh or ksh, doesnt matter. Basically, Ive got an output file from a db2 command that looks like so: SCHEMA NAME CARD LEAF ELEAF LVLS ISIZE NDEL KEYS F4 F5 ... (3 Replies)
Discussion started by: rdudejr
3 Replies

10. UNIX for Advanced & Expert Users

file extension based software execution

Hi Experts, I am working in HP-UX 11.0 workstaion. How can i make a file to be executed with the corresponding software to be invoked or executed. as in Windows. ie., if a file index.html has to be double clicked to invoke the Netscape Navigator. What i have to do to achieve this... (2 Replies)
Discussion started by: anent
2 Replies
Login or Register to Ask a Question