LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-04-2010, 10:07 AM   #1
danifunny
LQ Newbie
 
Registered: Nov 2010
Posts: 3

Rep: Reputation: 0
Bash - add Pattern in Front of Regex


Hello Together, I have following Problem:

A Java Program generates me some output that I pipe to a logfile. Now I want to add a Pattern (e.g. 'important') in Front of every Pattern in de Logfile that matches a specified regex.
But neither with sed then with awk I got to a working solution.

Here some ouput from the logfile:

04-Nov-2010 15:20:29 INFO DXReceive.main File "/myDirectory" umbenannt auf /myDirectory 04-Nov-2010 15:20:29 INFO DXReceive.main ... done DXReceive dxreceive.sh /myDirectory /myDirectory erfolgreich

And this should be the result:

important 04-Nov-2010 15:20:29 INFO DXReceive.main File "/myDirectory" umbenannt auf /myDirectory important 04-Nov-2010 15:20:29 INFO DXReceive.main ... done DXReceive dxreceive.sh /myDirectory /myDirectory erfolgreich

A working regex to get to the dates is [0-9][0-9]-[A-Z][a-z][a-z]-[0-9][0-9][0-9][0-9]

I hope you unterstand my Problem and I thank you for your help.

greetings
danifunny

(sorry for my bad english, isn't my native language)
 
Old 11-04-2010, 10:18 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

When does this pattern need to be added? The given regexp is true for every date, thus you will always add the pattern (assuming that all the logged lines start with a date). In that case you do not need to use the regexp.

Could you be more specific?
 
Old 11-04-2010, 10:25 AM   #3
danifunny
LQ Newbie
 
Registered: Nov 2010
Posts: 3

Original Poster
Rep: Reputation: 0
Hi

that's just the knicking point, the dates aren't always at the beginning of the line.
What I want is to say a simple find and replace. I want to find a regex and insert a pattern in front of de regex.

e.g.:
the regex should find dates and insert the pattern in front of the regex. for this example, the pattern is 'important'

Logfile before skript starts:
lorem ipsum 05-Dec-2009 thanks for help 09-Feb-2008 I am thankful for 06-Mar-2010 this forum

Logfile after skript finished:
lorem ipsum important05-Dec-2009 thanks for help important09-Feb-2008 I am thankful for important06-Mar-2010 this forum



I hope this made it clearer for you

thanks
 
Old 11-04-2010, 10:28 AM   #4
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

Ok, clear.

sed 's/\([0-9][0-9]-[A-Z][a-z][a-z]-[0-9][0-9][0-9][0-9]\)/IMPORTANT \1/g' infile

This uses back-referencing. Everything between \( and \) in the search part can be represented by \1 (and \2 \3 if you use more of them) in the replace part.

Hope this helps.
 
1 members found this post helpful.
Old 11-04-2010, 10:38 AM   #5
danifunny
LQ Newbie
 
Registered: Nov 2010
Posts: 3

Original Poster
Rep: Reputation: 0
Tested and works!
Thank you a lot, and again learned something very useful .
 
Old 11-04-2010, 10:46 AM   #6
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
You're welcome
 
  


Reply



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
Help with sed regex to match words via a pattern. logar0 Linux - Newbie 3 10-24-2010 04:33 PM
[SOLVED] /bin/bash if statement pattern search, end of pattern special character? headhunter_unit23 Programming 3 04-29-2010 08:05 AM
Shell regex - add new field czezz Programming 3 02-28-2009 03:13 PM
Pattern matching in a bash case statement using regex ciphyre Programming 1 01-31-2009 12:20 PM
Replacing a image filename with a regex pattern in a script jonaster Linux - General 1 09-05-2006 07:02 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:37 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