简体   繁体   中英

Delete a line in multiple files that contain a string in Linux

I am moving some of my pages from 8859 to UTF so I just included the UTF in my file that is included on most pages. I am trying to erase the line with the charset definition from pages with that include.

find . -type f -exec grep -lr 'headIncluded' {} + -exec sed -i '/meta http-equiv=\\"content-type\\" content=\\"text\\/html; charset/d' {} \\;

I thought this one should work, but I noticed that it erased the line in a few pages where headIncluded was not present. Any suggestions to what is wrong with this command?

为什么不尝试:

grep -lr "headIncluded" /pathtodirectory/* | xargs sed -i '/meta http-equiv=\"content-type\" content=\"text\/html; charset/d'

When you use + to terminate the -exec , the command is run with multiple files. If any of those files contain the string, the grep will succeed. Also, from the documentation "this variant of -exec always returns true", so the return value of grep is irrelevant.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM