Stack Overflow archive
0 scoreaccepted

shell programming - counting words from multiple files

score
0
question views
115
license
CC BY-SA 3.0
bash
#!/bin/sh

str=""
for i in $@
do
    str="${str}$(sed 's|\.||g' $i) " # remove the period and add space between files.
done
echo $str | tr -s ' ' '\n' | sort | uniq -c | sort -nr

bash
$ thescript file1.txt file2.txt

Output:

bash
  3 a
  2 mary
  2 little
  2 lamb
  2 had
  1 you
  1 white
  1 went
  1 was
  1 that
  1 snow
  1 Mary
  1 How
  1 His
  1 Hello
  1 fleece
  1 everywhere
  1 as
  1 are
  1 And

Originally posted on Stack Overflow. Public user contributions are licensed under Creative Commons Attribution-ShareAlike.