I have two files
file1.txt
a
b
c
file2.txt
a
b
c
d
Now using grep command
grep -vf file1.txt file2.txt
will give me output as ->
d
But using this I can't save this result in any one of files file1
or file2
. I want to simply overwrite this result in any one of the files.
How can this be done using grep?
CodePudding user response:
But using this I cant save this result in any one of files file1 or file2. I want to simply overwrite this result in any one of the files. How can this be done using grep
you can redirect the output to a file like this:
grep -vf file1.txt file2.txt > tmp_file && mv tmp_file file1.txt
Notice that with grep you cannot use the same input file as output file. To solve this you create a temporary file and then rename it to file1(2).txt
CodePudding user response:
If sed
is an option, you can delete the matches within a loop and save in place
while IFS= read -r line; do
sed -i "/$line/d" file2.txt
done < file1.txt
Output
$ cat file2.txt
d