Visit Our UNIX and Linux User Community


Characters in a single read


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Characters in a single read
# 15  
Old 08-22-2013
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
# 16  
Old 08-23-2013
Quote:
Originally Posted by alister
You're arguing something that hasnt' been asserted.
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.
# 17  
Old 08-23-2013
Quote:
Originally Posted by Corona688
You're just arguing semantics now, but it doesn't hold up. Sorry.
Not at all. I believe you misunderstood the original post.

Quote:
Originally Posted by Corona688
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.
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.

Quote:
Originally Posted by rajivn786
How many characters are in a single read?
In that question, "read" refers to a line.

Quote:
Originally Posted by rajivn786
Each line is called a read, containing a number of characters.
The task is to determine the length of a single line of text by counting letters (presumably all lines are the same length).

Quote:
Originally Posted by Corona688
Even systems with a weird character size, read in byte sizes.
You are absolutely correct, but the read syscall is not relevant here.

Regards,
Alister
# 18  
Old 08-23-2013
Quote:
Originally Posted by alister
You are absolutely correct, but the read syscall is not relevant here.
Please explain how he's going to read a line without it.
# 19  
Old 08-23-2013
Quote:
Originally Posted by Corona688
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
# 20  
Old 08-26-2013
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.
# 21  
Old 08-26-2013
A ASCII character is represented by a single byte. A unicode character is represented by more than one byte. It all depends what you need this information for. Is it possible that you will have international characters in your file?
Maybe an example using the unicode character ä makes it more clear to you:
Code:
$ echo "äbc" |wc -c
5
$ echo "äbc" |wc -m
4


Last edited by cero; 08-26-2013 at 10:26 AM..
 

Previous Thread | Next Thread
Test Your Knowledge in Computers #765
Difficulty: Medium
Cache read misses from an instruction cache generally cause the longest delays because the processor, and in particular the thread of execution, has to wait until the instruction is fetched from main memory.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Read 2 input and produce it in single ouput?

Hi all. I’ve 2 inputs here and would like to produce it in single ouput. I’ve drafted simple shell script but not sure how to put all this together. The final output should be “GROUP-XYZ” instead of “TEST” Please advise. INPUT1 GROUP-XYZ INPUT2 type8code0@box:~/dbedit$ cat... (8 Replies)
Discussion started by: type8code0
8 Replies

2. Shell Programming and Scripting

how to read multi line characters in a single column

Hi, I have file in which fields are pipe (|) separated and the record separator is new line (\n). But sometime I am getting a field value which is spread across multiple line. Basically I am getting a file from another system in the below format and needs to process the file and load data into a... (3 Replies)
Discussion started by: satyaatcgi
3 Replies

3. Shell Programming and Scripting

How to remove characters enclosed in single quotes?

How to remove characters enclosed in single quotes? My data is something like this (03/22/2011 08:17:26.650) : ( -> '1' -> '1-1-3' -> '6' -> '1' -> 'SALMOR58BB4' aaaaa bbbbbb ccccc ((dddd)) I want the output to be (03/22/2011 08:17:26.650) : ( -> -> -> -> -> aaaaa... (2 Replies)
Discussion started by: rdhanek
2 Replies

4. Shell Programming and Scripting

Read Embedded Newline characters with read (builtin) in KSH93

Hi Guys, Happy New Year to you all! I have a requirement to read an embedded new-line using KSH's read builtin. Here is what I am trying to do: run_sql "select guestid, address, email from guest" | while read id addr email do ## Biz logic goes here done I can take care of any... (6 Replies)
Discussion started by: a_programmer
6 Replies

5. Shell Programming and Scripting

read single chars

This reads single keystrokes and produces an output: #! /bin/bash while : ; do read -s -n 1 >/dev/null 2>&1 echo ${REPLY} done | awk '{print}' This second one don't. Even though these examples make no sense; the real code is more complicated. Who knows what the problem is... (2 Replies)
Discussion started by: elbrand
2 Replies

6. Shell Programming and Scripting

Bash KeyPress (or Read Single Character)

Hi, I'm sorry if this has already been posted somewhere but I can't seem to find it on the forums (or anywhere on google :( ) I am writing a script where a user must enter a single character to perform an action. For example, Press Q to Quit or R to Refresh Basically I am stuggling... (5 Replies)
Discussion started by: Ste_Moore01
5 Replies

7. Shell Programming and Scripting

Multiple characters including single quote in delimiter

Hello, I need to replace the comma to something else between the single quote: 1aaa,bbb,'cc,cc','ddd',1 2aaa,bbb,'ccc','d,d',0 to 1aaa,bbb,'cc<comma>cc','ddd',1 2aaa,bbb,'ccc','d<comma>d',0 Can someone help? Thanks. (2 Replies)
Discussion started by: bgirl
2 Replies

8. Shell Programming and Scripting

Read Single Value From File With Perl

Hi all, I have what I would have thought was a very simple problem but I can' find an elegant solution. I have a file which has a single value you in it, say 194. All I want my perl script to do is open the file, read the value and assign that value to a variable. I've done stuff like... (1 Reply)
Discussion started by: Donkey25
1 Replies

9. UNIX for Dummies Questions & Answers

Sed - Lower case single characters

Hello, I have a file where I am supposed to convert all the single i characters to uppercase, but when I try, it converts all the i's inside of words to uppercase as well. I tried doing: cat filename | sed 's/i/I/g' but that obviously does not work. Any help would be greatly... (6 Replies)
Discussion started by: zlindner
6 Replies

10. Shell Programming and Scripting

Read line with a single key press...

I would really like to have a script that will accept the key press from the user with out having to press the enter key afterwards. i.e. echo "Press Y to print \c" read YesNo At this point the user has to press the enter key to continue. Is there a way to accept the key press from the... (3 Replies)
Discussion started by: jagannatha
3 Replies

Featured Tech Videos