LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 11-23-2011, 06:08 PM   #1
kmkocot
Member
 
Registered: Dec 2007
Location: Queensland, Australia
Posts: 122

Rep: Reputation: 15
.\+ in sed: What am I doing wrong?


Hi all,

Sorry for the vague subject but I think this will be straightforward for most.

I have a series of text files like this:
Code:
(CBUL@contig00203_:0.04893644579804502098,SGIG@Contig10899_:0.06027414795007355469)
I want to delete everything between each at symbol (@) and the next colon ( so that the output looks like this:
Code:
(CBUL@:0.04893644579804502098,SGIG@:0.06027414795007355469)
I tried the following sed command:
Code:
sed 's/@.\+:/@/g' *.tre
But it deletes everything between the first at symbol and the last colon:
Code:
(CBUL@0.06027414795007355469)
I guessed that I need to replace the final /g with another flag but I can't figure out what that would be (/1 doesn't work).

Any suggestions would be greatly appreciated!

Thanks,
Kevin
 
Old 11-23-2011, 06:38 PM   #2
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 15,809

Rep: Reputation: 2168Reputation: 2168Reputation: 2168Reputation: 2168Reputation: 2168Reputation: 2168Reputation: 2168Reputation: 2168Reputation: 2168Reputation: 2168Reputation: 2168
You don't need the global (or any) flag at all.
Your problem is that regex is generally "greedy" - it goes to the end and works back. There are ways to make it non-greedy, but I generally prefer to define what I (don't) want exactly.
Instead of ".+" you need all characters that are not a ":" ... see how you go figuring that out.

BTW, you also don't need t substitute a char back, you can just define the string you don't want and use "//" on the RHS.

Last edited by syg00; 11-23-2011 at 06:40 PM. Reason: typos
 
Old 11-23-2011, 06:48 PM   #3
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,823
Blog Entries: 1

Rep: Reputation: 1218Reputation: 1218Reputation: 1218Reputation: 1218Reputation: 1218Reputation: 1218Reputation: 1218Reputation: 1218Reputation: 1218
Code:
sed 's/@[^:]*/@/' file
As mentioned above, you probably overcomplicated it
 
Old 11-24-2011, 01:32 PM   #4
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960
In addition, in sed and grep, "+" is considered an extended regex operator, whereas in basic regex mode it's just another character. But as a gnu extension, if you prefix it with a "\", then you are actually enabling its extended meaning, and it becomes the "one or more" repeat operator.

So in basic mode, ".\+" means "a string of any characters until the end of the line". For a literal plus sign, just use ".+".

However, if you turn on extended mode with -r (or -E in grep), then the above meanings are reversed. ".\+" becomes a literal plus sign, and ".+" becomes a continuous string of characters.

The man and info pages for grep have a more thorough discussion on the difference between basic and extended modes.

Last edited by David the H.; 11-24-2011 at 01:35 PM. Reason: minor rewording
 
Old 07-09-2013, 11:11 AM   #5
kmkocot
Member
 
Registered: Dec 2007
Location: Queensland, Australia
Posts: 122

Original Poster
Rep: Reputation: 15
Hi all,

Sorry, I am still stuck on this. sycamorex's suggestion had no effect on the example input file I gave (at least with the version of sed I'm using?) and I can't figure out how to modify it to make it work. syg00, I get what you are saying but I don't know how to implement it without having the same greedy algorithm-related problems.

Thanks,
Kevin
 
Old 07-09-2013, 02:33 PM   #6
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,529

Rep: Reputation: 2896Reputation: 2896Reputation: 2896Reputation: 2896Reputation: 2896Reputation: 2896Reputation: 2896Reputation: 2896Reputation: 2896Reputation: 2896Reputation: 2896
Well firstly, the 'g' at the end will be needed as the pattern appears more than once in the line and all are to be removed.

@OP - you have mentioned that your version is not having the desired result, may I ask if testing like below works:
Code:
echo '(CBUL@contig00203_:0.04893644579804502098,SGIG@Contig10899_:0.06027414795007355469)' | sed 's/@[^:]*/@/g'
If it does, then the issue is not using the -i option to make your changes to the actual file.

If not, it may help if you tell us which version you are using so we can help more?
 
1 members found this post helpful.
  


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] sed 's/Tb05.5K5.100/Tb229/' alone but doesn't work in sed file w/ other expressions Radha.jg Programming 6 03-03-2011 07:59 AM
sed: how to change MAC address string with sed cold Linux - Software 5 08-02-2010 07:43 AM
What is wrong with the "sed"? dynaguy Linux - General 6 05-31-2010 05:50 PM
sed command error - what am i doing wrong? Morrighan Linux - Newbie 8 06-15-2008 11:12 AM
Insert character into a line with sed? & variables in sed? jago25_98 Programming 5 03-11-2004 06:12 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 10:52 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration