LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 06-19-2014, 01:58 PM   #1
BeachHead
LQ Newbie
 
Registered: May 2012
Location: Germany
Distribution: Arch, AOSP
Posts: 24

Rep: Reputation: 2
sed - line processing depending on previous line match


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.
.
Attached Files
File Type: txt logcat.txt (2.4 KB, 10 views)

Last edited by BeachHead; 06-19-2014 at 02:01 PM.
 
Old 06-19-2014, 08:21 PM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,996

Rep: Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187
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
 
Old 06-19-2014, 09:27 PM   #3
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,099

Rep: Reputation: 4117Reputation: 4117Reputation: 4117Reputation: 4117Reputation: 4117Reputation: 4117Reputation: 4117Reputation: 4117Reputation: 4117Reputation: 4117Reputation: 4117
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
sed multiple lines previous line gushnik1 Linux - Software 2 10-09-2013 03:15 AM
[SOLVED] Adding a character in each line depending upon number of such characters present in a line. ls_milkyway Linux - Newbie 13 08-24-2013 03:19 AM
[SOLVED] SED replace line after match r00t Linux - Newbie 12 05-31-2012 07:17 AM
Insert line on match only once with sed? lhouk Programming 38 08-10-2010 09:23 AM
grep/sed/awk - find match, then match on next line gctaylor1 Programming 3 07-11-2007 08:55 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 02:27 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration