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:
Quote:
|
I can't guess your general problem, but
Code:
echo $string | cut -d\[ -f2 | cut -d\] -f1 |
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/\]//' |
Thanks Scasey , your sentence works like a charm .
However , what can i do to delete everything ahead the last symbol ] ? in this case : Quote:
|
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, 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 Have you searched for "regex syntax" on the web? |
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/\:.*$//' 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 ('\'). |
or
Code:
sed 's/.*\[\(.*\)].*/\1/' file |
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 ... |
You can simply include all trailing characters in the search. What matches is substituted
Code:
sed 's/.*\[//;s/\].*//' file 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 Code:
sed 's/[^[]*\[\([^]]*\)].*/\1/' file |
All times are GMT -5. The time now is 11:30 PM. |