LinuxQuestions.org
Help answer threads with 0 replies.
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 06-02-2018, 03:54 PM   #1
pedropt
Member
 
Registered: Aug 2014
Distribution: Devuan
Posts: 345

Rep: Reputation: Disabled
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 ?
 
Old 06-02-2018, 04:23 PM   #2
RandomTroll
Senior Member
 
Registered: Mar 2010
Distribution: Slackware
Posts: 1,953

Rep: Reputation: 270Reputation: 270Reputation: 270
I can't guess your general problem, but
Code:
 echo $string | cut -d\[ -f2 | cut -d\] -f1
will work on the example string.
 
Old 06-02-2018, 05:21 PM   #3
scasey
LQ Veteran
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.9.2009
Posts: 5,723

Rep: Reputation: 2210Reputation: 2210Reputation: 2210Reputation: 2210Reputation: 2210Reputation: 2210Reputation: 2210Reputation: 2210Reputation: 2210Reputation: 2210Reputation: 2210
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/\]//'
 
Old 06-02-2018, 05:38 PM   #4
pedropt
Member
 
Registered: Aug 2014
Distribution: Devuan
Posts: 345

Original Poster
Rep: Reputation: Disabled
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 .
 
Old 06-02-2018, 05:47 PM   #5
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,119

Rep: Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120
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.
 
2 members found this post helpful.
Old 06-02-2018, 05:49 PM   #6
scasey
LQ Veteran
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.9.2009
Posts: 5,723

Rep: Reputation: 2210Reputation: 2210Reputation: 2210Reputation: 2210Reputation: 2210Reputation: 2210Reputation: 2210Reputation: 2210Reputation: 2210Reputation: 2210Reputation: 2210
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?
 
Old 06-02-2018, 06:12 PM   #7
pedropt
Member
 
Registered: Aug 2014
Distribution: Devuan
Posts: 345

Original Poster
Rep: Reputation: Disabled
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 ('\').
 
Old 06-02-2018, 06:38 PM   #8
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,552

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
or
Code:
sed 's/.*\[\(.*\)].*/\1/' file
grep -Po '\[\K.*(?=])' file
 
Old 06-02-2018, 08:26 PM   #9
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,119

Rep: Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120
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 ...
 
Old 06-03-2018, 04:18 AM   #10
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,780

Rep: Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198
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 .*
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
[SOLVED] Multipal line edited using sed, how to make sed specific coolpraz Programming 4 01-05-2013 01:14 PM
sed replacing a specific character with a specific number ieatbunnies Linux - Newbie 2 11-04-2010 10:14 AM
sed including ± symbol Alkass Programming 4 04-22-2010 02:59 AM
Delete specific Range of lines Using sed , awk, grep etc. joyds219 Linux - Newbie 4 03-28-2008 08:59 AM
insert a symbol with sed tonton Programming 5 08-31-2004 11:33 AM

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

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

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