Hot answers tagged

39

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 ...


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 ...


26

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


23

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 ...


22

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


22

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 )


20

echo print newline (\n) at end 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; } ...


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 ...


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 ...


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

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 ...


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


14

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


14

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 ...


13

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


13

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


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 = ...


11

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 ...


10

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).


10

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 ...


10

I always love solutions with pure shell scripting (no exec!). #!/bin/bash min= is_empty_input="yes" while IFS= read -r a; do if [ -z "$min" -a "$is_empty_input" = "yes" ] || [ "${#a}" -lt "${#min}" ]; then min="$a" fi is_empty_input="no" done if [ -n "$a" ]; then if [ "$is_empty_input" = "yes" ]; then min="$a" ...


10

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.


10

Whenever you don't understand a command, read its manual. In this case, man wc would show you that: DESCRIPTION Print newline, word, and byte counts for each FILE, and a total line if more than one FILE is specified. [...] A word is a non-zero-length sequence of characters delimited by white space. So, the three numbers are i) the number of ...


9

The reason why you get 3 here has already been explained, but to add a bit more about the question in the subject: By the strict (POSIX) definition of the term, a text line is always terminated by a newline character, so counting the number of newline characters is the same as counting the number of lines. Sometimes though, you find files that have data ...


8

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 ...


8

Your system should have GNU grep, that has an option -P to use Perl expressions and you can use that, combined with -c (so no need for wc -l): grep -Pvc '\S' somefile The '\S' hands the pattern \S to grep and matches all line containing anything that is not space, -v selects all the other lines (those only with space), and -c counts them. From the man ...


8

Both bash and zsh have a >(pipeline) feature: psql ... | tee >(wc -l) | bzip2 Note that the > here is not a normal redirection, but a necessary part of the syntax. You would need a second > if you wanted to combine it with an actual redirection (with a space in between so it wouldn't be read as >> redirect-for-append).


8

Here a pure zsh solution (it prints all lines with the minimal length, from file): IFS=$'\n'; print -l ${(M)$(<file):#${~${(o@)$(<file)//?/?}[1]}} Example input: seven/7 4for 8 eight? five! four Output is: 4for four I think it needs a short explanation :-) First, we set the internal field separator to newline: IFS=$'\n'; So far so good, ...


8

Historically, ls wrote its output one file per line, which is a convenient format for processing with other text-based Unix tools (like wc). However, on a 24 line terminal with no scrollback, large listings had a tendency to scroll off the screen, making it hard to find what you were looking for. So, at some point, the BSD developers changed the behavior ...



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