awk: sort lines by count of a character or string in a line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk: sort lines by count of a character or string in a line
# 8  
Old 09-03-2010
Quote:
Originally Posted by Michael Stora
No. I need the deepest directories first. I need to sort be how many times the character '/' appears in the line (decending).

Mike
If you go down the reverse sorted list you will notice that it is not possible to remove a parent before the child.... Radoulov's -depth option is an even better solution which will also not sort the way you intended, but IMO should work for your purpose..

---------- Post updated at 12:03 ---------- Previous update was at 11:54 ----------

Quote:
Originally Posted by Michael Stora
PS. Still interested in the general AWK solution . . .
One way:
Code:
awk '{p=$0;print gsub(/\//,""),p}' | sort -k1,1nr | sed s/'[0-9]* //'

# 9  
Old 09-03-2010
Quote:
Originally Posted by Michael Stora
[...]

PS. Still interested in the general AWK solution . . .
awk is not good for sorting.

You can:

1. Use awk + some other tool (sort, cut):

Code:
awk -F/ '{
  print NF, $0 
  }' infile |
    sort -rn |
      cut -d\  -f2-

2. Use a more appropriate tool for the task (Perl):

Code:
perl -e'
  print map $_->[0], 
    sort { $b->[1] <=> $a->[1] } 
      map [ $_, tr/\/// ], <>
    ' infile

# 10  
Old 09-03-2010
Scrutinizer, I figured out my mistake before reading your second reply. Thanks again.

Mike
# 11  
Old 09-03-2010
Corrected:

Code:
awk -F/ 'END {
  n = asorti(d, t)
  for (i = n; i; i--)
    print d[t[i]]
  }
{
  k = sprintf("%15s", NF)
  d[k] = k in d ? d[k] RS $0 : $0
  }
  '  infile


Last edited by radoulov; 09-03-2010 at 07:33 AM..
# 12  
Old 09-03-2010
Code:
find ... |awk '{
  a[maxline*gsub(/\//,"&")+NR] = $0
}
END {
  for (i=maxline*maxpath;i>0;--i)
    if (i in a) print a[i]
}
' maxline=20 maxpath=5

This User Gave Thanks to binlib For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Count specific character of a file in each line and delete this character in a specific position

I will appreciate if you help me here in this script in Solaris Enviroment. Scenario: i have 2 files : 1) /tmp/TRANSACTIONS_DAILY_20180730.txt: 201807300000000004 201807300000000005 201807300000000006 201807300000000007 201807300000000008 2)... (10 Replies)
Discussion started by: teokon90
10 Replies

2. UNIX for Dummies Questions & Answers

Getting the character count of the last line

I need the character count of the last line of each file in a directory, and not the total. Now I have been doing this but unfortunately, -exec doesn't support pipes: find sent/ -type f -exec tail -1|wc -c {} \; If I try this: find sent/ -type f -exec tail -1 {} \; | wc -c It will give... (6 Replies)
Discussion started by: MIA651
6 Replies

3. Shell Programming and Scripting

awk - count character count of fields

Hello All, I got a requirement when I was working with a file. Say the file has unloads of data from a table in the form 1|121|asda|434|thesi|2012|05|24| 1|343|unit|09|best|2012|11|5| I was put into a scenario where I need the field count in all the lines in that file. It was simply... (6 Replies)
Discussion started by: PikK45
6 Replies

4. Shell Programming and Scripting

Character count of each line

Hi, I have a file with more than 1000 lines. Most of the lines have 16 characters. I want to find out lines that have less than 14 characters (usually 12 or 13). wc -l gives me the line count and wc -c gives me the total characters in a file. I could not get the total characters for each line.... (1 Reply)
Discussion started by: bobbygsk
1 Replies

5. Shell Programming and Scripting

awk new line issue, saying string can't contain new line character

Hi , I am doing some enhancements in an existing shell script. There it used the awk command in a function as below : float_expr() { IFS=" " command eval 'awk " BEGIN { result = $* print result exit(result == 0) }"' } It calls the function float_expr to evaluate two values ,... (1 Reply)
Discussion started by: mady135
1 Replies

6. Shell Programming and Scripting

sed or awk delete character in the lines before and after the matching line

Sample file: This is line one, this is another line, this is the PRIMARY INDEX line l ; This is another line The command should find the line with “PRIMARY INDEX” and remove the last character from the line preceding it (in this case , comma) and remove the first character from the line... (5 Replies)
Discussion started by: KC_Rules
5 Replies

7. Shell Programming and Scripting

Count character in one line

Please check the attachment for the example. Purpose: count how many "|" character in one line and also display the line number. expect result: Line 1 : there are 473 "|" characters Line 2 : there are 473 "|" characters I have tried to use awk to count it, it's ok when the statistic... (8 Replies)
Discussion started by: ambious
8 Replies

8. Shell Programming and Scripting

awk find a string, print the line 2 lines below it

I am parsing a nagios config, searching for a string, and then printing the line 2 lines later (the "members" string). Here's the data: define hostgroup{ hostgroup_name chat-dev alias chat-dev members thisisahostname } define hostgroup{ ... (1 Reply)
Discussion started by: mglenney
1 Replies

9. Shell Programming and Scripting

awk to print lines based on string match on another line and condition

Hi folks, I have a text file that I need to parse, and I cant figure it out. The source is a report breaking down softwares from various companies with some basic info about them (see source snippet below). Ultimately what I want is an excel sheet with only Adobe and Microsoft software name and... (5 Replies)
Discussion started by: rowie718
5 Replies

10. UNIX for Advanced & Expert Users

How to count no of occurences of a character in a string in UNIX

i have a string like echo "a|b|c" . i want to count the | symbols in this string . how to do this .plz tell the command (11 Replies)
Discussion started by: kamesh83
11 Replies
Login or Register to Ask a Question