Title sums it up. I'm working in Unix, korn shell. And I'm trying to print the file name and line number of each line that exceeds 80 characters of each file in the current folder.
I can use "awk 'length > 80' *.cpp" to give all lines longer than 80 characters, but I can't seem to pull out the line numbers or file names. I tried to use cat as well, because it allows for line numbers.
The idea is to get output similar to this:
test.cpp line 36
std::cout << ... line that is over 80 characters
test2.cpp line 40
Another line that is over 80 characters
Any help would be amazing.
Almost there! This will give you a grep
-style output, useful in editors like Vim to be able to navigate the output:
awk 'length > 80 {print FILENAME "(" FNR "): " $0}' *.cpp
Or to give the format you asked for:
awk 'length > 80 {print FILENAME " line " FNR "\n\t" $0}' *.cpp
FILENAME
and FNR
(like NR
, but for that particular file) are special variables in awk
.
Or you could use grep
itself, of course:
grep -n '^.\{80\}' *.cpp
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With