Sort file based on number of delimeters in line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Sort file based on number of delimeters in line
# 1  
Old 03-25-2016
Sort file based on number of delimeters in line

Hi,

Need to sort file based on the number of delimeters in the lines.

Code:
cat testfile
/home/oracle/testdb
/home
/home/oracle/testdb/newdb
/home/oracle

Here delimeter is "/"

expected Output:

Code:
/home/oracle/testdb/newdb
/home/oracle/testdb
/home/oracle
/home

TIA
# 2  
Old 03-25-2016
Try
Code:
 awk -F\/ '{print NF, $0}' file | sort -nr | awk 'sub ($1 FS, _)'
/home/oracle/testdb/newdb
/home/oracle/testdb
/home/oracle
/home

# 3  
Old 03-25-2016
Hello Sumanthsv,

Could you please try following and let me know if this helps.
Code:
awk '{A=$0;count=gsub(/\//,X,A);array[count]=array[count]?array[count] ORS $0:$0;} END{for(j=NR;j>=1;j--){if(array[j]){print array[j]}}}'  Input_file

Output will be as follows.
Code:
/home/oracle/testdb/newdb
/home/oracle/testdb
/home/oracle
/home

EDIT: Adding a non-one liner form for same solution.
Code:
awk '{
        A=$0;
        count=gsub(/\//,X,A);
        array[count]=array[count]?array[count] ORS $0:$0;
     }
        END{
                for(j=NR;j>=1;j--){
                                        if(array[j]){
                                                        print array[j]
                                                    }
           }
     }
    '   Input_file

Thanks,
R. Singh

Last edited by RavinderSingh13; 03-25-2016 at 07:57 AM.. Reason: Adding a non-one liner solution for same too.
# 4  
Old 03-25-2016
If all that you need is to have the pathnames of the files in a directory to sort before the pathname of the directory itself, all you need is:
Code:
sort -r testfile

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Split a line into multiple lines based on delimeters

Hi, I need help to split any lines that contain ; or , input.txtAc020 Not a good chemical process AC030 many has failed, 3 still maintained AC040 Putative; epithelial cells AC050 Predicted binding activity AC060 rodC Putative; upregulated in 48;h biofilm vs planktonic The output... (8 Replies)
Discussion started by: redse171
8 Replies

2. Shell Programming and Scripting

How to split a file based on pattern line number?

Hi i have requirement like below M <form_name> sdasadasdMklkM D ...... D ..... M form_name> sdasadasdMklkM D ...... D ..... D ...... D ..... M form_name> sdasadasdMklkM D ...... M form_name> sdasadasdMklkM i want split file based on line number by finding... (10 Replies)
Discussion started by: bhaskar v
10 Replies

3. Shell Programming and Scripting

Sort the file based on number of occurences

I have a file (input) I want to sort the file based on the number of times a pattern in the first column occurs for example grapes occurs 4 times in combination with other patterns so i want it to be first like shown in the output file. then apple ocuurs thrice so it occupies second position and so... (7 Replies)
Discussion started by: anurupa777
7 Replies

4. Shell Programming and Scripting

Splitting a file based on line number

Hi I have a file with over a million lines (rows) and I want to split everything from 500,000 to a million into another file (to make the file smaller). Is there a simple command for this? Thank you Phil (4 Replies)
Discussion started by: phil_heath
4 Replies

5. Shell Programming and Scripting

[Solved] How to separate one line to mutiple line based on certain number of characters?

hi Gurus, I need separate a file which is one huge line to multiple lines based on certain number of charactors. for example: abcdefghi high abaddffdd I want to separate the line to multiple lines for every 4 charactors. the result should be abcd efgh i hi gh a badd ffdd Thanks in... (5 Replies)
Discussion started by: ken6503
5 Replies

6. UNIX for Dummies Questions & Answers

Sort Files based on the number(s) on the file name

Experts I have a list of files in the directory mysample1 mysample2 mysample3 mysample4 mysample5 mysample6 mysample7 mysample8 mysample9 mysample10 mysample11 mysample12 mysample13 mysample14 mysample15 (4 Replies)
Discussion started by: dsedi
4 Replies

7. Shell Programming and Scripting

join based on line number when one file is missing lines

I have a file that contains 87 lines, each with a set of coordinates (x & y). This file looks like: 1 200.3 -0.3 2 201.7 -0.32 ... 87 200.2 -0.314 I have another file which contains data that was taken at certain of these 87 positions. i.e.: 37 125 42 175 86 142 where the first... (1 Reply)
Discussion started by: jackiev
1 Replies

8. Shell Programming and Scripting

Extract string from multiple file based on line count number

Hi, I search all forum, but I can not find solutions of my problem :( I have multiple files (5000 files), inside there is this data : FILE 1: 1195.921 -898.995 0.750312E-02-0.497526E-02 0.195382E-05 0.609417E-05 -2021.287 1305.479-0.819754E-02 0.107572E-01 0.313018E-05 0.885066E-05 ... (15 Replies)
Discussion started by: guns
15 Replies

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

10. Shell Programming and Scripting

Split File Based on Line Number Pattern

Hello all. Sorry, I know this question is similar to many others, but I just can seem to put together exactly what I need. My file is tab delimitted and contains approximately 1 million rows. I would like to send lines 1,4,& 7 to a file. Lines 2, 5, & 8 to a second file. Lines 3, 6, & 9 to... (11 Replies)
Discussion started by: shankster
11 Replies
Login or Register to Ask a Question