LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   sed - line processing depending on previous line match (https://www.linuxquestions.org/questions/linux-newbie-8/sed-line-processing-depending-on-previous-line-match-4175508526/)

BeachHead 06-19-2014 01:58 PM

sed - line processing depending on previous line match
 
1 Attachment(s)
Hallo,
i'm trying to convert a colored Android system log (logcat with ANSI escape sequences) to vbulletin color codes. My first script looks like this and works so far but is not optimal due to duplicates.
Code:

sed '/\x1b/ {
        s/\x1b\[0m\x1b\[38;5;40m/\[COLOR="Green"\]/
        s/\x1b\[0m\x1b\[38;5;196m/\[COLOR="Red"\]/
        s/\x1b\[0m\x1b\[38;5;75m/\[COLOR="Blue"\]/
        s/\x1b\[0m\x1b\[38;5;166m/\[COLOR="Sienna"\]/
        s/\x1b\[0m\x1b\[38;5;231m/\[COLOR="DarkSlateGray"\]/
        s/\x1b\[38;5;40m/\[COLOR="Green"\]/
        s/\x1b\[38;5;196m/\[COLOR="Red"\]/
        s/\x1b\[38;5;75m/\[COLOR="Blue"\]/
        s/\x1b\[38;5;166m/\[COLOR="Sienna"\]/
        s/\x1b\[38;5;231m/\[COLOR="DarkSlateGray"\]/
        s/\x1b\[0m/\[COLOR="Black"\]/
        s/$/\[\/COLOR\]/
        }' <logcat.txt >logcat2.txt

It produces this output:
PHP Code:

[COLOR="Blue"]D/dalvikvm(10103): VFYreplacing opcode 0x70 at 0x0002[/COLOR]
[
COLOR="Blue"]D/dalvikvm(10103): JIT code cache reset in 0 ms (0 bytes 2/0)[/COLOR

But it should rather look like this:
PHP Code:

[COLOR="Blue"]D/dalvikvm(10103): VFYreplacing opcode 0x70 at 0x0002
D
/dalvikvm(10103): JIT code cache reset in 0 ms (0 bytes 2/0)[/COLOR

Now i'm wondering what's the best method to accomplish that optimization. Is there a text processor already that can process multilines with hold&replace pattern match? Or should i do it manually with bash (conditional loops, previous pattern pushed into variables...)?

Any ideas?

Example logcat is attached.
.

grail 06-19-2014 08:21 PM

Well if it can be done I am sure one of the sed-ninjas will jump in, but for such task I would probably use awk, ruby or perl as sed, I find, is a bit clumsy in the method to save data while processing
the next line and so on

syg00 06-19-2014 09:27 PM

My first reaction is "why do you care" ?.
If the possibility exists for long runs of the same colour, I would post-process to get rid of the "duplicates" - much easier than holding several/many lines until a mismatch. I'd probably use awk, but almost anything will do - post-process should only need to manage 2 lines at a time.


All times are GMT -5. The time now is 07:59 PM.