Operating System - HP-UX
1834185 Members
2472 Online
110064 Solutions
New Discussion

Show character column or how many characters in a line on a file

 
SOLVED
Go to solution
Ratzie
Super Advisor

Show character column or how many characters in a line on a file

I have a file that must be 773 characters long.
How do I figure this out?
Also, how do I show where the cursor is, what character it is at.

example, I open a file in vi.
hit the l button a dozen times maybe even 100 times, how do I know where my cursor in in the line.
10 REPLIES 10
Sean Dale
Trusted Contributor

Re: Show character column or how many characters in a line on a file

to find out how many characters are in the file, you can do

cat file | wc -c

wc can also be used to count lines, words, etc.

see the manpage for the options


Live life everyday
Ratzie
Super Advisor

Re: Show character column or how many characters in a line on a file

No, I need to find out how many characters are on a line.
And also to show the carridge returns in the file.
Ratzie
Super Advisor

Re: Show character column or how many characters in a line on a file

No, sorry I am looking on a LINE basis. Should have been more clearer.

I need to find out how many characters are on a line.
And also to show the carridge returns in the file.
John Poff
Honored Contributor

Re: Show character column or how many characters in a line on a file

One trick that I like in vi is to use the vertical bar. If you type in a number and then press the vertical bar symbol, the cursor will go to that column. If the number is higher than the number of columns in that line, the cursor will go to the end of the line. So you could type in 7 7 3 | and it should take you to that column on the line.

JP
Sean Dale
Trusted Contributor

Re: Show character column or how many characters in a line on a file

I'm sure there are many ways to do this, but, here is how I would do it using a perl script:

open(FILE, "C:\\filename.txt");

while ()
{
$line++;
chomp $_;
$len = length($_);
print "line: $line, length: $len\n";
}

close(FILE);
Live life everyday
A. Clay Stephenson
Acclaimed Contributor
Solution

Re: Show character column or how many characters in a line on a file

To find the length of a line of a file do something like this:

while read X < myfile |
do
echo "X = \"${X}\""
LEN=$(expr length "${X}")
echo "Length = ${LEN}"
done

Man expr for details.

To determine exact character offsets you do not want to use vi but rather a tool like od or xd:

od -Ad -v -tc -toC myfile

This will display the ASCII character and octal value for each byte is my file as well as the offsets into the file.

od -Ad -v -tc -toC myfile > myfile.od

You can then view myfile.od with a text editor if the file is large and still see exactly the offsets. Control characters are printed as well so your LF's and CR's will be obvious.

od and xd are tools you should really be familiar with. Man od for details.


If it ain't broke, I can fix that.
Jeff_Traigle
Honored Contributor

Re: Show character column or how many characters in a line on a file

This will print the line number and the length of the line:

awk '{print NR, length()}' testfile

Within vi, I'm not sure how to do any of what you ask. A quick glance through the vi and ex man pages didn't show anything promising. I think these may only be available in one of the enhanced vi clones like vim or elvis.
--
Jeff Traigle
Hein van den Heuvel
Honored Contributor

Re: Show character column or how many characters in a line on a file

In my 'vi' I can use ^G to see where I am.

To check the whole file you could use 'wc' and compare the bytes reported with the lines reported times 773

But as is clear from several of your other questions around this magic 773 byte file, you should not be trying an interactive editting session on this file at all.

Hein.
Peter Godron
Honored Contributor

Re: Show character column or how many characters in a line on a file

Hi,
vi does not have the facility to show current row:column on screen.
There are clones (vim,vile,elvis,nvi) that do this.

You can create a file files with 773 nulls by:
prealloc filename 773
Ratzie
Super Advisor

Re: Show character column or how many characters in a line on a file

Thanks