I'm working in a small bash script. I need to remove an entire line that match an exact word in only the first word of the line.
That's how the text file looks:
John is a dumb
Maria is awesome
Toni and Maria are funny
I want to remove the just the second line.
Right now, I can match the exact word "maria" but it removes the third line too:
sed -i "/\b\(maria\)\b/d" file.txt
How to specify just the first word?
Thanks!
Currently you are looking for "maria" surrounded by a word boundary (represented by \b
.) Instead look for "maria" preceded by the start of line (represented by ^
.) Note I've also removed unnecessary parentheses and added the /I
flag, which will make the search case-insensitive. Your original wouldn't have matched "Maria".
sed -i "/^maria\b/Id" file.txt
Edit: fixed case insensitive flag to I instead of i!
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments