LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 08-30-2022, 01:14 AM   #1
beckss
Member
 
Registered: Nov 2008
Posts: 39

Rep: Reputation: 0
Sed exclude and include together


Hi gurus.
I would like ask question how use sed command with include and exclude patterns.
I have line, that started with " and ended with ",and include delimiters "|" between fields
example:

"3bd3ce4c-8839-4d55-bca7-8ad27cddf7f6"|"2e0636c6-400b-4cfe-b63b-a14a0de723f8"|"21713280"|"eb38a6a4-6b0a-496a-b2f7-63c33b21a706"|"We do not "allow" ""SSH"" into Erlang"|"03fb9da3-98a1-4cbe-8aef-d4f9d28e7065"|"2021-06-2 11:32:19.257000"|"2021-06-28 11:32:19.257000"

Using sed '/^"/,/$"/ {s/""/==/g}' will replace "" on SSH word
I need replace " near allow (just as example) and not my delimiter "|"

Thanks and best regards
 
Old 08-30-2022, 01:18 AM   #2
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 18,996

Rep: Reputation: 6449Reputation: 6449Reputation: 6449Reputation: 6449Reputation: 6449Reputation: 6449Reputation: 6449Reputation: 6449Reputation: 6449Reputation: 6449Reputation: 6449
Sorry, I don't really understand. What is the input (exactly) and what is the expected output?
(oh yes, and please use code tags)

Last edited by pan64; 08-30-2022 at 01:19 AM.
 
Old 08-30-2022, 01:20 AM   #3
beckss
Member
 
Registered: Nov 2008
Posts: 39

Original Poster
Rep: Reputation: 0
My input line from file is:
"3bd3ce4c-8839-4d55-bca7-8ad27cddf7f6"|"2e0636c6-400b-4cfe-b63b-a14a0de723f8"|"21713280"|"eb38a6a4-6b0a-496a-b2f7-63c33b21a706"|"We do not "allow" ""SSH"" into Erlang"|"03fb9da3-98a1-4cbe-8aef-d4f9d28e7065"|"2021-06-2 11:32:19.257000"|"2021-06-28 11:32:19.257000"


Desired output need to be:
"3bd3ce4c-8839-4d55-bca7-8ad27cddf7f6"|"2e0636c6-400b-4cfe-b63b-a14a0de723f8"|"21713280"|"eb38a6a4-6b0a-496a-b2f7-63c33b21a706"|"We do not allow ==SSH== into Erlang"|"03fb9da3-98a1-4cbe-8aef-d4f9d28e7065"|"2021-06-2 11:32:19.257000"|"2021-06-28 11:32:19.257000"

Thanks and regards
 
Old 08-30-2022, 01:50 AM   #4
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 18,996

Rep: Reputation: 6449Reputation: 6449Reputation: 6449Reputation: 6449Reputation: 6449Reputation: 6449Reputation: 6449Reputation: 6449Reputation: 6449Reputation: 6449Reputation: 6449
please use code tags
looks like
Code:
sed 's/...SSH.../===SSH===/'
will do the job. (or something similar)
 
Old 08-30-2022, 01:56 AM   #5
beckss
Member
 
Registered: Nov 2008
Posts: 39

Original Poster
Rep: Reputation: 0
Hi
SSH is just example. I need generic code, that replace "" with == and " with ==
but not replace " on the start and the end of line and not replace my delimiter "|"
Thanks
 
Old 08-30-2022, 02:04 AM   #6
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 18,996

Rep: Reputation: 6449Reputation: 6449Reputation: 6449Reputation: 6449Reputation: 6449Reputation: 6449Reputation: 6449Reputation: 6449Reputation: 6449Reputation: 6449Reputation: 6449
and again, please use code tags. Do you understand it? I also attached a link to the documentation.
Without that you cannot distinguish between " and '' (or it is extremely hard) which makes your examples useless.
Code:
Within code tags it is quite easy: " and ''
sed "s/''([^|]*)''/==$1==/" or something similar?
Or probably like this:
Code:
sed "s/''([^|]*)''/==$1==/"
 
Old 08-30-2022, 02:29 AM   #7
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 20,533

Rep: Reputation: 3889Reputation: 3889Reputation: 3889Reputation: 3889Reputation: 3889Reputation: 3889Reputation: 3889Reputation: 3889Reputation: 3889Reputation: 3889Reputation: 3889
Quote:
Originally Posted by beckss View Post
I need replace " near allow (just as example) and not my delimiter "|"
Not good enough - for regex, you have to define the requirements exactly. Only double quotes around words that precede two double quotes ?; double quotes around words anywhere ?; what's a word in this context ?.

Regex can be constructed to do any of the above, but likely not (simply) in one execution - sed supports multiple -e in one command invocation though.
 
Old 08-30-2022, 02:33 AM   #8
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 18,996

Rep: Reputation: 6449Reputation: 6449Reputation: 6449Reputation: 6449Reputation: 6449Reputation: 6449Reputation: 6449Reputation: 6449Reputation: 6449Reputation: 6449Reputation: 6449
Quote:
Originally Posted by syg00 View Post
Not good enough - for regex, you have to define the requirements exactly. Only double quotes around words that precede two double quotes ?; double quotes around words anywhere ?; what's a word in this context ?.

Regex can be constructed to do any of the above, but likely not (simply) in one execution - sed supports multiple -e in one command invocation though.
Yes, and additionally you cannot specify an regexp for exclusion (or at least not in the same step). Actually there is no such thing (regexp for exclusion), you need to look for the required regexp and skip that line (where it was found).
 
Old 08-30-2022, 07:04 AM   #9
boughtonp
Senior Member
 
Registered: Feb 2007
Location: UK
Distribution: Debian
Posts: 2,795

Rep: Reputation: 2049Reputation: 2049Reputation: 2049Reputation: 2049Reputation: 2049Reputation: 2049Reputation: 2049Reputation: 2049Reputation: 2049Reputation: 2049Reputation: 2049

Where did this data come from, why is it in that format, and why do you want to remove quotes?

I'm not sure how easily Sed can do this, but if you can more precisely define the requirements then Perl almost certainly can.

However, the bigger question is why are you wanting to remove quotes in the first place?

(This is important, because your question is a common one from people who incorrectly think it will protect SQL queries from injection attacks.)

 
Old 08-30-2022, 07:57 AM   #10
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,094

Rep: Reputation: 939Reputation: 939Reputation: 939Reputation: 939Reputation: 939Reputation: 939Reputation: 939Reputation: 939
Rule of thumb:
Have the long substitutions first.

The following is untested.
Code:
sed 's/\([^|]\)""\([^|]\)/\1==\2/g; s/\([^|]\)"\([^|]\)/\1\2/g'
Substitute embedded "" by ==
Substitute embedded " by nothing

You cannot limit the start/end of a /g substitution.
/^"/,/"$/ is a range of rows
 
  


Reply

Tags
sed bash, shell script


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] tar with --exclude and/or --exclude-from vbekker Linux - Newbie 6 12-09-2010 02:51 AM
Can we use exclude option in"rm" command to exclude some files/folders? yadav_rk727 Linux - Newbie 1 02-03-2010 10:14 AM
CVS Exclude : Exclude sub directories from check out On Linux from command line shajay12 Linux - Newbie 1 08-03-2009 12:36 AM
tar --exclude --exclude-from cefn Linux - Software 4 10-11-2005 07:31 PM

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

All times are GMT -5. The time now is 04:39 AM.

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