Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

deleting lines from a file recrsivly

Status
Not open for further replies.

gfunk123

IS-IT--Management
May 22, 2001
143
GB
Hi

Is there any way from a starting directory (say /usr) to search for files with lines of text that contain the string "hello" and delete the whole line from the file (and ONLY that line)

I have a whole load of html files through a directory structure that have an unwanted line which i need to remove

thanks and regards
Gary
 
Something like this ?
find /usr -name '*.html' | while read f
do sed 'g/hello/d' "$f" >"$f.tmp" && mv "$f.tmp" "$f"
done

Hope This Help, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884
 
could you just explain whats happening on that sed line please, im a bit confused where the delete line command is
 
or something like that [a variation on the same theme]:

#!/bin/ksh
find . -type f | while read file; do ex - ${file} <<EOF
g/^hello/d
wq!
EOF
done;


vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
Sorry for the typo:
sed '/hello/d'

Hope This Help, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884
 
the delete is the "/d"

man sed will help you, and if you intend to do real work on Unix then you really need to know at least the basics.



Regards

Frederico Fonseca
SysSoft Integrated Ltd
 
I tried this

find /usr -name '*.html' | while read f
do sed '/hello/d' "$f" >"$f.tmp" && mv "$f.tmp" "$f"
done

and i get the following error

sed: command garbled: /var/apache/htdocs/index.html
+ read f
+ /hello/d


any ideas where im going wrong ( i put a carriage return after the first line (read f)
 
You may try this:
find /usr -name '*.html' | while read f
do sed -e '/hello/d' <"$f" >"$f.tmp" && mv "$f.tmp" "$f"
done
BTW are you sure the sed command is '/hello/d' ?

Hope This Help, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884
 
As far as you don't use special shell character in the sed script you can use either single quotes (') or double quotes (") like this:
sed -e "/_files/d" </path/to/input >/path/to/output

Hope This Help, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884
 
Or try this:
Code:
find /usr -type f -name "*.html" | xargs perl -i -n -e 'print unless /hello/;'
Note, try this out with [tt]-i.bak[/tt] first, which will create a backup file.
Cheers, Neil
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top