I want to learn how to delete from a txt file the line that contains a word that user typed in a variable I've tried grep -v but then clears the whole content of the file Help?!!!
1 Answer
Here is an example program how to archive this:
Save this in example.sh:
#!/bin/bash
word="$1"
grep -F -v "$word"
Save this in test.txt:
Hello world
foo bar
baz bau
Call the program and feed it with the file test.txt on standard input:
chmod u+x example.sh # Need to do this only once per script (*.sh file)
./example.sh Hello < test.txt
Output ("Hello world" line is deleted):
foo bar
baz bau
7 Comments
Ardit.A
Can I ask you something else?
akraf
If its not related to this answer, better ask a new question for that (if there is not already one out there)
Ardit.A
Its related with what you said about the regular expression in sed
akraf
Go ahead! (Sidenote: you can also use @mention to notify users in a comment)
Ardit.A
I want to replace morse code with english letters for example '. -' with 'a' how to do that with sed @akraf
|
sed --in-place '/some string here/d' yourfilesedaccepts regular expressions, so if you do for example something likesed "/./d"it does not delete just lines with dots, but deletes everything (as.stands for "any character" in a regular expression)