文字間抽出のためのシェルスクリプト

文字間抽出のためのシェルスクリプト

以下のように、キーワード「/」と「NET」の間のファイルからユーザー名を抽出したいと思います。これを行うにはどうすればよいですか?または同様のプログラムを使用awkできますsedcut

から:

audit: command=true   rsa1/[email protected] running
audit: command=true   user2/[email protected] executing

到着する:

audit: command=true   rsa1 running
audit: command=true   user2 executing

ベストアンサー1

使用sed:

< inputfile sed 's/\/.*NET//' > outputfile

sedローカルで使用:

sed -i.bak 's/\/.*NET//' inputfile

コマンド#1失敗:

  • < inputfile:コンテンツを「s」inputfileにリダイレクトします。sedstdin
  • > outputfilesed:コンテンツをstdout次にリダイレクトします。outputfile

コマンド#2分割:

  • -i.baksedバックアップファイルを強制生成し、inputfile.bakその場所で編集します。inputfile
  • inputfile: 強制sed読み取り入力inputfile

正規表現の分解:

  • s: 置換を実行するためのアサーション
  • /: 検索モード開始
  • \//文字と一致します。
  • .*NETNET: 文字列が終了する前のすべての文字と一致します。
  • /:検索モードを停止/交換モードを開始
  • /: 交換モードの停止

出力例:

~/tmp$ cat inputfile
audit: command=true   rsa1/[email protected] running
audit: command=true   user2/[email protected] executing
~/tmp$ < inputfile sed 's/\/.*NET//' > outputfile
~/tmp$ cat outputfile 
audit: command=true   rsa1 running
audit: command=true   user2 executing
~/tmp$ sed -i.bak 's/\/.*NET//' inputfile
~/tmp$ cat inputfile.bak
audit: command=true   rsa1/[email protected] running
audit: command=true   user2/[email protected] executing
~/tmp$ cat inputfile
audit: command=true   rsa1 running
audit: command=true   user2 executing
~/tmp$ 

おすすめ記事