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

Rep: Reputation: Disabled
(SOLVED) Search and Replace Exact string without removing the line


I have this text :

Quote:
I (love) rock & roll he (love) rock & rool john (love) dance
you (love) rock he (love) rock & rool
I want to remove only :
(love)

leaving as example text :
Quote:
I rock & roll he rock & rool john dance
you rock he rock & rool
The problem that i am facing are the () in the text i want to replace or delete if possible without messing up with the other text .

Code:
var=$(cat text.txt |sed "/\'(love)'//d")
echo $var
It is not working as i need .
Any solutions ?

Last edited by pedropt; 09-09-2017 at 02:51 PM.
 
Old 09-09-2017, 02:30 PM   #2
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,881
Blog Entries: 1

Rep: Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871
Code:
sed 's/\(love\)//g' text.txt >output.txt

Last edited by NevemTeve; 09-10-2017 at 12:00 AM. Reason: to many "Code:"
 
Old 09-09-2017, 02:31 PM   #3
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,359
Blog Entries: 3

Rep: Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767
Three tips: Skip the cat. Don't search for doube quotes if they are not in the target string. Check the substitution command in sed

Code:
man sed
 
1 members found this post helpful.
Old 09-09-2017, 02:37 PM   #4
pedropt
Member
 
Registered: Aug 2014
Distribution: Devuan
Posts: 345

Original Poster
Rep: Reputation: Disabled
Code:
sed 's/\(love\)//g' text.txt >output.txt
is not working , it removes the word "love" but not the complete (love) as i want .
using that command i get :
Quote:
I () rock & roll he () rock & rool john () dance
you () rock he () rock & rool
 
Old 09-09-2017, 02:50 PM   #5
pedropt
Member
 
Registered: Aug 2014
Distribution: Devuan
Posts: 345

Original Poster
Rep: Reputation: Disabled
Just got it .

Quote:
sed -e "s/(love)//g"
 
Old 09-10-2017, 12:20 AM   #6
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,881
Blog Entries: 1

Rep: Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871
Sorry, you're right, the code I suggested doesn't work; ()parentheses without \backslash are interpreted as literal characters, with \backslash are acting as grouping operators.

Some examples for myself
Code:
$ echo 'ApartB' | sed 's/ApartB/a&b/g' # '&': total back-reference
aApartBb

$ echo 'A(part)B' | sed 's/A(part)B/a&b/g' # '&': total back-reference
aA(part)Bb

$ echo 'ApartB' | sed 's/A\(part\)B/a\1b/g' # '\1': partial back-reference
apartb

$ echo 'A(part)B' | sed 's/A(\(part\))B/a\1b/g' # '\1': partial back-reference
apartb

$ echo 'A(part)B' | sed 's/A\((part)\)B/a\1b/g' # '\1': partial back-reference
a(part)b

Last edited by NevemTeve; 09-10-2017 at 12:21 AM.
 
Old 09-10-2017, 03:53 AM   #7
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,832

Rep: Reputation: 1219Reputation: 1219Reputation: 1219Reputation: 1219Reputation: 1219Reputation: 1219Reputation: 1219Reputation: 1219Reputation: 1219
You must distinguish between BRE style, grep, sed, expr, where \( \) are special for grouping,
and ERE style, egrep, grep -E, sed -r, awk, perl, where ( ) are special for grouping.
 
1 members found this post helpful.
  


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
[Close] search for exact string on file and delete that line pedropt Programming 3 07-11-2017 03:36 PM
[Solved] Search & Replace exact string with sed pedropt Programming 17 06-25-2017 02:57 PM
LXer: How to search and replace a multi-line string in a file LXer Syndicated Linux News 0 02-25-2016 05:00 AM
[SOLVED] sed: global search and replace if a string isn't anywhere in that line linux_kung_fu Linux - General 5 03-09-2012 10:53 AM
search and replace with multi line string kambrish Programming 5 04-28-2008 06:02 AM

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

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