Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Command to find line length in a text file

Status
Not open for further replies.

jbmjlm10

Programmer
Jun 16, 2004
11
US
Hello,
Is there an AIX or UNIX command to find the length of each line of a text file? The wc command does not give us that option. Thank you for your help.
 
awk can do that:

awk '{printf "%6d ", length($0); print}' /path/to/file

HTH,

p5wizard
 
Try this script:

Code:
#!/bin/sh

line="Test"

if [ -z "$1" ]
then
  echo "Usage: $0 filename"
else
  if [ -r $1 ]
  then
    while read line
    do
      echo "$line" | wc -c
    done < $1
  fi
fi

just copy this script and give it a name (eg: countlines)

then issue this command to make it executable:

Code:
# chmod +x countlines

Then run it passing to it the file name to be examined:

Code:
# countlines testfile

Hope this will do the job for you guys :)

Regards
Khalid
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top