Hot answers tagged

55

From info ls: '-1' '--format=single-column' List one file per line. This is the default for 'ls' when standard output is not a terminal. When you pipe the output of ls, you get one filename per line. ls only outputs the files in columns when the output is destined for human eyes. Here's where ls decides what to do: switch (...


39

The command wc aka. word count can do it: $ wc -w <file> example $ cat sample.txt today is a good day $ wc -w sample.txt 5 sample.txt # just the number (thanks to Stephane Chazelas' comment) $ wc -w < sample.txt 5


38

The difference is caused by a newline added to the here string. See the Bash manual: The result is supplied as a single string, with a newline appended, to the command on its standard input (or file descriptor n if n is specified). wc is counting in the same way, but its input is different.


36

wc shows 3 characters more because your example file contains a fancy Unicode apostrophe ’ (most likely because you copied the contents from a browser or text editor): $ cat file Amy looked at her watch. He was late. The sun was setting but Jake didn’t care. $ wc file 1 16 82 file With plain ASCII apostrophe ': $ cat file2 Amy looked at her ...


33

You should use a command like this: find /group/book/four/word/ -type f -exec wc -l {} + | sort -rn find : search for files on the path you want. If you don't want it recursive, and your find implementation supports it, you should add -maxdepth 1 just before the -exec option. exec : tells the command to execute wc -l on every file. sort -rn : sort the ...


27

So I went to the source, and it looks like the slowness is in handling double byte characters. Essentially, for every character read in, it needs to call mbrtowc() to try to convert it to a wide character, then that wide character is tested to see if it's a word separator, line separator, etc. Indeed, if I change my locale LANG variable from the default ...


26

It's a succeeding newline added by the here-string redirector: $ s="hello" $ hexdump -C <<<"$s" 00000000 68 65 6c 6c 6f 0a |hello.| 00000006 $ printf "$s" | hexdump -C 00000000 68 65 6c 6c 6f |hello| 00000005


23

cut -d, -f3 | tr -d '\n' | wc -m (remember that wc -c counts bytes, not characters: $ echo a,1,españa,2 | cut -d, -f3 | tr -d '\n' | wc -c 7 $ echo a,1,españa,2 | cut -d, -f3 | tr -d '\n' | wc -m 6 )


22

awk -F, '{sum+=length($3)}; END {print +sum}' file


21

echo print newline (\n) at end of line echo abcd | xxd 0000000: 6162 6364 0a abcd. With some echo implementations, you can use -n : -n do not output the trailing newline and test: echo -n abcd | wc -c 4 With some others, you need the \c escape sequence: \c: Suppress the <newline> that otherwise follows the final argument in the ...


20

You can try to write in C: #include <unistd.h> #include <stdio.h> #include <string.h> int main(){ char buf[BUFSIZ]; int nread; size_t nfound=0; while((nread=read(0, buf, BUFSIZ))>0){ char const* p; for(p=buf; p=memchr(p,'\n',nread-(p-buf)); nfound++,p++) {;} } if(nread<0) { perror("Error"); return 1; } printf("%...


19

The char type in C is one byte, but it's intended for ASCII characters; there are variable-width encodings like UTF-8 that can take up many bytes per character. wc uses the mbrtowc(3) function to decode multibyte sequences, depending on the locale set by the LC_CTYPE environment variable. If you set the locale properly, you should get the same result for all ...


18

The wc command is counting the words in the output from grep, which includes "for": > grep shell test.txt for shell_A shell_B shell_C So there really are 4 words. If you only want to count the number of lines that contain a particular word in a file, you can use the -c option of grep, e.g., grep -c shell test.txt Neither of those actually count words,...


18

You can improve on the solution suggested by @pskocik by reducing the number of calls to read. There are a lot of calls to read BUFSIZ chunks from a 1Gb file. The usual approach to doing this is by increasing the buffer size: just for fun, try increasing the buffer-size by a factor of 10. Or 100. On my Debian 7, BUFSIZ is 8192. With the original ...


17

Here's a variant of an awk solution for printing the first found minimum line: awk ' NR==1 || length<len {len=length; line=$0} END {print line} ' which can simply be extended by one condition to print all minimum lines: awk ' length==len {line=line ORS $0} NR==1 || length<len {len=length; line=$0} END {print line}' '


17

With sqlite3: sqlite3 <<EOT CREATE TABLE file(line); .import "data.txt" file SELECT line FROM file ORDER BY length(line) LIMIT 1; EOT


17

Probably your last line does not have a newline. See this: $ printf bla > file $ wc -l file 0 file $ man 1p wc -l Write to the standard output the number of newlines in each input file. On Unix it's good style to have a newline character at the end of text files. If you can't repair your files you can use these workarounds to count the last ...


16

The command 'grep' is outputting the entire lines that "shell" appear on. Not just the word "shell." As can be seen below: grep shell test.txt for shell_A shell_B shell_C I would recomend using the option -o, --only-matching So: grep -o "shell" test.txt | wc -w


15

At a guess, Your locale uses UTF-8 encoding, and About 10% of your file consists of characters which require more than one octet to encode into UTF-8. By the way, from man wc: -c, --bytes print the byte counts -m, --chars print the character counts


14

Because the output of ls depends on the std output, it is different for terminal and pipe. Try /bin/ls | cat


14

sed -n 10p myfile | wc -c will count the bytes in the tenth line of myfile (including the linefeed/newline character). A slightly less readable variant, sed -n "10{p;q;}" myfile | wc -c (or sed '10!d;q' or sed '10q;d') will stop reading the file after the tenth line, which would be interesting on longer files (or streams). (Thanks to Tim Kennedy and ...


14

The first command you mention, find . -type f -exec wc -l {} +, really says "run wc -l on as many files as possible, until all of them have been processed". This can run wc multiple times! On the other hand, find . -type f -exec cat {} + | wc -l can run cat several times, but will only run wc once. (More in detail, this is because in this case cat is called ...


13

One approach would be to make use of ls to give us a list of the files, but we want this list to be guaranteed to show only 1 file or directory per line. The -1 switch will do this for us. $ ls -1 dir1 dir2 dir3 fileA fileB fileC Example Create the above sample data in an empty directory. $ mkdir dir{1..3} $ touch file{A..C} Check it: $ ls dir1 dir2 ...


13

A Perl way. Note that if there are many lines of the same, shortest length, this approach will only print one of them: perl -lne '$m//=$_; $m=$_ if length()<length($m); END{print $m if $.}' file Explanation perl -lne : -n means "read the input file line by line", -l causes trailing newlines to be removed from each input line and a newline to be added ...


13

Yes, there are such cases. In case of symlinks on Linux system with GNU ls, the ls -l will put out the size of the link, while wc -c will resolve the actual file and read number of bytes there. Below you can see that ls -l reports 29 bytes , while wc reports 172 bytes in the actual file. $ ls -l /etc/resolv.conf ...


12

In your first example echo will add it's newline at the end, you can stop this by adding the -n option to echo. wc counts characters, words and lines, lines are defined as zero or more characters ending in line feed (\n).


12

Python comes out fairly concise, and the code Does What It Says On The Tin: python -c "import sys; print min(sys.stdin, key=len)," The final comma is obscure, I admit. It prevents the print statement adding an additional linebreak. Additionally, you can write this in Python 3 supporting 0 lines like: python3 -c "import sys; print(min(sys.stdin, key=len, ...


12

pipe the file thrught xxd to see a hex output side-by-side of the ascii, this will let you see if there are extra characters which you can't see or are unprintable. $ cat file one‏ and ‏two $ cat file | wc 1 3 18 $ cat file | xxd 00000000: 6f6e 65e2 808f 2061 6e64 20e2 808f 7477 one... and ...tw 00000010: 6f0a ...


11

There is no newline, so wc -l is correct. Instead, you want to count the number of start of lines. One way to do it: $ diff -y --suppress-common-lines a b | grep '^' | wc -l 1


11

When ls is executed it parses various options. It also detect if output is a tty or not by isatty(). ls.c: code case LS_LS: /* This is for the `ls' program. */ if (isatty (STDOUT_FILENO)) { format = many_per_line; /* See description of qmark_funny_chars, above. */ qmark_funny_chars = true; } else { format = ...


Only top voted, non community-wiki answers of a minimum length are eligible