0

I have a log file in SUN Solaris 9 system, like this:

2011-06-16 18:30:59 abc 2011-06-16 18:31:00 def 2011-06-16 18:35:21 ghi 2011-06-16 18:40:15 jkl

And i want cut this string in separate line like this:

2011-06-16 18:30:59 abc
2011-06-16 18:31:00 def
2011-06-16 18:35:21 ghi
2011-06-16 18:40:15 jkl

I have other control character, because this file made for reading in a special log viewer. This character repeated in each same text string in this file (before date & time). I want to delete this character too.

  • 1
    Is this output straight from `cat /var/log/logfile` (and not first captured in a variable or run through some other process)? Have you checked whether it's delimited by NUL bytes or other control characters that don't show up on terminals? – that other guy May 15 '14 at 05:08
  • @thatotherguy, You are right, i have other control character. because this file made for reading in a special log viewer. this character repeated in each same text string in this file (before date). i want to delete this character too. – user3121138 Aug 14 '15 at 13:49
  • Repeat previous comment to my ask too. – user3121138 Aug 14 '15 at 13:54
  • You should break the lines on that character then. For example, if it was octal character 002, you can use `tr '\002' '\n' < /var/log/logfile` (002 is just an example, use a hex editor or similar to find out what your file uses). – that other guy Aug 14 '15 at 16:06

4 Answers4

0

Perhaps this may help:

sed  's/[0-9]\{4\}-[0-9]\{2\}-[0-9]\{2\} [0-9]\{2\}:[0-9]\{2\}:[0-9]\{2\}/\n&/g'

$ echo "2011-06-16 18:30:59 abc 2011-06-16 18:31:00 def 2011-06-16 18:35:21 ghi 2011-06-16 18:40:15 jkl" | sed  's/[0-9]\{4\}-[0-9]\{2\}-[0-9]\{2\} [0-9]\{2\}:[0-9]\{2\}:[0-9]\{2\}/\n&/g'

2011-06-16 18:30:59 abc
2011-06-16 18:31:00 def
2011-06-16 18:35:21 ghi
2011-06-16 18:40:15 jkl
anishsane
  • 20,270
  • 5
  • 40
  • 73
0
#!/bin/bash

while read -r -a arr
do
  len=${#arr[@]}
  for ((i=0; i < len; i+=3))
  do
    echo ${arr[@]:$i:3}
  done
done < logfile

Output:

2011-06-16 18:30:59 abc
2011-06-16 18:31:00 def
2011-06-16 18:35:21 ghi
2011-06-16 18:40:15 jkl
a5hk
  • 7,532
  • 3
  • 26
  • 40
0

how about this

xargs -n3 < file
BMW
  • 42,880
  • 12
  • 99
  • 116
0

Try this simple sed command,

sed 's/ \([0-9]\{4\}\)/\n\1/g' file

Example:

$ echo '2011-06-16 18:30:59 abc 2011-06-16 18:31:00 def 2011-06-16 18:35:21 ghi 2011-06-16 18:40:15 jkl' | sed 's/ \([0-9]\{4\}\)/\n\1/g'
2011-06-16 18:30:59 abc
2011-06-16 18:31:00 def
2011-06-16 18:35:21 ghi
2011-06-16 18:40:15 jkl

This code first search for a space followed by 4 digit number. If it found any, then it will be replaced by a newline character followed by the same four digit number.

Avinash Raj
  • 172,303
  • 28
  • 230
  • 274