views:

48

answers:

5

Hey there!!

Firstly, I'm really new on this and I'm having difficulties...

I have this data:

[05/Apr/2010:09:59:34 -0300] /~bcc/topo-zero.html 200 238

         2

[05/Apr/2010:10:01:19 -0300] /~bsi/materiais/ed/u6.html 200 286960

         3

[05/Apr/2010:10:04:56 -0300] /~firedo/AISG/AISGroupContributions.html 200 33193

         2

[05/Apr/2010:10:08:33 -0300] /~bcc/topo-zero.html 200 238
         2

And I want that to become on this:

[05/Apr/2010:09:59:34 -0300] /~bcc/topo-zero.html 200 238 2

[05/Apr/2010:10:01:19 -0300] /~bsi/materiais/ed/u6.html 200 286960 3

[05/Apr/2010:10:04:56 -0300] /~firedo/AISG/AISGroupContributions.html 200 33193 2

[05/Apr/2010:10:08:33 -0300] /~bcc/topo-zero.html 200 238 2

I'd appreciate if someone could give me a solution for that using a linux command...

A: 

cat blabla | grep 0300 | cut -f1,2,3,4,5

tojas
That doesn't combine the log entry with the number on its own line, though, which is kind of the point of this question.
David Zaslavsky
Sorry, but it haven't changed anything !
Alucard
A: 

The standard UNIX tools (e.g. sed) are kind of deficient in their handling of newlines, in my opinion. They're designed to work line by line, and it can be kind of hard to do things like mushing together different lines of input into one line of output. Maybe someone with more sed experience than I have could do it... but I'd suggest trying Perl instead, if you have access to it.

perl -e 'while (<>) { length || cont; chomp; s/^\s+(\d+)$/ \1\n/; print; }' inputfile.txt

If you want to retain an extra blank line between records, exactly as you have it formatted in your question, just change the \n to \n\n (to print two newlines after each log record, instead of one).

David Zaslavsky
Sorry, but it haven't changed anything...
Alucard
The program definitely works - I copied and pasted the contents of the log file from your question and ran that on it, and got exactly the output you were looking for. So what did you do to run it and check the output?
David Zaslavsky
A: 

I always find these little scripts easier to understand if I put them in a file with formatting. The following awk script seems to produce the desired output. This awk script simply looks for lines that start with a square bracket and also have a closing bracket. When it encounters a line with a single digit, it prints the saved line with the digit.

/^\[.*\]/{
      p = $0;
}

/^ *[0-9] *$/ {
   printf( "%s %s\n\n", p, $1 );
}

If you put the above in a file such as tmp.awk, then this command should produce the example output:

awk -f tmp.awk origfile.txt > newfile.txt
Mark Wilkins
A: 
 cat data | tr -s ' \n' | sed 'N;s/\n//g'

That is, pass the file data through tr squeezing multiple spaces and newlines. The result of that is passed through sed, combining alternating lines with the new line character between them (N command) and then removing those new line characters (s command)

Bryan Ash
`N` actually appends the Next line rather than inserting a newline (the newline just comes along for the ride).
Dennis Williamson
Dennis: thanks for the comment. Is the edit clearer?
Bryan Ash
A: 
$ awk 'BEGIN{RS="[";FS="\n"}{$1=$1}NR>1{print "["$0}' OFS="" file
[05/Apr/2010:09:59:34 -0300] /~bcc/topo-zero.html 200 238         2
[05/Apr/2010:10:01:19 -0300] /~bsi/materiais/ed/u6.html 200 286960         3
[05/Apr/2010:10:04:56 -0300] /~firedo/AISG/AISGroupContributions.html 200 33193         2
[05/Apr/2010:10:08:33 -0300] /~bcc/topo-zero.html 200 238         2
ghostdog74
...except for all the extra whitespace. Change the field separator to this: `FS="[\n ]+"`
Dennis Williamson