简体   繁体   中英

Bash sed delete line from file not working

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM