Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unix: Print file name and line number of each line that exceeds 80 characters of each file in a folder

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.

like image 833
carlinyuen Avatar asked Feb 03 '23 20:02

carlinyuen


1 Answers

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
like image 148
Johnsyweb Avatar answered Feb 13 '23 19:02

Johnsyweb