Reputation: 9752
Hi I want to find all strings anywhere in a file that begin with the letters rs
i.e.
rs12345 100
rs54321 200
300 rs13579
and delete all strings that begin with the criteria so that I get:
100
200
300
i.e. replace the string with nothing. I am not bothered about leading whitespace before final output as I deal with that later. I have tried sed 's/rs*//g'
however this gives:
12345 100
54321 200
i.e. only removes the rs
.
How do I edit my sed command to delete the entire string? Thanks
Upvotes: 3
Views: 20185
Reputation: 289495
You can replace from starting rs
up to a space by an empty string, so that the rsXXX
gets removed.
sed 's/^rs[^ ]*//' file
This supposed your rs
was in the beginning of the line. If rs
can appear anywhere in the file, use:
sed 's/\brs[^ ]*//' file
The \b
works as word boundary, so that things like hellorshello
does not match.
$ cat a
rs12345 100
rs54321 200
hellooo 300
$ sed 's/^rs[^ ]*//' a
100
200
hellooo 300
Note I am not dealing with the whitespace, since you mention you are handling it later on. In case you needed it, you can say sed 's/^rs[^ ]* \+//' file
.
rs
anywhere:
$ cat a
rs12345 100
rs54321 200
hellooo 300
and rs12345 100
thisrsisatest 24
$ sed 's/\brs[^ ]*//' a
100
200
hellooo 300
and 100
thisrsisatest 24
Note you current approach wasn't working because with rs*
you are saying: r
followed by 0 or more s
.
Upvotes: 9
Reputation: 8972
Let the word be a pattern starting with word break \b
and having characters \w
(and ending with \b
too, but we will not need it).
The command that removes words starting with rs will be sed -r 's/\brs\w+//g' file
$ cat file
rs12345 100
rs54321 200
ars1234 000
$ sed -r 's/\brs\w+//g' file
100
200
ars1234 000
Upvotes: 0