How to sort lines by substring


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to sort lines by substring
# 1  
Old 04-29-2008
How to sort lines by substring

Dear all

There is a file which contains the following formatted files. I need to sort it by substring(strings after dot) in order to process efficiently.
Please give me any idea how to sort it.


Sample file:

Code:
1AAABBBCCC.20080401
1AAABBBCCC.20080402
2AAABBBCCC.20080401
2AAABBBCCC.20080402
3AAABBBCCC.20080401
3AAABBBCCC.20080402
4AAABBBCCC.20080401
4AAABBBCCC.20080402
5AAABBBCCC.20080401
5AAABBBCCC.20080402
6AAABBBCCC.20080401
6AAABBBCCC.20080402
7AAABBBCCC.20080401
7AAABBBCCC.20080402
8AAABBBCCC.20080401
8AAABBBCCC.20080402
9AAABBBCCC.20080401
9AAABBBCCC.20080402
AAAABBBCCC.20080401
AAAABBBCCC.20080402
BAAABBBCCC.20080401
BAAABBBCCC.20080402
CAAABBBCCC.20080401
CAAABBBCCC.20080402
DAAABBBCCC.20080401
DAAABBBCCC.20080402
EAAABBBCCC.20080401
EAAABBBCCC.20080402
FAAABBBCCC.20080401
FAAABBBCCC.20080402

Result:
Code:
1AAABBBCCC.20080401
2AAABBBCCC.20080401
3AAABBBCCC.20080401
4AAABBBCCC.20080401
5AAABBBCCC.20080401
6AAABBBCCC.20080401
7AAABBBCCC.20080401
8AAABBBCCC.20080401
9AAABBBCCC.20080401
AAAABBBCCC.20080401
BAAABBBCCC.20080401
CAAABBBCCC.20080401
DAAABBBCCC.20080401
EAAABBBCCC.20080401
FAAABBBCCC.20080401
1AAABBBCCC.20080402
2AAABBBCCC.20080402
3AAABBBCCC.20080402
4AAABBBCCC.20080402
5AAABBBCCC.20080402
6AAABBBCCC.20080402
7AAABBBCCC.20080402
8AAABBBCCC.20080402
9AAABBBCCC.20080402
AAAABBBCCC.20080402
BAAABBBCCC.20080402
CAAABBBCCC.20080402
DAAABBBCCC.20080402
EAAABBBCCC.20080402
FAAABBBCCC.20080402

Thank you very much
# 2  
Old 04-29-2008
Code:
sort -t. -k2,2 filename

# 3  
Old 04-29-2008
if you need the first column sorted as well, adjust krishmaths' response:

Code:
sort -t. -k2,2 -k1,1 filename

# 4  
Old 04-29-2008
Thank you very much
Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Remove lines matching a substring in a specific column

Dear group, I have following input text file: Brit 2016 11 18 12 00 10 1.485,00 EUR Brit 2016 11 18 12 00 10 142,64 EUR Brit 2016 11 18 12 00 10 19,80 EUR Brit 2016 11 18 12 00 10 545,00 EUR Brit 2016 11 18 12 00 10 6.450,00 EUR... (3 Replies)
Discussion started by: gfhsd
3 Replies

2. UNIX for Dummies Questions & Answers

awk - (URGENT!) Print lines sort and move lines if match found

URGENT HELP IS NEEDED!! I am looking to move matching lines (01 - 07) from File1 and 77 tab the matching string from File2, to File3.txt. I am almost done but - Currently, script is not printing lines to File3.txt in order. - Also the matching lines are not moving out of File1.txt ... (1 Reply)
Discussion started by: High-T
1 Replies

3. Shell Programming and Scripting

Using sed, awk or perl to remove substring of all lines except the first

Greetings All, I would like to find all occurences of a pattern and delete a substring from the all matching lines EXCEPT the first. For example: 1234::group:user1,user2,user3,blah1,blah2,blah3 2222::othergroup:user9,user8 4444::othergroup2:user3,blah,blah,user1 1234::group3:user5,user1 ... (11 Replies)
Discussion started by: jacksolm
11 Replies

4. Shell Programming and Scripting

Count lines containing substring

I have 2 files, and I want to count how many lines contain matching words. Example: file1 a_+b a_+b_+c file2 ab a_+b a_+bc I want to get 1, as the the first line of file1 is a substring of the first line of file2. While the second line isn't. I suspect using sdiff, but not sure how to... (3 Replies)
Discussion started by: Viernes
3 Replies

5. Shell Programming and Scripting

grep from 3 lines and sort

Pseudo name=hdiskpower54 Symmetrix ID=000190101757 Logical device ID=0601 state=alive; policy=SymmOpt; priority=0; queued-IOs=0 ============================================================================== ---------------- Host --------------- - Stor - -- I/O Path - -- Stats --- ### HW... (7 Replies)
Discussion started by: Daniel Gate
7 Replies

6. Shell Programming and Scripting

awk to loop lines and print substring

The input data: mbeanName: com.sap.default:name=tc~bl~deploy_controller,j2eeType=SAP_J2EEServicePerNode,SAP_J2EEClusterNode=3620850,SAP_J2EECluster=XXX Get attribute Properties: {undepl_parallelism_strategy=normal, deployment_forbidden=off, locking_retries=50, suppress_ds_warnings=on,... (5 Replies)
Discussion started by: ux4me
5 Replies

7. Shell Programming and Scripting

PERL : Sort substring occurrences in array of strings

Hi, My developer is on vacation and I am not sure if there is something which is easier for this. I have an array of strings. Each string in the array has "%" characters in it. I have to get the string(s) which have the least number of "%" in them. I know how I can get occurrences : ... (7 Replies)
Discussion started by: sinpeak
7 Replies

8. UNIX for Dummies Questions & Answers

How to sort lines by strings between ()?

Hello,everyone. I am learning some Info commands.I put all commands and their explanations in a file. This is a part of it: ESC PgUp (scroll-other-window-backward)Scroll the other window backward ESC Right (forward-word) Move forward a word ESC r (move-to-window-line) ESC TAB... (3 Replies)
Discussion started by: vic005
3 Replies

9. Shell Programming and Scripting

How to extract a substring and append to subsequent lines

Hi all,I am really new to Shell Scripting.I have the following doubt. Let us assume the one sample file which contains the below data HEADERCARMENTRACIE1555090414 PERIOD0905090501090531 DETAIL0645693037023073836 GROUNDAV 090501 01 GROUNDAV 090502 01 TRIP 0091282542 0905084101... (5 Replies)
Discussion started by: jaligamasriniva
5 Replies

10. Shell Programming and Scripting

Finding duplicates from positioned substring across lines

I have million's of records each containing exactly 50 characters and have to check the uniqueness of 4 character substring of 50 character (postion known prior) and report if any duplicates are found. Eg. data... AAAA00000000000000XXXX0000 0000000000... upto50 chars... (2 Replies)
Discussion started by: gapprasath
2 Replies
Login or Register to Ask a Question