1

Want a linux/ unix script that will tell me max characters in any line in a directory tree.

So I can specify a root folder. It walks down it and processes files with some mask (like *.java) - a find command ... and then keeps the max characters in a line in a var and prints that.

I saw this question but i wanted the maximum only without having to copy to spread sheet or other processing.

Dont want specific characters want to consider all characters.

Community
  • 1
  • 1
tgkprog
  • 4,493
  • 4
  • 41
  • 70

2 Answers2

1

This return the line with the most number of characters ( to count them pipe to wc -c ) in all *java files in the current directory:

perl -e 'while(<>){$l=length;  $l>$m && do {$c=$_; $m=$l}  } print $c' *.java

It's not exactly what you ask, but is a good starting point.

Thomas8
  • 1,117
  • 1
  • 11
  • 22
1

Use that find command:

find -type f -iname "*.java" -exec awk 'length($0)>a{a=length($0)} END{print FILENAME":"a}' {} \;

Explanation:

  • -type f fond only files
  • -iname "*.java" only file ending with .java
  • -exec awk execute awk for each file
    • length($0)>a{a=length($0)} if the length of the line is greater than a set a to that length.
    • END{print FILENAME":"a}: at the end, print the filename and the length
chaos
  • 8,162
  • 3
  • 33
  • 40