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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help required in displaying lines exceeding 79 chars along with their line numbers ??
# 1  
Old 10-08-2008
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......
# 2  
Old 10-08-2008
Code:
perl -ne '$l=length()-1; print "$l $_" if ($l > 79)' Sample.cpp

For source code you should probably also expand tabs. (man expand)
# 3  
Old 10-08-2008
Check if this helps..

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

# 4  
Old 10-08-2008
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.

Code:
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

Code:
egrep -n '.{80}' file

# 5  
Old 10-08-2008
The scripts cannot identify tab space in between.......

Hi folks,

Thanks for the replies...Smilie

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 ?????..Smilie....for time being i assume that the cases i face will be the second one....and will continue to use the mentioned scripts.......thnks..
# 6  
Old 10-08-2008
Code:
awk ' length >= 80 { print $0, NR }' filename

# 7  
Old 10-09-2008
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

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

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Reading a file line by line and print required lines based on pattern

Hi All, i want to write a shell script read below file line by line and want to exclude the lines which contains empty value for MOUNTPOINT field. i am using centos 7 Operating system. want to read below file. # cat /tmp/d5 NAME="/dev/sda" TYPE="disk" SIZE="60G" OWNER="root"... (4 Replies)
Discussion started by: balu1234
4 Replies

2. Shell Programming and Scripting

Delete several lines if the first line contain numbers > 200

I have a file of the following format: $data1 size 1278 dataw datat datau datai $data2 size 456 datak dataf datat datay datal $data3 size 154 datag datas datat datar datas (8 Replies)
Discussion started by: FelipeAd
8 Replies

3. UNIX for Dummies Questions & Answers

Replace lines of two files by the corresponding line numbers.

I want to replace lines. The files 1 are (separated by \t) Gm01 phytozome9_0 three_prime_UTR 70641 70759 . - . ID=PAC:26323927.three_prime_UTR.1;Parent=PAC:26323927;pacid=26323927 Gm01 phytozome9_0 three_prime_UTR 90230 90692 . - . ... (1 Reply)
Discussion started by: grace_shen
1 Replies

4. Shell Programming and Scripting

Find Special/Null/Control Chars and Print Line Numbers

Hi All, This might be a basic question... I need to write a script to find all/any Speacial/Null/Control Chars and Print Line Numbers from an input file. Output something like Null Characters in File Name at : Line Numbers Line = Print the line Control Characters in File Name at : Line... (2 Replies)
Discussion started by: Kevin Tivoli
2 Replies

5. UNIX for Dummies Questions & Answers

Grep lines with numbers greater than 2 digits at the end of the line

I'm trying to grep lines where the digits at the end of each line are greater than digits. Tried this but it will only allow me to specify 2 digits. Any ideas would greatly be appreciated. grep -i '\<\{3,4,5\}\>' file ---------- Post updated at 05:58 PM ---------- Previous update was at 05:41... (1 Reply)
Discussion started by: jimmyf
1 Replies

6. Shell Programming and Scripting

script to replace numbers on lines according to condition on the same line

hello everyone my file contains many records, the following is a sample: BEGIN ASX1500000050002010120000000308450201012000177 ASX1100002000000201012000000038450201012000220 ASX1600100005000201012000000038450020101200177 ASX1900100006000201067000000058450020101200177... (2 Replies)
Discussion started by: neemoze
2 Replies

7. Shell Programming and Scripting

Delete lines with line numbers.

Hi, I have a file will 1000 lines.... I want to deleted some line in the file... like 800-850 lines i want to remove in that... can somebody help me..? thanks. (2 Replies)
Discussion started by: Kattoor
2 Replies

8. Shell Programming and Scripting

Unix help to find blank lines in a file and print numbers on that line

Hi, I would like to know how to solve one of my problems using expert unix commands. I have a file with occasional blank lines; for example; dertu frthu fghtu frtty frtgy frgtui frgtu ghrye frhutp frjuf I need to edit the file so that the file looks like this; (10 Replies)
Discussion started by: Lucky Ali
10 Replies

9. Shell Programming and Scripting

find 4 chars on 2nd line, 44 chars over

I know this should be simple, but I've been manning sed awk grep and find and am stupidly stumped :( I'm trying to use sed (or awk, find, etc) to find 4 characters on the second line of a file.txt 44-47 characters in. I can find lots of sed things for lines, but not characters. (4 Replies)
Discussion started by: unclecameron
4 Replies

10. Shell Programming and Scripting

display lines b/w given line numbers

write a shell script that accepts a file name starting and ending line numbers as arguments and displays all the lines between the given line numbers:b:.help is appreciated.thank you. (3 Replies)
Discussion started by: shawz
3 Replies
Login or Register to Ask a Question