Help required in displaying lines exceeding 79 chars along with their line numbers ??

Hi folks,

I am looking for a solution to display those lines in any file that contains 80 or more characters along with their corresponding line number in the file.

The below script will print the lines with their corresponding line numbers...
sed = Sample.cpp | sed 'N;s/\n/\t/; s/*\(.\{6,\}\)\n/\1/' | sed -n '/^
.\{80\}/p'

but i've found that the sed command for numbering has affected the final result......the new result calculates the line numbers as characters too.....which i dont require......

Does any one have any alternate idea to get the expected result ???? Kindly help......

perl -ne '$l=length()-1; print "$l $_" if ($l > 79)' Sample.cpp

For source code you should probably also expand tabs. (man expand)

Check if this helps..

cat -n <filename>| sed -n '/^.\{80\}/p'

You need to subtract the length of the line number from the 80 if you do that; or use a regular expression which ignores the line number part.

cat -n file | egrep '	.{80}'

That's a literal tab before the dot. (In some shells you need to type ctrl-v tab to get a literal tab into the command line.)

But then you might as well

egrep -n '.{80}' file

Hi folks,

Thanks for the replies...:slight_smile:

I tried the mentioned methods..but its partially working....but again some interesting results. I am using vi to edit the code...and i entered "ii" 4 times by adding tab space in between such that the last "ii" will be place after 79..like given below...

(well the space between "ii"'s is bit large say 8 tab space, the unix forum text editor is not allowing me to demonstrate that.....)

ii ii ii ii

If i use the following script
egrep -n '.{80}' VerCsm.cpp

it does not print the exceeded line....ii

but the interesting fact is that..if i type

iiiiiii...........

continuous till 80 or more....with no tab space in between the above script will work......dont know why ???? any idea ?????..:confused:....for time being i assume that the cases i face will be the second one....and will continue to use the mentioned scripts.......thnks..

awk ' length >= 80 { print $0, NR }' filename

Like mentioned above, you will need to expand tabs. A tab character is a single character but uses the width of up to 8 characters. You can calculate the actual width of each tab yourself, but it's probably easier if you pipe the file through expand

expand Sample.cpp | egrep -n '.{80}'

Hi folks,

Thanks for the reply...sadly to say the issue is still there.

The script is unable to calculate the the TAB space in between the words..such that even if it exceeds col 79....it does'nt count...

But the script can calculate spaces given by "space bar"....

1----------------------------------------------79 #LINE COLOUMN 79
ii[TAB][TAB][TAB]ii[TAB][TAB][TAB]ii[TAB][TAB][TAB]ii
ii[SPACE][SPACE][SPACE]ii[SPACE]ii[SPACE][SPACE][SPACE]ii

The script doesn't work with the first one, but will work perfectly with the second........

Kindly help.....:confused:

Neither of those example lines have a display width of over 79 characters. Anyway, again: a tab is a single character and the script counts characters. If you want to work on the display width, you will need to expand tabs one way or another.

Did you try the expand command as suggested above?

expand <<HERE | egrep -n '.{80}'
# eleven tabs with ii in between them
ii	ii	ii	ii	ii	ii	ii	ii	ii	ii	ii	ii
disregard this short line
# six spaces eleven times, with ii in between
ii      ii      ii      ii      ii      ii      ii      ii      ii      ii      ii      ii
HERE

Please use code tags when posting samples.