Unix: specify the file name and line number of each line that exceeds 80 characters of each file in the folder

The title summarizes it. I work on Unix, the 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 " so that all lines are longer than 80 characters, but I cannot pull out line numbers or file names. I also tried to use cat because it allows line numbers.

The idea is to get a similar result:

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 awesome.

+8
scripting unix shell
source share
2 answers

Almost there! This will give you a grep line output, useful in editors like Vim to navigate the output file:

 awk 'length > 80 {print FILENAME "(" FNR "): " $0}' *.cpp 

Or specify the format you requested:

 awk 'length > 80 {print FILENAME " line " FNR "\n\t" $0}' *.cpp 

FILENAME and FNR (e.g. NR , but for this particular file) are special variables in awk .

Or you could use grep yourself, of course:

 grep -n '^.\{80\}' *.cpp 
+11
source share
 awk 'length > 80 { print FILENAME ":" NR ": " $0 }' *.cpp 
0
source share

All Articles