LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
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 03-12-2015, 10:09 AM   #1
abefroman
Senior Member
 
Registered: Feb 2004
Location: lost+found
Distribution: CentOS
Posts: 1,430

Rep: Reputation: 55
How can I handle a , in sed?


How can I get the following:
Code:
 echo '"Im a test",dont remote this,"another test",dont remove this too' | sed 's/".*"//g'
to print:
Code:
dont remote this,dont remove this too
In other words I want to replace everything in quotes, including an optional , with blank. Also it should read 2 strings of quotes: "Im a test" and "another test" rather than one long string: "Im a test",dont remote this,"another test"

TIA
 
Old 03-12-2015, 10:15 AM   #2
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 22,022

Rep: Reputation: 7343Reputation: 7343Reputation: 7343Reputation: 7343Reputation: 7343Reputation: 7343Reputation: 7343Reputation: 7343Reputation: 7343Reputation: 7343Reputation: 7343
, is a regular char, you can use it as is. What is the problem with it? Did you try some scripts already?
 
Old 03-12-2015, 10:17 AM   #3
firstfire
Member
 
Registered: Mar 2006
Location: Ekaterinburg, Russia
Distribution: Debian, Ubuntu
Posts: 709

Rep: Reputation: 428Reputation: 428Reputation: 428Reputation: 428Reputation: 428
Code:
$ echo '"Im a test",dont remote this,"another test",dont remove this too' | sed 's/"[^"]*",\?//g'
dont remote this,dont remove this too
 
1 members found this post helpful.
Old 03-12-2015, 10:31 AM   #4
abefroman
Senior Member
 
Registered: Feb 2004
Location: lost+found
Distribution: CentOS
Posts: 1,430

Original Poster
Rep: Reputation: 55
Quote:
Originally Posted by firstfire View Post
Code:
$ echo '"Im a test",dont remote this,"another test",dont remove this too' | sed 's/"[^"]*",\?//g'
dont remote this,dont remove this too
Thanks!! I didn't know you need to escape the question mark.

Also what does this part mean: [^"] (start of string or quote?)
 
Old 03-13-2015, 12:23 AM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,011

Rep: Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194
Firstly, why you got your result, * is greedy bey default, hence it looks for the largest match possible to your expression, so ".*" matches "Im a test",dont remote this,"another test". You confirm this with a simple grep
with colouring turned on:
Code:
$ echo '"Im a test",dont remote this,"another test",dont remove this too' | grep '".*"'
"Im a test",dont remote this,"another test",dont remove this too
As for [^"], ^ means the start of the line when not escaped or in square brackets. When the first item in square brackets it negates anything else included in them, so in this case it says return everything that is not "

The reason for escaping the ? is so sed does not interpret it as a ? but so it says zero or one of the preceding character, ie. the comma (,). Alternative you can remove the escape if you use the -r option for sed.
 
1 members found this post helpful.
Old 03-13-2015, 09:59 AM   #6
abefroman
Senior Member
 
Registered: Feb 2004
Location: lost+found
Distribution: CentOS
Posts: 1,430

Original Poster
Rep: Reputation: 55
Quote:
Originally Posted by grail View Post
As for [^"], ^ means the start of the line when not escaped or in square brackets. When the first item in square brackets it negates anything else included in them, so in this case it says return everything that is not "
Now its coming back, I totally forgot about that.
 
  


Reply



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
create file list: SED inline vs SED standalone, enormous speed difference Corsari Linux - Newbie 4 09-02-2013 03:01 AM
[SOLVED] Multipal line edited using sed, how to make sed specific coolpraz Programming 4 01-05-2013 01:14 PM
[SOLVED] sed help to run sed command against multiple different file names bkone Programming 2 04-16-2012 12:27 PM
[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
How to handle odd characters in sed maker10 Programming 6 07-02-2008 03:15 PM

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

All times are GMT -5. The time now is 05:28 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