1

I need to make a script, I need to sort out 5 files names which have the most lines. This is the part that I don't know how to do. Maybe someone could help me?

ls work | wc -l 

my script, but it somehow still doesn't print names of those 5 files that has most lines

mkdir work 
cp /etc/*.conf . 
 ls work | perl -e 'print sort { length($b) <=> length($a) } <>' | head -5
tar cf work.tar.gz work
Jason Aller
  • 3,541
  • 28
  • 38
  • 38
Gabriell
  • 11
  • 2
  • my whole script wit all the things I need to do now looks like this #!/bin/bash mkdir work cp /etc/*.conf . ls work | wc -l tar cf work.tar.gz work – Gabriell Dec 10 '20 at 01:52
  • Does this answer your question? [Sort a text file by line length including spaces](https://stackoverflow.com/questions/5917576/sort-a-text-file-by-line-length-including-spaces) – ggorlen Dec 10 '20 at 01:53
  • 2
    please update the question with your latest code changes; not everyone will read through the comments trying to piece together the various bits-n-pieces – markp-fuso Dec 10 '20 at 01:54
  • Many answers in the linked thread. Here's one `ls work | perl -e 'print sort { length($b) <=> length($a) } <>' | head -5`. Or are you looking for the filenames containing the longest line in their content? Hard to tell. – ggorlen Dec 10 '20 at 01:55
  • Yes, I need to put out 5 filenames containing the longest line in their content and since Im new to this i dont really know how to write everything correctly, i tried to put in your recommended line but i still get error – Gabriell Dec 10 '20 at 02:12
  • I'm still not clear what you're trying to do. You have a bunch of files, and you want to find the 5 top files sorted by longest line they contain? Can you add three example input files to your question and show the expected output ordering? – Benjamin W. Dec 10 '20 at 02:28
  • Yes, i have a bunch of files in catalog work and i need to sort 5 filenames containing the longest line from that catalog. filenames in that catalog looks like this : adduser.conf, abb.conf, pam.conf and etc. its just their names in there. Since I need to write this script for school I dont have example how the output should look like. There should just be those filenames – Gabriell Dec 10 '20 at 02:44

1 Answers1

0

You need to perform the wc command on each line of output from ls as opposed to all of the output and so use xargs. You can then sort the output with sort -n and remove the total from the output, printing the top 5 entries with tail -6/head -5

ls work | xargs wc -l | sort -n | tail -6 | head -5
Raman Sailopal
  • 12,320
  • 2
  • 11
  • 18