0

I am creating a basic shell script to manipulate a database as a college assignment. However, How many times I try, I can't get the sed command fully correct. The functionality where sed is to be used is this:

$name is the variable. User will supply the name via the terminal. This name will be looked up in the database and will be deleted if present.

The command I used is this:

sed -i /$name/d <filename>

However, this command only works if the entered value contains no space.

I tried out my solution by using what I could salvage from this such as sed -i '/$name/d' <filename>

However, to no avail. How to go about this?

  • Adding quotes somehow doesn't work –  Jun 30 '19 at 11:16
  • 1
    Really? Show me. – glenn jackman Jun 30 '19 at 11:18
  • Err.... would drive link do? –  Jun 30 '19 at 11:20
  • lol, not very big. It's a tiny college assignment with the file containing only few entries. The columns are:

    Name designation department

    –  Jun 30 '19 at 11:34
  • Yay!! you're an angel jesse :D –  Jun 30 '19 at 11:39
  • 3
    Note that building a database that you manipulate with sed is extremely fragile. To take an example, what's stopping a user from deleting everything by entering . into the value of that variable? How would you prevent disclosing the contents of it when someone enters a value that contains actual sed code? – Kusalananda Jun 30 '19 at 11:57
  • 1
    In this particular case, you could get away with a single set of double quotes: sed -i "/$name/d" <filename>" but there are other cases where you have to mix quotes in order to get the shell to do what you want. – NickD Jun 30 '19 at 12:05
  • @Kusalananda Who uses sed to create and edit databases??? We have SQL and the likes. I am doing this shitty thing just to get 10 marks for completing my assignment ;) –  Jun 30 '19 at 12:05
  • Err. Can we use regex to simply replace two pairs of words with different text in between? –  Jul 02 '19 at 06:45

0 Answers0