LoginSignup
10
10

More than 5 years have passed since last update.

改行をスペースに置換して一行にする

Posted at

sedでは改行を含む文字列を一行に結合するのは結構面倒臭い。引数無しのxargsにパイプで複数行の文字列を渡すと、タブや改行がすべてスペースに置き換わった一行のテキストが得られる。

$ ps 
  PID TTY           TIME CMD
  277 ttys000    0:00.28 -zsh
  746 ttys000    0:00.01 tmux
  747 ttys000    0:00.00 (tmux)
  749 ttys001    0:01.38 -zsh
26028 ttys002    0:02.13 -zsh
17193 ttys003    0:00.10 -zsh
17203 ttys004    0:00.06 -zsh
19801 ttys005    0:00.60 -zsh
 $ ps | xargs
PID TTY TIME CMD 277 ttys000 0:00.28 -zsh 746 ttys000 0:00.01 tmux 747 ttys000 0:00.00 (tmux) 749 ttys001 0:01.38 -zsh 26028 ttys002 0:02.15 -zsh 36213 ttys002 0:00.00 xargs 17193 ttys003 0:00.10 -zsh 17203 ttys004 0:00.06 -zsh 19801 ttys005 0:00.60 -zsh
10
10
1

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