sed to delete lines using multiple search string
Clash Royale CLAN TAG#URR8PPP
up vote
4
down vote
favorite
I have a text file, let say test1.txt
, I need to remove the line containing dog and date with "29-APR-2015"
Using the below command to accomplish it but it is not deleting the lines.
Where as if i mention just with /DOG/d
it is deleting the lines containing DOG
.
command file (sedcommands)
/DOG,29-APR-2015/d
test1.txt
DOG 29-APR-2015
DOG 29-APR-2015
DOG 30-APR-2015
CAT 29-APR-2015
CAT 29-APR-2015
command
sed -f sedcommands test1.txt > test2.txt
sed
add a comment |
up vote
4
down vote
favorite
I have a text file, let say test1.txt
, I need to remove the line containing dog and date with "29-APR-2015"
Using the below command to accomplish it but it is not deleting the lines.
Where as if i mention just with /DOG/d
it is deleting the lines containing DOG
.
command file (sedcommands)
/DOG,29-APR-2015/d
test1.txt
DOG 29-APR-2015
DOG 29-APR-2015
DOG 30-APR-2015
CAT 29-APR-2015
CAT 29-APR-2015
command
sed -f sedcommands test1.txt > test2.txt
sed
add a comment |
up vote
4
down vote
favorite
up vote
4
down vote
favorite
I have a text file, let say test1.txt
, I need to remove the line containing dog and date with "29-APR-2015"
Using the below command to accomplish it but it is not deleting the lines.
Where as if i mention just with /DOG/d
it is deleting the lines containing DOG
.
command file (sedcommands)
/DOG,29-APR-2015/d
test1.txt
DOG 29-APR-2015
DOG 29-APR-2015
DOG 30-APR-2015
CAT 29-APR-2015
CAT 29-APR-2015
command
sed -f sedcommands test1.txt > test2.txt
sed
I have a text file, let say test1.txt
, I need to remove the line containing dog and date with "29-APR-2015"
Using the below command to accomplish it but it is not deleting the lines.
Where as if i mention just with /DOG/d
it is deleting the lines containing DOG
.
command file (sedcommands)
/DOG,29-APR-2015/d
test1.txt
DOG 29-APR-2015
DOG 29-APR-2015
DOG 30-APR-2015
CAT 29-APR-2015
CAT 29-APR-2015
command
sed -f sedcommands test1.txt > test2.txt
sed
sed
edited Nov 20 at 22:28
Rui F Ribeiro
38.2k1475125
38.2k1475125
asked Apr 29 '15 at 2:33
Gopal
12313
12313
add a comment |
add a comment |
4 Answers
4
active
oldest
votes
up vote
4
down vote
accepted
With GNU sed:
sed '/DOG//29-APR-2015/d' test1
This method allows for any order. ie. DOG
can either be before or after the date.
@cuonglm -sed --posix '/DOG//29-APR-2015/d'
works fine in GNU sed version 4.2.1 (with all GNU extensions disabled).
– Peter.O
Apr 30 '15 at 4:11
@Peter.O: No, POSIX require terminator between the command anh}
. At least your command won't work in BSD sed.
– cuonglm
Apr 30 '15 at 4:28
@Peter.O, GNUsed
's--posix
is to tell it to be more POSIX conformant. It's not telling it to report an error on any non-POSIX code you throw at it. It would not make GNUsed
more POSIX conformant to report an error when you give it that non-standardxxx
code.
– Stéphane Chazelas
May 4 '15 at 16:25
add a comment |
up vote
3
down vote
POSIXly:
sed -e '/DOG/!b' -e '/29-APR-2015/!b' -e d file
b
ranch to the end if you don't provide any label.
or:
sed '/DOG//29-APR-2015/d
' file
Modern sed
implementations also support command;
form, this's an accepted extension by POSIX, but not required:
sed '/DOG//29-APR-2015/d;' file
add a comment |
up vote
2
down vote
/DOG,29-APR-2015/d
will not work because there is no comma between DOG
and 9-APR-2015
. Try this;
$ sed -e '/DOG[[:space:]]*29-APR-2015/d' test1.txt
DOG 30-APR-2015
CAT 29-APR-2015
CAT 29-APR-2015
[[:space:]]*
allows for zero or more white space characters between DOG
and 9-APR-2015
. The character class [:space:]
allows both ordinary spaces and tabs and is safe to use with unicode fonts.
add a comment |
up vote
1
down vote
The command file should contain:
/DOG *29-APR-2015/d
That is, DOG
followed by 0 or more spaces, followed by specified date.
add a comment |
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
4
down vote
accepted
With GNU sed:
sed '/DOG//29-APR-2015/d' test1
This method allows for any order. ie. DOG
can either be before or after the date.
@cuonglm -sed --posix '/DOG//29-APR-2015/d'
works fine in GNU sed version 4.2.1 (with all GNU extensions disabled).
– Peter.O
Apr 30 '15 at 4:11
@Peter.O: No, POSIX require terminator between the command anh}
. At least your command won't work in BSD sed.
– cuonglm
Apr 30 '15 at 4:28
@Peter.O, GNUsed
's--posix
is to tell it to be more POSIX conformant. It's not telling it to report an error on any non-POSIX code you throw at it. It would not make GNUsed
more POSIX conformant to report an error when you give it that non-standardxxx
code.
– Stéphane Chazelas
May 4 '15 at 16:25
add a comment |
up vote
4
down vote
accepted
With GNU sed:
sed '/DOG//29-APR-2015/d' test1
This method allows for any order. ie. DOG
can either be before or after the date.
@cuonglm -sed --posix '/DOG//29-APR-2015/d'
works fine in GNU sed version 4.2.1 (with all GNU extensions disabled).
– Peter.O
Apr 30 '15 at 4:11
@Peter.O: No, POSIX require terminator between the command anh}
. At least your command won't work in BSD sed.
– cuonglm
Apr 30 '15 at 4:28
@Peter.O, GNUsed
's--posix
is to tell it to be more POSIX conformant. It's not telling it to report an error on any non-POSIX code you throw at it. It would not make GNUsed
more POSIX conformant to report an error when you give it that non-standardxxx
code.
– Stéphane Chazelas
May 4 '15 at 16:25
add a comment |
up vote
4
down vote
accepted
up vote
4
down vote
accepted
With GNU sed:
sed '/DOG//29-APR-2015/d' test1
This method allows for any order. ie. DOG
can either be before or after the date.
With GNU sed:
sed '/DOG//29-APR-2015/d' test1
This method allows for any order. ie. DOG
can either be before or after the date.
edited Apr 29 '15 at 14:23
cuonglm
101k23196297
101k23196297
answered Apr 29 '15 at 3:17
Peter.O
18.7k1791143
18.7k1791143
@cuonglm -sed --posix '/DOG//29-APR-2015/d'
works fine in GNU sed version 4.2.1 (with all GNU extensions disabled).
– Peter.O
Apr 30 '15 at 4:11
@Peter.O: No, POSIX require terminator between the command anh}
. At least your command won't work in BSD sed.
– cuonglm
Apr 30 '15 at 4:28
@Peter.O, GNUsed
's--posix
is to tell it to be more POSIX conformant. It's not telling it to report an error on any non-POSIX code you throw at it. It would not make GNUsed
more POSIX conformant to report an error when you give it that non-standardxxx
code.
– Stéphane Chazelas
May 4 '15 at 16:25
add a comment |
@cuonglm -sed --posix '/DOG//29-APR-2015/d'
works fine in GNU sed version 4.2.1 (with all GNU extensions disabled).
– Peter.O
Apr 30 '15 at 4:11
@Peter.O: No, POSIX require terminator between the command anh}
. At least your command won't work in BSD sed.
– cuonglm
Apr 30 '15 at 4:28
@Peter.O, GNUsed
's--posix
is to tell it to be more POSIX conformant. It's not telling it to report an error on any non-POSIX code you throw at it. It would not make GNUsed
more POSIX conformant to report an error when you give it that non-standardxxx
code.
– Stéphane Chazelas
May 4 '15 at 16:25
@cuonglm -
sed --posix '/DOG//29-APR-2015/d'
works fine in GNU sed version 4.2.1 (with all GNU extensions disabled).– Peter.O
Apr 30 '15 at 4:11
@cuonglm -
sed --posix '/DOG//29-APR-2015/d'
works fine in GNU sed version 4.2.1 (with all GNU extensions disabled).– Peter.O
Apr 30 '15 at 4:11
@Peter.O: No, POSIX require terminator between the command anh
}
. At least your command won't work in BSD sed.– cuonglm
Apr 30 '15 at 4:28
@Peter.O: No, POSIX require terminator between the command anh
}
. At least your command won't work in BSD sed.– cuonglm
Apr 30 '15 at 4:28
@Peter.O, GNU
sed
's --posix
is to tell it to be more POSIX conformant. It's not telling it to report an error on any non-POSIX code you throw at it. It would not make GNU sed
more POSIX conformant to report an error when you give it that non-standard xxx
code.– Stéphane Chazelas
May 4 '15 at 16:25
@Peter.O, GNU
sed
's --posix
is to tell it to be more POSIX conformant. It's not telling it to report an error on any non-POSIX code you throw at it. It would not make GNU sed
more POSIX conformant to report an error when you give it that non-standard xxx
code.– Stéphane Chazelas
May 4 '15 at 16:25
add a comment |
up vote
3
down vote
POSIXly:
sed -e '/DOG/!b' -e '/29-APR-2015/!b' -e d file
b
ranch to the end if you don't provide any label.
or:
sed '/DOG//29-APR-2015/d
' file
Modern sed
implementations also support command;
form, this's an accepted extension by POSIX, but not required:
sed '/DOG//29-APR-2015/d;' file
add a comment |
up vote
3
down vote
POSIXly:
sed -e '/DOG/!b' -e '/29-APR-2015/!b' -e d file
b
ranch to the end if you don't provide any label.
or:
sed '/DOG//29-APR-2015/d
' file
Modern sed
implementations also support command;
form, this's an accepted extension by POSIX, but not required:
sed '/DOG//29-APR-2015/d;' file
add a comment |
up vote
3
down vote
up vote
3
down vote
POSIXly:
sed -e '/DOG/!b' -e '/29-APR-2015/!b' -e d file
b
ranch to the end if you don't provide any label.
or:
sed '/DOG//29-APR-2015/d
' file
Modern sed
implementations also support command;
form, this's an accepted extension by POSIX, but not required:
sed '/DOG//29-APR-2015/d;' file
POSIXly:
sed -e '/DOG/!b' -e '/29-APR-2015/!b' -e d file
b
ranch to the end if you don't provide any label.
or:
sed '/DOG//29-APR-2015/d
' file
Modern sed
implementations also support command;
form, this's an accepted extension by POSIX, but not required:
sed '/DOG//29-APR-2015/d;' file
edited May 4 '15 at 17:04
answered Apr 29 '15 at 4:46
cuonglm
101k23196297
101k23196297
add a comment |
add a comment |
up vote
2
down vote
/DOG,29-APR-2015/d
will not work because there is no comma between DOG
and 9-APR-2015
. Try this;
$ sed -e '/DOG[[:space:]]*29-APR-2015/d' test1.txt
DOG 30-APR-2015
CAT 29-APR-2015
CAT 29-APR-2015
[[:space:]]*
allows for zero or more white space characters between DOG
and 9-APR-2015
. The character class [:space:]
allows both ordinary spaces and tabs and is safe to use with unicode fonts.
add a comment |
up vote
2
down vote
/DOG,29-APR-2015/d
will not work because there is no comma between DOG
and 9-APR-2015
. Try this;
$ sed -e '/DOG[[:space:]]*29-APR-2015/d' test1.txt
DOG 30-APR-2015
CAT 29-APR-2015
CAT 29-APR-2015
[[:space:]]*
allows for zero or more white space characters between DOG
and 9-APR-2015
. The character class [:space:]
allows both ordinary spaces and tabs and is safe to use with unicode fonts.
add a comment |
up vote
2
down vote
up vote
2
down vote
/DOG,29-APR-2015/d
will not work because there is no comma between DOG
and 9-APR-2015
. Try this;
$ sed -e '/DOG[[:space:]]*29-APR-2015/d' test1.txt
DOG 30-APR-2015
CAT 29-APR-2015
CAT 29-APR-2015
[[:space:]]*
allows for zero or more white space characters between DOG
and 9-APR-2015
. The character class [:space:]
allows both ordinary spaces and tabs and is safe to use with unicode fonts.
/DOG,29-APR-2015/d
will not work because there is no comma between DOG
and 9-APR-2015
. Try this;
$ sed -e '/DOG[[:space:]]*29-APR-2015/d' test1.txt
DOG 30-APR-2015
CAT 29-APR-2015
CAT 29-APR-2015
[[:space:]]*
allows for zero or more white space characters between DOG
and 9-APR-2015
. The character class [:space:]
allows both ordinary spaces and tabs and is safe to use with unicode fonts.
answered Apr 29 '15 at 2:41
John1024
45.4k4101118
45.4k4101118
add a comment |
add a comment |
up vote
1
down vote
The command file should contain:
/DOG *29-APR-2015/d
That is, DOG
followed by 0 or more spaces, followed by specified date.
add a comment |
up vote
1
down vote
The command file should contain:
/DOG *29-APR-2015/d
That is, DOG
followed by 0 or more spaces, followed by specified date.
add a comment |
up vote
1
down vote
up vote
1
down vote
The command file should contain:
/DOG *29-APR-2015/d
That is, DOG
followed by 0 or more spaces, followed by specified date.
The command file should contain:
/DOG *29-APR-2015/d
That is, DOG
followed by 0 or more spaces, followed by specified date.
answered Apr 29 '15 at 2:40
unxnut
3,5522918
3,5522918
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f199279%2fsed-to-delete-lines-using-multiple-search-string%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown