I have installed WSL Ubuntu 20.04 on windows 11. I am trying to run the following File:
echo "Delete Lines that contain Given Word: "echo "Before Deletion: "cat "$1"sed -i.bak "/$2/d" "$1"echo " "echo "After Deletion: "cat "$1"This code deletes all lines in a given text file that contain a given word. The command line argument I am giving in terminal is:
bash delfile.sh test.txt I where delfile.sh is the shell script, test.txt is the text document and "I" is the word that gets its entire line deleted.
The text in test.txt is:
Hello Friend!My Good Name IsJohnny Jack.I come from the planet Earthand I live in Italy.I like Pasta.Everytime I run the code, I get the following output:
Delete Lines that contain Given Word:Before Deletion:cat: 'test.txt'$'\r': No such file or directory: No such file or directoryAfter Deletion:Hello Friend!My Good Name IsJohnny Jack.I come from theplanet Earthand I live in Italy.I like Pasta.As you can see, the last cat $1 is able to print the contents of the file, but the first cat and sed commands give error "No such file or directory" even though all files are in same folder.
I tried running this same setup on a different system and it works fine there, but just not on the one I use frequently. I tried
sed -i 's/\r//' text.txt to convert ASCII text, with CRLF line terminators to just ASCII text, but that also didn't work. All solutions I found on the internet indicate that the problem is something related to how windows and linux handle endline differently, but seeing as this code runs perfectly fine on a different system, and that the last cat command also works, I doubt that that is the case.
If the shell file simply contains sed -i.bak "/$2/d" "$1" then my file gets edited and the lines containing "I" get deleted, but as soon as another cat $1 comes, the last one is executed and the ones above give error.
If anyone can help sort out this issue, it will be much appreciated.