Hazin C.
Hazin C.

Reputation: 43

Sed deleting exact pattern in variable from file

I've watched many solutions on this site but none of them have helped me solve my problem. I got a file containing

aaa
bbbb
bb
ddddd
dd
ccccc
ccc

I've tried to delete an exact line with sed -i /$1/d ./.resources where $1 is user input. when I fill in bb it is suppose to delete only bb and not all matching patterns like bbbb. I've tried using \<\> for exact pattern like sed -i /\<$1\>/d ./.resources but it didn't work. What am I doing wrong? How can I solve this?

Upvotes: 1

Views: 421

Answers (2)

user2350426
user2350426

Reputation:

You need to tell sed that the pattern should be matched within this limits:

  • Start of the line: ^
  • End of the line: $

So, this will work:

set -- dd

sed '/^${1}$/d' ./.resources

Upvotes: 1

Armin Braun
Armin Braun

Reputation: 3683

You gotta match only those lines that have the pattern exactly in between the beginning and end of the line by using the ^ and $ operator: So for your example the code would be:

sed -i /^${1}$/d ./.resources

Upvotes: 1

Related Questions