1

I am trying to delete a line from a text file that has a matching ID number.

Student id variable: $sid, for example 12345678; $FILE = student_record

I first tried:

sed -i '/$sid/d' student_record.txt

Which gave me file not found. Next:

sed -i '/$sid/d' $FILE

And I get: sed: 1: "student_record": unterminated substitute in regular expression

sed -i '/12345678/d' $FILE

Same error as above

sed -i '/$sid/ d' student_record.txt

yields:

sed 1: "student_record.txt": bad flag in substitute command: 'x'

If I try without -i,

sed '/$sid/ d' $FILE

It just prints the whole file and doesn't delete any lines.

Advice would be great.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Lizzeiy
  • 428
  • 1
  • 8
  • 22

1 Answers1

5

If the file is called student_record as you say for $FILE, you may be making a mistake using student_record.txt which would explain while you get file not found.

For many of the others, if you use single quotes it will not expand variables, so you'll literally be looking for the string "$sid". If you use double quotes it will expand, so try

sed -i "/$sid/d" "$FILE"

assuming you have GNU sed. If you're on something that does not have GNU, you may not have -i or it may require an argument.

Eric Renouf
  • 13,950
  • 3
  • 45
  • 67
  • Yeah I am using Mac OSX so I just had a look there was an answer stating that you "need to provide an extension for your backups", so for the sed command it would be, sed -i ' ' "/$sid/d" $FILE, and that seems to work! Thanks for the tip :) – Lizzeiy May 26 '15 at 04:24
  • 1
    http://stackoverflow.com/questions/4247068/sed-command-failing-on-mac-but-works-on-linux – Lizzeiy May 26 '15 at 04:24