Find the length of each line in the file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Find the length of each line in the file
# 1  
Old 02-22-2011
Find the length of each line in the file

Hi,

I want to find the length of each line(including all the characters, spaces etc.) in a file and check if all the lines are of same length using a ksh script. Please help. Thanks in advance.
# 2  
Old 02-22-2011
You can read the lines in file one by one using 'awk' and get the length using "length()" function.

Try this :: ( Lets say the fields are separated by | )

Quote:
BEGIN { FS = "|" }
{
for (i=0;i<-NR;i++);
array[i]=$0;
}
{ print array[i] , length(array[i] ; }
# 3  
Old 02-22-2011
also,

Code:
$ while read line; do echo -n "$line" | wc -c; done< f1
5
6
7
$ cat f1
char5
char 6
char  7
$

# 4  
Old 02-22-2011
Shell:
Code:
while IFS= read -r line; do
  echo ${#line}
done < infile

Including linefeed character:
Code:
while IFS= read -r line; do
  echo $(( ${#line} + 1 ))
done < infile

This User Gave Thanks to Scrutinizer For This Post:
# 5  
Old 02-22-2011
Hi Anchal,

Thanks for your reply.

I have attached a sample file for which I am trying to find out the length. I tried the same code you have sent. I am not getting the correct output. This is the output I get:
562
576
The actual output is:
609
609

Thanks
# 6  
Old 02-22-2011
Hi Scrutinizer,

Thanks a lot for your reply..the code works absolutely fineSmilie
# 7  
Old 02-22-2011
Code:
awk '{print length+1}' checklength.txt

Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to find the length of MP4 file in cygwin?

Hi, Apologies if I'm posting in wrong section. How can I find length (duration) of MP4 videos and m4a audio files on cygwin? I heard about mediainfo, ffmpeg, avconv utilities on Linux platform but not sure if they work (or available) on cygwin. Please advise, TIA (1 Reply)
Discussion started by: prvnrk
1 Replies

2. Shell Programming and Scripting

Check for length which exceeds specified length in a line

Hi, I have a issue, I need to loop through a comma delimited file and check for the length which exceeds specified length , if Yes truncate the string. But my problem is , I do not have to check for all the fields and the field lenght is not same for all the fields. For ex: Say my line... (9 Replies)
Discussion started by: rashmisb
9 Replies

3. Shell Programming and Scripting

separate file by line length

hi all, i'm new in unix.... i have question, sorry if it's missplace or too silly let say i have a file name testfile.log that contains data 000001 000002 000003 aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbbbb cccccccccccccc dddddddddddddddddd 000004 i want to make new file... (3 Replies)
Discussion started by: venven
3 Replies

4. UNIX for Dummies Questions & Answers

What the command to find out the record length of a fixed length file?

I want to find out the record length of a fixed length file? I forgot the command. Any body know? (9 Replies)
Discussion started by: tranq01
9 Replies

5. Shell Programming and Scripting

How To Find Length of a Field in XML File

Hi I have a xml file with below data...have to find the length of the filedvalues... <?xml version="1.0" encoding="ISO-8859-15" standalone="no"?><abc xmlns:xsi="http://www.w3.org/2000/XMLSchem... (3 Replies)
Discussion started by: naughty21
3 Replies

6. Shell Programming and Scripting

find the length of file names in a directory?

Hi, how can find length of file names in a directory. Examp: I have a directory with name "d1". directory: d1 files: aaa.log bbb.log abcd.log abcdef.log I wold like out put like: file name legnth aaa.log 3 bbb.log 3 abcd.log 4 abcdef.log 5 (5 Replies)
Discussion started by: koti_rama
5 Replies

7. Shell Programming and Scripting

Find the length of the longest line

Dear All, To find the length of the longest line from a file i have used wc -L which is giving the proper output... But the problem is AIX os does not support wc -L command. so is there any other way 2 to find out the length of the longest line using awk or sed ? Regards, Pankaj (1 Reply)
Discussion started by: panknil
1 Replies

8. UNIX for Dummies Questions & Answers

Need find a file based length

Can some please help me? Want to find files over 35 characters in length? I am running HPUX. Would it be possible with find? Thanks in advance (8 Replies)
Discussion started by: J_ang
8 Replies

9. UNIX for Advanced & Expert Users

Fixing line length in a file

I have a file containing many lines all with varying lengths. I need each line in the file to be 100 characters in width but I can not find a way of doing this. I want to append to each line spaces to bring the width of the line to 100 but I need a generic way to work out how many spaces I need. ... (2 Replies)
Discussion started by: dbessell
2 Replies
Login or Register to Ask a Question