deleting a line matching two or more regexp in bash, sed maybe?
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
#!/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
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/^[^:]+://'
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
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
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.