LoginSignup
3
3

More than 5 years have passed since last update.

シェルに詳しくない人はRuby でワンライナー

Posted at

シェルに詳しくないけど, ターミナルでゴニョゴニョしたい.
何かの言語でワンライナーしてみたらいいかも.

基本

-e オプションをつけて実行すると 引数の文字列の部分が実行される

$ ruby -e 'puts "hello"'

応用

-n

-n オプションをつけると while getsend で囲った状態になる
$_ に入力された値が入る

ruby -ne  'puts $_'

cat test.txt | ruby -ne 'puts $_.count'

-p

-n オプションをつけると while getsprint$_; end で囲った状態になる
$_ を変更したりするといい

ruby -pe '$_.concat(".csv")' 

例 (hellow => hello への置換)

cat text.txt | ruby -pe '$_.gsub!(/hellow/, "hello")'

-a

-a オプションをつけると $F$_.split が入った状態になる
$FArray

ruby -ane '$F[0]' 

例 (python のプロセス全キル)

# プロセス一覧 | python のものを抜き出し | プロセスID 取り出し        | キル
ps -ax      | grep python         | ruby -ane 'puts $F[0]' | xargs kill -9

最後に

Unixコマンドに詳しくなくてもとりあえずこれさえあればなんとかなるきがする

別に Ruby で無くてもいい

3
3
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
3
3