ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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)
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.
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
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.