开发者

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
    An开发者_如何学JAVAother 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


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜