概要
小ネタ。
大抵、最初に覚えるコマンドの一つである cat コマンドさん。
実はいろんなオプションがあって、面白いです。
行番号を付与して出力
-n
オプション、または -b
オプションを付与すると行番号を付与して出力してくれます。
違いは -n
オプションが空行も含めて全行に行番号を振ってくれるのと、-b
オプションは空行には行番号を振らないって違いです。
#!/usr/bin/env bash # # cat コマンドのちょっとした Tips (行番号を付与) # # REFERENCES: # - https://linuxhint.com/cat-command-linux/ # _exec() { # 作業用ディレクトリ作成 basedirpath=/tmp/try-linux/cat/with_number rm -rf "${basedirpath}" mkdir -p "${basedirpath}" cat << EOF > "${basedirpath}/app.go" package main func main() { println("hello Golang") } EOF # -n オプションをつけると行番号を付与してくれる # この場合、空行を含む全行に行番号が振られる cat -n "${basedirpath}/app.go" echo '---------------------------------' # -b オプションをつけても行番号を付与してくれる # この場合、空行には行番号は振られない cat -b "${basedirpath}/app.go" } _exec
try-linux/with_number.sh at master · devlights/try-linux · GitHub
実行すると以下のようになります。
gitpod /workspace/try-linux $ make ENTER EXAMPLE NAME: with_number [INPUT ] with_number [TARGET] cat_with_number [SCRIPT] basic/cat/with_number.sh ===== START [basic/cat/with_number.sh] ===== 1 package main 2 func main() { 3 4 5 println("hello Golang") 6 7 } --------------------------------- 1 package main 2 func main() { 3 println("hello Golang") 4 } ===== END [basic/cat/with_number.sh] ===== DONE
TABを可視化
-T
オプションを付けるとタブを ^I
という文字にして出力してくれます。
#!/usr/bin/env bash # # cat コマンドのちょっとした Tips (タブを表示) # # REFERENCES: # - https://linuxhint.com/cat-command-linux/ # _exec() { # 作業用ディレクトリ作成 basedirpath=/tmp/try-linux/cat/display_tabchar rm -rf "${basedirpath}" mkdir -p "${basedirpath}" printf "hello\tworld\nworld\thello\n\nhello,world\n" > "${basedirpath}/hello.txt" # -T オプションを付与するとタブ文字が ^I として可視化される cat -T "${basedirpath}/hello.txt" } _exec
try-linux/display_tabchar.sh at master · devlights/try-linux · GitHub
実行すると以下のようになります。
gitpod /workspace/try-linux $ make ENTER EXAMPLE NAME: display_tabchar [INPUT ] display_tabchar [TARGET] cat_display_tabchar [SCRIPT] basic/cat/display_tabchar.sh ===== START [basic/cat/display_tabchar.sh] ===== hello^Iworld world^Ihello hello,world ===== END [basic/cat/display_tabchar.sh] ===== DONE
行末を $ で表示
-e
オプションを付けると、行末 (EOL) を $
で出力してくれます。
#!/usr/bin/env bash # # cat コマンドのちょっとした Tips (EOL(End Of Line)を表示) # # REFERENCES: # - https://linuxhint.com/cat-command-linux/ # _exec() { # 作業用ディレクトリ作成 basedirpath=/tmp/try-linux/cat/display_eol rm -rf "${basedirpath}" mkdir -p "${basedirpath}" printf "hello\tworld\nworld\thello\n\nhello,world\n" > "${basedirpath}/hello.txt" # -e オプションを付与すると行末が $ として可視化される cat -e "${basedirpath}/hello.txt" } _exec
try-linux/display_eol.sh at master · devlights/try-linux · GitHub
実行すると以下のようになります。
gitpod /workspace/try-linux $ make ENTER EXAMPLE NAME: display_eol [INPUT ] display_eol [TARGET] cat_display_eol [SCRIPT] basic/cat/display_eol.sh ===== START [basic/cat/display_eol.sh] ===== hello world$ world hello$ $ hello,world$ ===== END [basic/cat/display_eol.sh] ===== DONE
重複する空行を除去
-s
オプションを付けると、重複する空行(空行の連続)を除去して一つにしてくれます。
#!/usr/bin/env bash # # cat コマンドのちょっとした Tips (重複する空行を除去) # # REFERENCES: # - https://linuxhint.com/cat-command-linux/ # _exec() { # 作業用ディレクトリ作成 basedirpath=/tmp/try-linux/cat/omit_empty_line rm -rf "${basedirpath}" mkdir -p "${basedirpath}" cat << EOF > "${basedirpath}/app.go" package main func main() { println("hello Golang") } EOF # -s オプションをつけると重複している空行を除去してくれる # つまり、連続する空行を一行にしてくれる cat -s "${basedirpath}/app.go" } _exec
try-linux/omit_empty_lines.sh at master · devlights/try-linux · GitHub
実行すると以下のようになります。
gitpod /workspace/try-linux $ make ENTER EXAMPLE NAME: omit_empty_line [INPUT ] omit_empty_line [TARGET] cat_omit_empty_lines [SCRIPT] basic/cat/omit_empty_lines.sh ===== START [basic/cat/omit_empty_lines.sh] ===== package main func main() { println("hello Golang") } ===== END [basic/cat/omit_empty_lines.sh] ===== DONE
参考情報
過去の記事については、以下のページからご参照下さい。
- いろいろ備忘録日記まとめ
サンプルコードは、以下の場所で公開しています。
- いろいろ備忘録日記サンプルソース置き場