Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to print the number of characters in each line of a text file

Tags:

shell

unix

sed

awk

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.

like image 285
vikas368 Avatar asked Jan 09 '12 10:01

vikas368


People also ask

How do I count characters in a text file?

The most easiest way to count the number of lines, words, and characters in text file is to use the Linux command “wc” in terminal. The command “wc” basically means “word count” and with different optional parameters one can use it to count the number of lines, words, and characters in a text file.

How many characters are in one line of a text file?

Ruder concluded that the optimal line length for body text is 50–60 characters per line, including spaces (“Typographie”, E. Ruder). Other sources suggest that up to 75 characters is acceptable.

How do you count the characters of each line in a file in Python?

len(line) takes the number of characters in a string and converts it to the equivalent integer value. Your lengthList will then contain how many characters are on each line, stored as ints. If there are '\n's, you may want to use len(line) - 1, depending on what you want to do with the lengths.

What is the process to count the number of characters and lines in a file?

wc. The wc command is used to find the number of lines, characters, words, and bytes of a file. To find the number of lines using wc, we add the -l option.


3 Answers

Use Awk.

awk '{ print length }' abc.txt
like image 146
Fred Foo Avatar answered Sep 30 '22 18:09

Fred Foo


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.

like image 25
Jan Hudec Avatar answered Sep 30 '22 17:09

Jan Hudec


Here is example using xargs:

$ xargs -d '\n' -I% sh -c 'echo % | wc -c' < file
like image 4
kenorb Avatar answered Sep 30 '22 19:09

kenorb