0

i have this file:

Tue Nov 11 10:32:10 2014        172.18.0.240    csotelo tty1    172.18.1.131    stop    task_id=88      timezone=UTC    service=shell   priv-lvl=15     cmd=configure terminal <cr>

I used that command:

sed -e 's/\s\+/,/g' input.txt > output.txt

result from output.txt:

Tue,Nov,11,10:32:10,2014,172.18.0.240,csotelo,tty1,172.18.1.131,stop,task_id=88,timezone=UTC,service=shell,priv-lvl=15,cmd=configure,terminal,<cr>

Desired Output:

Tue Nov 11 10:32:10 2014,172.18.0.240,csotelo,tty1,172.18.1.131,stop,task_id=88,timezone=UTC,service=shell,priv-lvl=15,cmd=configure terminal <cr>

Can anyone please help me?

Gilles Quénot
  • 173,512
  • 41
  • 224
  • 223
  • You have to be more specific in your REGEX ! – Gilles Quénot Nov 12 '14 at 17:52
  • 1
    you could first replace one space with "something", then replace multispaces, then replace "something" with one space. (or use `cmd=` as right "bound" when doing your `s///`) –  Nov 12 '14 at 17:52
  • [What is the purpose?](http://mywiki.wooledge.org/XyProblem) Tab separated files are easier to work with in code, and tools like LibreOffice Calc and Excel understand tab separated files just fine. – l0b0 Nov 12 '14 at 18:40
  • @l0b0: while this is a true statement it's irrelevant to the OPs question; syslog files don't usually have TABs in them. – tink Nov 12 '14 at 20:44

2 Answers2

0

It's not that simple because you want to replace multiple spaces in some places (like between the date and IP) but also replace a single space elsewhere (between "csotelo" and "tty1"). So a simple pattern will not be enough, you need something that can tell these cases apart. An example using awk could be something like:

awk '{print $1 " " $2 " " $3 " " $4 " " $5 "," $6 ...}'

where ... should continue outputting fields separated by spaces or commas depneding on what you want to do in each particular case until the last field. What this does is to split the line on whitespace and then output one element after the other, separated by space or comma.

Michał Kosmulski
  • 9,855
  • 1
  • 32
  • 51
0

Here you go:

sed -e 's/\s\{2,\}/,/g' input.txt > output.txt

Your regex uses +, which means "one or more", but you don't want to substitute single spaces.

  • {2,} means "between two and infinite" or "two or more".
  • {2} means "exactly two".
  • {2,5} means "between two and five".
  • {,5} means "between zero and five" or "as many as five".
gdrooid
  • 158
  • 5