Skip to content

Instantly share code, notes, and snippets.

@k0f1sh
Created May 10, 2020 12:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save k0f1sh/b4e6afb8b80c54764ca57a088812075f to your computer and use it in GitHub Desktop.
Save k0f1sh/b4e6afb8b80c54764ca57a088812075f to your computer and use it in GitHub Desktop.
第2章: UNIXコマンド
#!/bin/bash
# https://nlp100.github.io/ja/ch02.html
# 10
cat popular-names.txt | bb -io "(count *input*)"
# 11
cat popular-names.txt | bb -i '(doseq [line *input*] (println (str/replace line #"\t" " ")))' > popular-names-spaces.txt
# 12
cat popular-names.txt | bb -i '(doseq [line *input*] (println (nth (str/split line #"\t") 0)))' > col1.txt
cat popular-names.txt | bb -i '(doseq [line *input*] (println (nth (str/split line #"\t") 1)))' > col2.txt
# 13
BBCODE=$(cat << EOF
(with-open [col1 (io/reader "./col1.txt")
col2 (io/reader "./col2.txt")]
(doseq [[name sex] (map list (line-seq col1) (line-seq col2))]
(println (str name "\t" sex))))
EOF
)
bb -e "$BBCODE"
# 14
cat col1.txt | bb -i '(doseq [line (take (Integer. (first *command-line-args*)) *input*)] (println line))' 5
# 15
cat col1.txt | bb -i '(doseq [line (take-last (Integer. (first *command-line-args*)) *input*)] (println line))' 5
# 16
BBCODE=$(cat << EOF
(let [lines (partition (Integer. (first *command-line-args*)) *input*)]
(doseq
[[idx lines] (map-indexed (fn [idx lines] [idx lines]) lines)]
(spit (str "filename" idx) (str/join "\n" lines))))
EOF
)
cat col1.txt | bb -i "$BBCODE" 100
# 17
cat col1.txt | bb -i '(doseq [name (into #{} *input*)] (println name))'
# 18
BBCODE=$(cat << EOF
(let [sorted-lines (sort (fn [a b]
(let [a-third (nth (str/split a #"\t") 2)
b-third (nth (str/split b #"\t") 2)]
(compare a-third b-third)))
*input*)]
(doseq [line sorted-lines]
(println line)))
EOF
)
cat popular-names.txt | bb -i "$BBCODE"
# 19
cat col1.txt | bb -i '(->> (frequencies *input*) (sort-by val >) (map key))'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment