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 05-18-2010, 12:34 PM   #1
patolfo
Member
 
Registered: Jan 2006
Distribution: Debian-Sarge r2-k.2.6.8-2.386
Posts: 101
Blog Entries: 1

Rep: Reputation: 15
deleting a line matching two or more regexp in bash, sed maybe?


Hi guys, i want to delete from a file lines matching two regexp using sed, or other one line command

any ideas?
 
Old 05-18-2010, 01:50 PM   #2
AlucardZero
Senior Member
 
Registered: May 2006
Location: USA
Distribution: Debian
Posts: 4,824

Rep: Reputation: 615Reputation: 615Reputation: 615Reputation: 615Reputation: 615Reputation: 615
You can pass multiple -e options to sed
 
Old 05-18-2010, 01:53 PM   #3
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
Show an example of the patterns you want to match. Show the before and after state
 
Old 05-18-2010, 02:52 PM   #4
patolfo
Member
 
Registered: Jan 2006
Distribution: Debian-Sarge r2-k.2.6.8-2.386
Posts: 101

Original Poster
Blog Entries: 1

Rep: Reputation: 15
Yep problem is how to put two reg exp

Code:
grep regex1 file | sed '/regex2/d' -i file
Quote:
Originally Posted by AlucardZero View Post
You can pass multiple -e options to sed
 
Old 05-18-2010, 05:00 PM   #5
g0su
LQ Newbie
 
Registered: Sep 2007
Posts: 9

Rep: Reputation: 1
Two Regular Expressions

cat file | sed -e 's/pattern//g' -e 's/pattern//g' > newFile;
mv newFile file;
 
Old 05-18-2010, 05:03 PM   #6
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,786

Rep: Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083
Code:
sed -i '/regex1/{/regex2/d}' file

Last edited by ntubski; 05-18-2010 at 05:24 PM. Reason: Forgot quotes as Andrew Benton pointed out
 
Old 05-18-2010, 05:22 PM   #7
Andrew Benton
Senior Member
 
Registered: Aug 2003
Location: Birkenhead/Britain
Distribution: Linux From Scratch
Posts: 2,073

Rep: Reputation: 64
I like nutbski's solution (though I would wrap it in '') But just to be different I thought I'd say how I'd do it
Code:
sed -i '/regex1.*regex2/d' file
 
Old 05-19-2010, 10:16 AM   #8
patolfo
Member
 
Registered: Jan 2006
Distribution: Debian-Sarge r2-k.2.6.8-2.386
Posts: 101

Original Poster
Blog Entries: 1

Rep: Reputation: 15
I tried that too, problem was that there are some lines containing the first pattern, but no the second.
And they got affected

Quote:
Originally Posted by g0su View Post
cat file | sed -e 's/pattern//g' -e 's/pattern//g' > newFile;
mv newFile file;
 
Old 05-19-2010, 10:28 AM   #9
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
Did you try ntubski's? Worked like a charm for me
 
Old 05-19-2010, 03:48 PM   #10
patolfo
Member
 
Registered: Jan 2006
Distribution: Debian-Sarge r2-k.2.6.8-2.386
Posts: 101

Original Poster
Blog Entries: 1

Rep: Reputation: 15
Quote:
Originally Posted by grail View Post
Did you try ntubski's? Worked like a charm for me
Let me try it again, i was doing other stuff at the same time

...
 
Old 05-19-2010, 04:11 PM   #11
patolfo
Member
 
Registered: Jan 2006
Distribution: Debian-Sarge r2-k.2.6.8-2.386
Posts: 101

Original Poster
Blog Entries: 1

Rep: Reputation: 15
yep the ntubski is quite interesting

Quote:
Originally Posted by ntubski View Post
Code:
sed -i '/regex1/{/regex2/d}' file
Andrew actually i like your piece code, but ntubski (if it is a name where does it comes from) i find yours quite interesting, the {} inclusion, well i must said it is a first time for me...

please do not start trowing rotten tomatoes yet, but if i do remember well
sed starts from left to right, right?

so if i get it straight, this command looks for regex1, and to those lines it applies the second regex2/delete line command

which could be another command?, not precisely deleting

So the {}, can be used to put a command inside a command; now a far fetched question, how many substitution {'s}, can be nested, inside sed, one or more?

By the way thanks to both of you for your code, i completely forgot about making composite regexps, i was thinking in terms of isolated terms instead of seeing the text line as a whole.
 
Old 05-19-2010, 04:14 PM   #12
patolfo
Member
 
Registered: Jan 2006
Distribution: Debian-Sarge r2-k.2.6.8-2.386
Posts: 101

Original Poster
Blog Entries: 1

Rep: Reputation: 15
Thumbs up results

Code:
#!/bin/bash
sed -i '/aunque/{/tengo/d}' $1
#sed -i '/aunque.*tengo/d' $1
exit
input
Code:
ENGANCHADO A TI
(bunbury) 
aunque me haga daño
aunque sea extraño
aunque cuando no te tengo
aunque me hayas capturado
aunque me confundes
aunque me transformes
aunque sea un mr. high encantador
output
Code:
ENGANCHADO A TI
(bunbury) 
aunque me haga daño
aunque sea extraño
aunque me hayas capturado
aunque me confundes
aunque me transformes
aunque sea un mr. high encantador
Both roads take us to Rome ...
Thanks
 
Old 05-19-2010, 06:46 PM   #13
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
You can think of the braces as the same as in awk, if the previous statement is true then proceed with next inside braces.
 
Old 05-19-2010, 07:13 PM   #14
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
Does that mean you meant "matching two of two" instead of "matching two or more" regexes? The solutions for "two or more" are different than what you ended up with. Here is one, just because I think the title of the post is more interesting.
Code:
#!/bin/bash

max_matches=1               #max number of pattern matches allowed
patterns=('aunque' 'tengo') #the patterns to match (you can use as many as you want)

file="$1"

counts="$( eval echo -n {1..$(($max_matches+1))} | tr ' ' '|' )"

{ for pattern in "${patterns[@]}"; do
  egrep -n "$pattern" "$file"
done; grep -n '' "$file"; } | sort -n | uniq -c | egrep "^ *($counts) " | sed -r 's/^[^:]+://'
Kevin Barry

PS On FreeBSD, use -E instead of -r for sed.

Last edited by ta0kira; 05-19-2010 at 07:16 PM.
 
Old 05-19-2010, 07:32 PM   #15
patolfo
Member
 
Registered: Jan 2006
Distribution: Debian-Sarge r2-k.2.6.8-2.386
Posts: 101

Original Poster
Blog Entries: 1

Rep: Reputation: 15
well two or more...

Yes the idea is two or more, that is why ntubski code pick my interest, because there i can nest regexps, well using variables, '"$regex_n"' inside the brackets.

Thanks for pointing that out, i marked solved because the possibility of nesting.

However, your code on the other hand, well it does what i wanted with out nesting all the regexs

Quote:
Originally Posted by ta0kira View Post
Does that mean you meant "matching two of two" instead of "matching two or more" regexes? The solutions for "two or more" are different than what you ended up with. Here is one, just because I think the title of the post is more interesting.
Code:
#!/bin/bash

max_matches=1               #max number of pattern matches allowed
patterns=('aunque' 'tengo') #the patterns to match (you can use as many as you want)

file="$1"

counts="$( eval echo -n {1..$(($max_matches+1))} | tr ' ' '|' )"

{ for pattern in "${patterns[@]}"; do
  egrep -n "$pattern" "$file"
done; grep -n '' "$file"; } | sort -n | uniq -c | egrep "^ *($counts) " | sed -r 's/^[^:]+://'
Kevin Barry

PS On FreeBSD, use -E instead of -r for sed.
p.s. By the way, this is where i got the data, the lyrics, i mean... the text lines.
Code:
http://www.youtube.com/watch?v=ufoANAKh-GE

Last edited by patolfo; 05-19-2010 at 07:39 PM. Reason: ps
 
  


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
Deleting empty line at end of text file in BASH human2.0 Linux - General 8 04-01-2009 02:44 AM
vim or sed multiline regexp matching eentonig Programming 1 09-08-2008 09:06 AM
javascript regexp - strange exec behaviour, or space matching? jkobrien Programming 3 08-20-2008 07:09 AM
SED - Delete line above or below as well as matching line... OldGaf Programming 7 06-26-2008 11:51 PM
help with sed / regexp elinenbe Programming 2 02-01-2008 10:09 AM

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

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