I would like to print the number of characters in each line of a text file using a unix command. I know it is simple with powershell
gc abc.txt | % {$_.length}
but I need unix command.
Use Awk.
awk '{ print length }' abc.txt
while IFS= read -r line; do echo ${#line}; done < abc.txt
It is POSIX, so it should work everywhere.
Edit: Added -r as suggested by William.
Edit: Beware of Unicode handling. Bash and zsh, with correctly set locale, will show number of codepoints, but dash will show bytes—so you have to check what your shell does. And then there many other possible definitions of length in Unicode anyway, so it depends on what you actually want.
Edit: Prefix with IFS=
to avoid losing leading and trailing spaces.
IFS=
on the read
command when wanting to read in arbitrary data. So IFS= read -r
. read
uses the IFS
to do word splitting, and even though all the split words then get pasted back together into the one available variable (line
), there is no guarantee that they get pasted back together with all the original separator characters they had or just one potentially different ones. For example, with the default IFS, the line foo bar
could become foo bar
, losing 7 spaces. (Like how Stack Overflow lost the adjacent spaces in that example string in this comment).
IFS
should be set, but the problem when it isn't is more subtle.
Dec 30, 2019 at 12:43
Here is example using xargs
:
$ xargs -d '\n' -I% sh -c 'echo % | wc -c' < file
I've tried the other answers listed above, but they are very far from decent solutions when dealing with large files -- especially once a single line's size occupies more than ~1/4 of available RAM.
Both bash and awk slurp the entire line, even though for this problem it's not needed. Bash will error out once a line is too long, even if you have enough memory.
I've implemented an extremely simple, fairly unoptimized python script that when tested with large files (~4 GB per line) doesn't slurp, and is by far a better solution than those given.
If this is time critical code for production, you can rewrite the ideas in C or perform better optimizations on the read call (instead of only reading a single byte at a time), after testing that this is indeed a bottleneck.
Code assumes newline is a linefeed character, which is a good assumption for Unix, but YMMV on Mac OS/Windows. Be sure the file ends with a linefeed to ensure the last line character count isn't overlooked.
from sys import stdin, exit
counter = 0
while True:
byte = stdin.buffer.read(1)
counter += 1
if not byte:
exit()
if byte == b'\x0a':
print(counter-1)
counter = 0
Try this:
while read line
do
echo -e |wc -m
done <abc.txt
echo -e | wc -m
, didn't you? It's useless use of commands; shell can count characters in a variable. Plus echo -e
is totally incompatible and works in half of the shells while starting with some escape sequence works in some other and nothing in the rest.
Jan 9, 2012 at 13:46