Characters in a single read

Hi,

I have the file which has the data :

accctaaccctaaccctaaccctaaccctaaccctaaccctaaccctaac
cctaacccaaccctaaccctaaccctaaccctaaccctaaccctaacccc
taaccctaaccctaaccctaaccctaacctaaccctaaccctaaccctaa
ccctaaccctaaccctaaccctaaccctaacccctaaccctaaccctaaa
ccctaaaccctaaccctaaccctaaccctaaccctaaccccaaccccaac
cccaaccccaaccccaaccccaaccctaacccctaaccctaaccctaacc
ctaccctaaccctaaccctaaccctaaccctaaccctaacccctaacccc
taaccctaaccctaaccctaaccctaaccctaaccctaacccctaaccct
aaccctaaccctaaccctcgcggtaccctcagccggcccgcccgcccggg
tctgacctgaggagaactgtgctccgccttcagagtaccaccgaaatctg

How many characters are in a single read?

Thanks.

Please use code tags as required by forum rules!

What do you mean by "a single read"? Reading into a shell variable (which shell)? Sngle variable or multiple? Into awk? In C?

The question is nonsense. A program can ask for as much, or as little, as it needs to, and the amount it actually it gets may be equal to or less than that -- subject to things like interrupts and end-of-file.

Each line is called a read, containing a number of characters.

accctaaccctaaccctaaccctaaccctaaccctaaccctaaccctaac
cctaacccaaccctaaccctaaccctaaccctaaccctaaccctaacccc
taaccctaaccctaaccctaaccctaacctaaccctaaccctaaccctaa
ccctaaccctaaccctaaccctaaccctaacccctaaccctaaccctaaa
ccctaaaccctaaccctaaccctaaccctaaccctaaccccaaccccaac
cccaaccccaaccccaaccccaaccctaacccctaaccctaaccctaacc
ctaccctaaccctaaccctaaccctaaccctaaccctaacccctaacccc
taaccctaaccctaaccctaaccctaaccctaaccctaacccctaaccct
aaccctaaccctaaccctcgcggtaccctcagccggcccgcccgcccggg
tctgacctgaggagaactgtgctccgccttcagagtaccaccgaaatctg

So, the question is : how many characters in a single read (line)?

  1. 50 characters, and one line-feed.

If it's windows text, it will be 52 -- 50 characters, one carriage return, one line feed.

try something like:

awk '{print length}' infile

I am new to Unix.
Can we use this command?

head -1 database.dat | wc

That's pretty close, really.

Use wc -c to print the number of characters instead of lines.

head -1 file_nam  | wc -m

So, is the character count is 51 or 50..
Do we report the line feed?

wc -c, not wc -m. For ASCII text, you want bytes. The difference is small but important.

wc will include the line feed in the length, yes.

If the task is to count characters, then -m should be used. Of course, to count characters correctly, the effective locale must be consistent with the file's contents.

Regards,
Alister

No, you absolutely do not want variable-sized characters when considering how many bytes to read.

For a moment there I thought I was going to learn something fundamental, but it seems we simply interpreted the task differently.

I don't see anything specifying bytes. From the thread's title to every relevant instance in OP text, the word "character" is used.

Your interpretation of the OP's requirements may be correct, but there's nothing in the text that supports t.

In practice, though, the difference is almost certainly academic.

Regards,
Alister

The only extant system I know where the system's character size is anything but 8-bit is Windows CE 3/4. (And perhaps newer.) And even it specifies reads in bytes, not characters.

You're arguing something that hasnt' been asserted. Even if you're 100% correct regarding which systems use which default encoding, it's irrelevant if the task involves counting the number of characters in a file (which may be encoded in an encoding that differs from the system default).

wc -c counts bytes. wc -m counts characters. If you care about characters, use -m . There's really nothing more to say except make sure that the correct encoding is in effect when counting characters.

Regards,
Alister

The very, very first post asserts that he wanted to read from a file. His follow up explained that he wanted to do so in fixed sizes.

If he's using shell, the size he needs to worry about for that, is bytes.

If he's not using shell, the size he needs to worry about for that -- is also bytes.

Not characters. Profoundly not characters. Even systems with a weird character size, read in byte sizes.

You're just arguing semantics now, but it doesn't hold up. Sorry.

Not at all. I believe you misunderstood the original post.

You are mistaken.

Your first response in this thread, post #3, indicates that you misinterpreted the use of read in post #1 as a reference to the system call of the same name. The OP's follow up, post #4, indicates that read instead refers to a line.

In that question, "read" refers to a line.

The task is to determine the length of a single line of text by counting letters (presumably all lines are the same length).

You are absolutely correct, but the read syscall is not relevant here.

Regards,
Alister

Please explain how he's going to read a line without it.

I did not say that read(2) is not necessary, only that it is irrelevant. What's relevant to correctly counting the number of characters in a line is the correct locale and inspection by a locale-aware interface, such as mbrtowc(3) and friends. Using -m provides the latter.

By the way, none of the OP's posts mention reading a fixed quantity (and none use the word byte). The very solution you were assisting with, head piped into wc, makes it clear that a fixed-size read is not part of the equation.

Regards,
Alister

Thanks for the replies guys..
I have done bit of research & found that wc -m counts characters while wc -c counts the bytes..
Is byte a character in general in this context??? Because both give the same results.