1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

標準入力・標準出力の学習のまとめ

Posted at

基本概念

標準入力・標準出力とは

標準入力(stdin)  → [プログラム] → 標準出力(stdout)
                              → 標準エラー出力(stderr)

デフォルト:

  • 標準入力:キーボード
  • 標準出力:画面
  • 標準エラー出力:画面

リダイレクト

標準入力・標準出力をデフォルトから切り替える

基本的なリダイレクト

# lsで表示される出力をファイルに
ls > list.txt

# 入力をファイルから
cat < file.txt

# エラーをファイルに
cat nofile.txt 2> error.txt

コマンドの分類

データ生成系(標準出力メイン)

ls          # ファイル一覧
cat file    # ファイル内容
echo hello  # テキスト出力
date        # 現在時刻

データ処理系(標準入力→標準出力)

grep txt    # txtを含む行を抽出
sort        # ソート
head -5     # 最初の5行
wc -l       # 行数カウント

データ表示系(標準入力→画面)

less        # ページ単位表示

パイプライン

パイプの仕組み

コマンドA | コマンドB
  • コマンドAの標準出力 → コマンドBの標準入力

簡単な例

# ファイル検索
ls | grep txt

# ファイル数カウント
ls | wc -l

# ソートして表示
ls | sort

# 最初の3個だけ
ls | head -3

# 見やすく表示
ls | less

複数の組み合わせ

ls | grep txt | sort | head -5
# txtファイルを探して、ソートして、最初の5個表示

重要なポイント

** データの流れ**

生成 → 処理 → 処理 → 表示
ls  → grep → sort → less

基本パターン

何かを表示 | 絞り込み
何かを表示 | 並び替え
何かを表示 | 数える
1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?