Remember that you are using regex metacharacters.
sed -i "s/test.pwd/d" test.txt
says "delete any line that has test
, then any single character, then pwd
". In your file, all of the lines you showed match.
Quote the .
, and add something that disqualifies non-matching lines. As
Paul Pazderski suggested in his very first comment, adding the =
does the trick, though more specificity is better. sed -i 's/^test[.]pwd=/d' test.txt
will only remove the intended line from your example.
If putting your pattern in a variable, be wary of quoting and metacharacters that need backslashes, but for this example, it looks pretty straightforward.
$: cat file
safe-line=seeMe
test.pwd=test@123
test-pwd=keepMe
test.pwd.path=/tmp/test
test.pwd1.sample=123
$: pat="test.pwd" # loses most lines
$: sed "/$pat/d" file
safe-line=seeMe
$: pat="test.pwd=" # still loses test-pwd
$: sed "/$pat/d" file
safe-line=seeMe
test.pwd.path=/tmp/test
test.pwd1.sample=123
$: pat=^test\.pwd= # unquoted string, literal dot - still loses test-pwd
$: sed "/$pat/d" file
safe-line=seeMe
test.pwd.path=/tmp/test
test.pwd1.sample=123
$: pat="^test\.pwd=" # works, but some may wonder - why not \\ ?
$: sed "/$pat/d" file
safe-line=seeMe
test-pwd=keepMe
test.pwd.path=/tmp/test
test.pwd1.sample=123
$: pat="^test[.]pwd=" # works, and clear
$: sed "/$pat/d" file
safe-line=seeMe
test-pwd=keepMe
test.pwd.path=/tmp/test
test.pwd1.sample=123