summaryrefslogtreecommitdiffhomepage
path: root/vhosts/blog/content/posts/2025-03-27/zip-function-like-command-paste-command.dj
diff options
context:
space:
mode:
authornsfisis <nsfisis@gmail.com>2025-04-09 22:24:50 +0900
committernsfisis <nsfisis@gmail.com>2025-04-09 22:24:50 +0900
commitd1b4bc44170196a4dcef5254d092fc387e73792e (patch)
treea6bbe0e61b73c99a815d730ae7956a5124200066 /vhosts/blog/content/posts/2025-03-27/zip-function-like-command-paste-command.dj
parentbba1212ab46ed85c2ed3b646f2362bdbb1f45b63 (diff)
parent4f46d262e6967c9c638b40f3b0246d21b7a9b9dc (diff)
downloadnsfisis.dev-d1b4bc44170196a4dcef5254d092fc387e73792e.tar.gz
nsfisis.dev-d1b4bc44170196a4dcef5254d092fc387e73792e.tar.zst
nsfisis.dev-d1b4bc44170196a4dcef5254d092fc387e73792e.zip
Merge branch 'nuldoc-djot'
Diffstat (limited to 'vhosts/blog/content/posts/2025-03-27/zip-function-like-command-paste-command.dj')
-rw-r--r--vhosts/blog/content/posts/2025-03-27/zip-function-like-command-paste-command.dj93
1 files changed, 93 insertions, 0 deletions
diff --git a/vhosts/blog/content/posts/2025-03-27/zip-function-like-command-paste-command.dj b/vhosts/blog/content/posts/2025-03-27/zip-function-like-command-paste-command.dj
new file mode 100644
index 00000000..8c9417fa
--- /dev/null
+++ b/vhosts/blog/content/posts/2025-03-27/zip-function-like-command-paste-command.dj
@@ -0,0 +1,93 @@
+---
+[article]
+uuid = "99111377-27e7-427b-9dc5-a23f621fa826"
+title = "zip 関数のようなコマンド paste"
+description = "zip 関数のような動きをする paste コマンドについてのメモ。"
+tags = [
+ "note-to-self",
+]
+
+[[article.revisions]]
+date = "2021-03-22"
+remark = "デジタルサーカス株式会社の社内記事として公開"
+isInternal = true
+
+[[article.revisions]]
+date = "2025-03-27"
+remark = "ブログ記事として一般公開"
+---
+::: note
+この記事は、2021-03-22 に [デジタルサーカス株式会社](https://www.dgcircus.com/) の社内 Qiita Team に公開された記事をベースに、加筆修正して一般公開したものです。
+:::
+
+{#intro}
+# 実現したい内容
+
+次の2ファイル `a.txt` / `b.txt` から出力 `ab.txt` を得たい。
+
+`a.txt`
+
+```
+a1
+a2
+a3
+```
+
+`b.txt`
+
+```
+b1
+b2
+b3
+```
+
+`ab.txt`
+
+```
+a1
+b1
+a2
+b2
+a3
+b3
+```
+
+ちょうど Python や Haskell などにある `zip` 関数のような動きをさせたい。
+
+{#paste-command}
+# 実現方法
+
+記事タイトルに書いたように、`paste` コマンドを使うと実現できる。
+
+```
+$ paste -d '\
+' a.txt b.txt > ab.txt
+```
+
+`paste` コマンドは複数のファイルを引数に取り、それらを1行ずつ消費しながら `-d` で指定した文字で区切って出力する。
+`-d` は区切り文字の指定で、デフォルトだとタブ区切りになる。
+
+ファイル名には `-` を指定でき、その場合は標準入力から読み込んで出力する。
+このとき `paste - -` のように複数回 `-` を指定すると、指定した回数の行ごとに連結することができる。
+例えば `ab.txt` だとこうなる。
+
+```
+$ paste - - < ab.txt
+a1 b1
+a2 b2
+a3 b3
+```
+
+これは標準入力を使うとき特有の挙動で、単に同じファイル名を指定してもこうはならない。
+
+```
+$ paste ab.txt ab.txt
+a1 a1
+b1 b1
+a2 a2
+b2 b2
+a3 a3
+b3 b3
+```
+
+ときどき便利。