Converter espaços/tabs em um único caracter (um espaço ou vírgula etc) com sed

O regex abaixo remove tabs e espaços do bloco processado e substitui por , (vírgula):

sed -e "s/[[:blank:]]\{1,\}/,/g"

Como exemplo, executando o comando:

ps awxu |grep ssh

para pegar todos os processos ssh que estão rodando no servidor e mostrando o owner, o output seria parecido com o abaixo:

dklima          89242   0.4  0.1  2505164   8876 s001  S+    9:59PM   0:33.84 ssh -l root 192.168.1.10
dklima          89565   0.0  0.0  2491988   4172 s003  S+   11:43PM   0:00.31 ssh 192.168.1.11
dklima          89461   0.0  0.0  2464340   4068 s000  S+   11:29PM   0:00.13 ssh 192.168.1.12
dklima          85894   0.0  0.0  2464340   4084 s002  S+    2:31AM   0:00.03 ssh 192.168.1.10
dklima          20097   0.0  0.0  2519592   5524   ??  S    25May16   0:01.18 /usr/bin/ssh-agent -l
dklima          89746   0.0  0.0  2434840    692 s005  S+   12:30AM   0:00.00 grep ssh

Aplicando a regra do sed conforme no começo, o output seria como abaixo:

$ ps awxu |grep ssh | sed -e "s/[[:blank:]]\{1,\}/,/g"
dklima,89565,0.0,0.0,2491988,4172,s003,S+,11:43PM,0:00.31,ssh,192.168.1.11
dklima,89461,0.0,0.0,2464340,4068,s000,S+,11:29PM,0:00.13,ssh,192.168.1.12
dklima,89242,0.0,0.1,2505164,8876,s001,S+,9:59PM,0:34.00,ssh,-l,root,192.168.1.10
dklima,85894,0.0,0.0,2464340,4084,s002,S+,2:31AM,0:00.03,ssh,192.168.1.10
dklima,20097,0.0,0.0,2519592,5524,??,S,25May16,0:01.18,/usr/bin/ssh-agent,-l

Repare que a coluna que corresponde aos comandos sendo executados também apresentam a , (vírgula), se não é desejável esse output, execute o sed abaixo:

sed -e "s/[[:blank:]]\{2,\}/,/g"

Com isso a coluna referente aos comandos será preservada, conforme abaixo:

$ ps awxu |grep ssh | sed -e "s/[[:blank:]]\{2,\}/,/g"
dklima,89565,0.0,0.0,2491988,4172 s003,S+,11:43PM,0:00.31 ssh 192.168.1.11
dklima,89461,0.0,0.0,2464340,4068 s000,S+,11:29PM,0:00.13 ssh 192.168.1.12
dklima,89242,0.0,0.1,2505164,8876 s001,S+,9:59PM,0:34.04 ssh -l root 192.168.1.10
dklima,85894,0.0,0.0,2464340,4084 s002,S+,2:31AM,0:00.03 ssh 192.168.1.10
dklima,20097,0.0,0.0,2518544,5508,??,S,25May16,0:01.18 /usr/bin/ssh-agent -l
dklima,89755,0.0,0.0,2434840,692 s005,S+,12:34AM,0:00.00 grep ssh
Share this post

Join the conversation