[SOLVED] Delete everything until specific symbol - sed
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.
Nobody can write matching regex if you keep changing the data and requirements. You need to fully define the situation before you can start with the regex. For simple regex to work it needs consistent data - all the records must conform to a pattern.
You still haven't shown any evidence you'd made any effort yourself.
Sorry , this is a specific script to check up the server logs , however after your filter i get : next in the lines , and i already figure out how to do it .
Code:
sed 's/.*\[//;s/\]//' <file | sed 's/\:.*$//'
Thanks for your help on this one , and you were right .
And yes , after checking sed manua i get :
'/REGEXP/'
This will select any line which matches the regular expression
REGEXP. If REGEXP itself includes any '/' characters, each must be
escaped by a backslash ('\').
You can simply include all trailing characters in the search. What matches is substituted
Code:
sed 's/.*\[//;s/\].*//' file
Here sed opens file itself. While <file lets the shell open it.
The .* is greedy, so the rightmost [ is found.
Sometimes you have two [ and want the first [ then look for "not [" characters before the [
Code:
sed 's/[^[]*\[//;s/\].*//' file
The same for the variant with back-reference
Code:
sed 's/[^[]*\[\([^]]*\)].*/\1/' file
It looks for "not [" characters before the [ then "not ]" characters befor the ]
Last edited by MadeInGermany; 06-04-2018 at 03:43 AM.
Reason: fixed misplaced .*
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.