Finding a particular string in a xml file using gr

2019-09-21 20:44发布

问题:

#!/bin/bash
I = `grep -R --include="*.xml" "Non Frontal Face" /home/ashutosh/Desktop/imgdone | grep "Frontal Face" > temp.txt`
cut -d':' -f1 temp.txt > out.txt
cat out.txt

What I want is that all the xmls having "Frontal Face" should only be displayed, but instead of that in the result I get all the files and wherever "Non Frontal Face" string is present, I get "Frontal Face" highlighted in bold.

Please help me out !! ?

回答1:

If I understand correctly, you want to remove the "Non Frontal Face" from the "Frontal Face" matches?

grep -R --include="*.xml" "Frontal Face" /home/ashutosh/Desktop/imgdone |
grep -v "Non Frontal Face"

If you want just the file nmes, pipe that to cut -d : -f 1. There is no need for a temporary file, and the odd command substitution with a syntax error was completely superfluous.

(If you fixed the syntax error with the whitespace around the equals sign, I would end up containing an empty string, because you were redirecting all the output to a file anyway. You were not using I for anything anyhow, so it was doubly or triply superfluous.)



回答2:

Maybe this can help

grep -Rl "Frontal Face" *.xml |grep -v "Non Frontal Face" > out.txt

-l tell grep to print only the name of the file.