LoginSignup
0
0

More than 1 year has passed since last update.

改行(\n)とカンマ(,)の置き換えをコマンドで

Posted at
$ cat ports.txt
22
25
80
9929
12345
31337

awkで可能

$ cat ports.txt | awk '{print $1}' ORS=','
22,25,80,9929,12345,31337,

awkの方が、一度に複数の処理ができる:

$ cat results.txt
22/tcp
25/tcp
80/tcp
9929/tcp
12345/tcp
31337/tcp

$ cat results.txt | awk -F '/' '{print $1}' ORS=','
22,25,80,9929,12345,31337,

このカンマ以外でも置き換え可能:

$ cat results.txt | awk -F '/' '{print $1}' ORS='test'
22test25test80test9929test12345test31337test

今まではtr使ってた

$ cat ports.txt | tr '\n' ','
22,25,80,9929,12345,31337,

awkの方が一度で色々処理できるから楽だな。

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