I have a sample text file called result.txt
just to demonstrate:
{HEX}.A.Bs
{HEX}.A.Bss
{HEX}.A.Bsss
{HEX}.A.Bssss
but in real application, the content of the result.txt
can have any characters line by line like:
/usr/local/abc.txt
/var/tmp/
png
OIPOP()_+(_)&*#)@IOJDNU*@Utest
and I want to delete a line of string via argument in a bash script. So, in my bash script called test.sh
, I have the following code:
!/bin/sh
#test.sh
result=result.txt
del="$1"
sed -i "/$del/d" $result
cat result.txt
Example 1
So to delete "{HEX}.A.Bs"
, I run the following script with an argument:
./test.sh "{HEX}.A.Bs"
this should delete a string called "{HEX}.A.Bs"
inside result.txt
but all the contents inside result.txt
were deleted. The expected output should be:
{HEX}.A.Bss
{HEX}.A.Bsss
{HEX}.A.Bssss
Example 2
If I want to delete "{HEX}.A.Bss"
:
./test.sh "{HEX}.A.Bss"
The above example left only a string "{HEX}.A.Bs"
inside result.txt
. It should delete only the string "{HEX}.A.Bss"
. So, the expected output from the this example 2 should yield this:
{HEX}.A.Bs
{HEX}.A.Bsss
{HEX}.A.Bssss
May I know what is wrong with the code here?
.
will match any single character - not only literal.
- there's more in-depth discussion at What characters do I need to escape when using sed in a sh script? – steeldriver Jun 06 '20 at 13:13cat -vet inputfile
and post its output as well. – Rakesh Sharma Jun 07 '20 at 03:43