Remove line from file in bash script using sed command

user3521205

I am trying to remove lines from a text file from a Bash Script using command sed.

Here is how this function works.
User enters record number Program Searches for record number Program deletes record

Here is my code:

r=$(grep -h "$record"  student_records.txt|cut -d"," -f1)  #find the record that needs to be deleted
echo $line -->> This proves that previous command works
sed -i '/^$r/d' student_records.txt  -->> this does not work

Any ideas?

John1024

To remove a line containing $record from the file:

grep -v "$record" student_records.txt >delete.me && mv -f delete.me student_records.txt

In the above, $record is treated as a regular expression. This means, for example, that a period is a wildcard. If that is unwanted, add the -F option to grep to specify that $record is to be treated as a fixed string.

Comments

Consider these two line:

r=$(grep -h "$record"  student_records.txt|cut -d"," -f1)  #find the record that needs to be deleted
echo $line -->> This proves that previous command works

The first line defines a shell variable r. The second line prints the shell variable line, a variable which was unaffected by the previous command. Consequently, the second line is not a successful test on the first.

sed -i '/^$r/d' student_records.txt  -->> this does not work

Obseve that the expression $r appears inside single-quotes. The shell does not alter any inside single quotes. Consequently, $r will remain a dollar sign followed by an r. Since a dollar sign matches the end of a line, this expression will match nothing. The following would work better:

sed -i "/^$r/d" student_records.txt

Unlike the grep command, however, the above sed command is potentially dangerous. It would be easy to construct a value of r that would cause sed to do surprising things. So, don't use this approach unless you trust the process by which you obtained r.

What if more than one line matches record?

If there is more than one line that matches record, then the following would generate an unterminated address regex error from sed:

r=$(grep -h "$record"  student_records.txt|cut -d"," -f1)
sed -i "/^$r/d" student_records.txt

This error is an example of the surprising results that can happen when a shell variable is expanded into a sed command.

By contrast, this approach would remove all matching lines:

grep -v "$record" student_records.txt >delete.me && mv -f delete.me student_records.txt

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Remove line from php file using sed

How can I remove the first line of a text file using bash/sed script?

Bash (sed) script works directly from command line but not through watch

sed is not working for commenting a line in a file using bash script

how to remove last comma from line in bash using "sed or awk"

Is there a way to remove a string from a line of a csv using sed in bash scripts?

Remove a single line from a very large file using sed

Unzipping file with exclamation mark from command line in bash script

applying sed to certains line from file using bash

bash/sed script to get output from a file using a regex

Bash script using sed to find a line WITH brackets

How to remove characters from file names using command line?

Remove comments and empty lines from a file using bash/shell command

Delete a line from XML file using bash script

Remove words starting with "_" in file using sed in bash

Adding a line to a file using sed in a shell script

Bash sed delete line from file not working

Use Command Line Argument and Variable with sed Inside Bash Script

ffmpeg command not working from bash script but working from command line

rm command with ! works from command line but not from bash script

chmod command in Ubuntu works from command line but not from bash script

Bash script: Using "script" command from a bash script for logging a session

Using sed on Bash Command

Remove the last line from a file in Bash

Remove a line from file that starts with a number in bash

Remove a certain line from Bash history file

python script command line arguments from file

converting txt file from bash command line

Use line from file as a command in bash