0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Linucレベル1勉強メモ #28

Posted at

# 第三章 GNUとUnixのコマンド

練習問題3.3
ファイルsample.txt内の英小文字をすべて英大文字に変更して画面上に出力するコマンドをすべて選択してください。

A. tr 'a-z' 'A-Z' sample.txt

B. cat sample.txt | tr 'a-z' 'A-Z'

C. tr -f 'a-z' -t 'A-Z' < sample.txt

D. tr [:lower:] [:upper:] <sample.txt

E. tr '[:lower:]' '[:upper:]' < sample.txt

(解答)B, D, E

(解説)
trコマンドで文字を変換して画面上に出力することができます。trコマンドはファイルを引数に指定できないので注意してください。catコマンドなどを使って出力したものをパイプでtrコマンドに送るか、入力リダイレクトを使って指定します。なお、引用符(’)で囲まなくても構いません。選択肢Cは書式が誤っているので不正解です。

※注意 trコマンドはファイルを引数として指定できない!!!

trコマンド
標準入力から読み込まれた文字列を変換したり、削除したりします。
trコマンドの主なオプション

  1. -d, --delete
    「文字列1」とマッチした文字列を削除する。
  2. -s, --squeeze-repeats
    連続するパターン文字列を1文字として処理する

クラス

  1. [:alpha:]
    英字
  2. [:lower:]
    英小文字
  3. [:upper:]
    英大文字
  4. [:digit:]
    数字
  5. [:alnum:]
    英数字
  6. [:space:]
    スペース

(わからない単語解説)

  • パイプ
    コマンドやプログラムの出力結果を、別のコマンドやプログラムの入力にわたせると、単純な動作のコマンドを組み合わせて複雑な処理をすることができます。この時に使われるのがパイプ(パイプライン)です。記号では「|」で表します。
  • <(リダイレクト)
    標準入力のリダイレクトであり、これを行うと<後のファイルの記載内容を1行ずつ読み込んで実行するという処理を行うことができます。
  • catコマンド
    LinuxやUNIX系OSでファイルの内容を表示したり、ファイルを凍結したありするのに使われる基本的なコマンドです。
0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?