LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Delete everything until specific symbol - sed (https://www.linuxquestions.org/questions/programming-9/delete-everything-until-specific-symbol-sed-4175631054/)

pedropt 06-02-2018 03:54 PM

Delete everything until specific symbol - sed
 
Hi everyone , when it turns to sed and to delete some symbols i have a lot of difficulties to find a proper solution .

Here it is what i have :
Quote:

erjreglkrglkregerlkghreldsoigslçkgje[34423dfewkfhw]
and this is what i need as output :

Quote:

34423dfewkfhw
How can i do it with sed ?

RandomTroll 06-02-2018 04:23 PM

I can't guess your general problem, but
Code:

echo $string | cut -d\[ -f2 | cut -d\] -f1
will work on the example string.

scasey 06-02-2018 05:21 PM

What have you tried?

Because the special characters [ and ] have meaning to sed, they must be escaped with \ to be taken literally.

Code:

sed 's/.*\[//;s/\]//'

pedropt 06-02-2018 05:38 PM

Thanks Scasey , your sentence works like a charm .

However , what can i do to delete everything ahead the last symbol ] ?

in this case :

Quote:

erjreglkrglkregerlkghreldsoigslçkgje[34423dfewkfhw]:dwfwe982350
Sed is not easy to work on some regex because like you told before , it will interpet them as a command .

syg00 06-02-2018 05:47 PM

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.

scasey 06-02-2018 05:49 PM

Sorry, you're really going to have to show some work.
Do you understand what I said about escaping special characters?
Have you read
Code:

man sed
sed info
man perlre

?
Have you searched for "regex syntax" on the web?

pedropt 06-02-2018 06:12 PM

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 ('\').

keefaz 06-02-2018 06:38 PM

or
Code:

sed 's/.*\[\(.*\)].*/\1/' file
grep -Po '\[\K.*(?=])' file


syg00 06-02-2018 08:26 PM

Better hope there's only one set of square brackets. And matched.
Potential corner cases abound.

We would all hope logs are consistent, but ya never know ...

MadeInGermany 06-03-2018 04:18 AM

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 ]


All times are GMT -5. The time now is 11:30 PM.