LinuxQuestions.org
Register a domain and help support LQ
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, 01: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, 02:50 PM   #2
AlucardZero
Senior Member
 
Registered: May 2006
Location: USA
Distribution: Debian
Posts: 4,653

Rep: Reputation: 536Reputation: 536Reputation: 536Reputation: 536Reputation: 536Reputation: 536
You can pass multiple -e options to sed
 
Old 05-18-2010, 02:53 PM   #3
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

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

Original Poster
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, 06: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, 06:03 PM   #6
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian
Posts: 2,543

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

Last edited by ntubski; 05-18-2010 at 06:24 PM. Reason: Forgot quotes as Andrew Benton pointed out
 
Old 05-18-2010, 06: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, 11:16 AM   #8
patolfo
Member
 
Registered: Jan 2006
Distribution: Debian-Sarge r2-k.2.6.8-2.386
Posts: 101
Blog Entries: 1

Original Poster
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, 11:28 AM   #9
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,698

Rep: Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988
Did you try ntubski's? Worked like a charm for me
 
Old 05-19-2010, 04:48 PM   #10
patolfo
Member
 
Registered: Jan 2006
Distribution: Debian-Sarge r2-k.2.6.8-2.386
Posts: 101
Blog Entries: 1

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

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

Original Poster
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, 07:46 PM   #13
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,698

Rep: Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988
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, 08: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 08:16 PM.
 
Old 05-19-2010, 08:32 PM   #15
patolfo
Member
 
Registered: Jan 2006
Distribution: Debian-Sarge r2-k.2.6.8-2.386
Posts: 101
Blog Entries: 1

Original Poster
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 08:39 PM. Reason: ps
 
  


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


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