Thursday, May 2, 2024
11
rated 0 times [  11] [ 0]  / answers: 1 / hits: 19444  / 2 Years ago, tue, march 1, 2022, 8:38:55

I'm interested to find out the line number of the longest line from a file.



For example, if I have a file with the following content:



lalala
tatatata
abracadabra
mu mu mu


how can I write a bash script that will give me an output something like this: 3 -> abracadabra?


More From » command-line

 Answers
0

You don't need a script for doing this. A simple command is enough:





egrep -n "^.{$(wc -L < filename)}$" filename


This will work even when you have two or more lines with the same maximum length.



If you want that the output to be exactly in this form: 3 -> abracadabra, then use:



egrep -n "^.{$(wc -L < filename)}$" filename | sed 's/:/ -> /'


References:




[#28451] Tuesday, March 1, 2022, 2 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
oreera

Total Points: 472
Total Questions: 121
Total Answers: 116

Location: Mayotte
Member since Thu, Dec 17, 2020
3 Years ago
;