dryzone
asked on
Simple file editing question
Let's say I have a directory with 1000 files each containing a line with text e.g "entry=" which I want to replace with another text entery.
If someone has an emacs or commandline or script solution it would be appreciated.
If someone has an emacs or commandline or script solution it would be appreciated.
ASKER
Ok were halfway there
Unfortunately the files contain text which is commented out. I need to uncomment these lines in bulk fashion.
The hash seems to create problems.
I checked with an echo that the filenames were read with your script and passed to sed, but the hash....seems to kick sed out.
sed 's/#exclude1='NEW'/exclude 1=
Unfortunately the files contain text which is commented out. I need to uncomment these lines in bulk fashion.
The hash seems to create problems.
I checked with an echo that the filenames were read with your script and passed to sed, but the hash....seems to kick sed out.
sed 's/#exclude1='NEW'/exclude
The hash isn't the problem, it's the single-quotes inside the single-quoted sed arguments, plus the lack of a trailing /
sed "s/#exclude1='NEW'/exclude 1=/"
sed "s/#exclude1='NEW'/exclude
ASKER
No, I just did not copy all (see below), if sed has problems with quotes, then how the heck am I going to remove/replace them?
find . -type f | while read file
do
echo reading $file
sed 's/#exclude1='NEW'/exclude 1=Half.com /' < "$file" > "$file.tmp" && mv "$file.tmp" "$file"
done
find . -type f | while read file
do
echo reading $file
sed 's/#exclude1='NEW'/exclude
done
ASKER
OK you solved half of the problem.
Now how do I get rid of lines containing ' '
I could not figure out from sed man page.
Now how do I get rid of lines containing ' '
I could not figure out from sed man page.
find . -type f -exec perl -i -pe 's/(.*entry=)something/$1s omething-e lse/' {} \;
Quote the #: \#
ASKER
4 ahoffmann
Did not work.
Trying to replace e.g. 'NEW' in the files with e.g. hello failed. 'NEW' remained in the files.
find . -type f -exec perl -i -pe 's/(.*entry=)'NEW'/hello/' {} \;
Did not work.
Trying to replace e.g. 'NEW' in the files with e.g. hello failed. 'NEW' remained in the files.
find . -type f -exec perl -i -pe 's/(.*entry=)'NEW'/hello/'
> find . -type f -exec perl -i -pe 's/(.*entry=)'NEW'/hello/' {} \;
this cannot work. Did you mean:
find . -type f -exec perl -i -pe 's/(.*entry=)NEW/$1hello/' {} \;
or
find . -type f -exec perl -i -pe 's/(.*entry=)'"'"'NEW'"'"' /$1hello/' {} \;
this cannot work. Did you mean:
find . -type f -exec perl -i -pe 's/(.*entry=)NEW/$1hello/'
or
find . -type f -exec perl -i -pe 's/(.*entry=)'"'"'NEW'"'"'
ASKER
The text files 100's of them contain the following text.
'NEW'
#exclude2='a'
#exclude3='b'
#exclude4='c'
#exclude5='d'
Of which I want to replace 'NEW' with other text -e.g., new line, to yield
new line
#exclude2='a'
#exclude3='b'
#exclude4='c'
#exclude5='d'
The examples above did not work.
'NEW' was not replaced with the new text e.g. new line
Note: I dont just want to replace NEW but rather 'NEW' , which is why I have problems with my usual methods processing batches of files.
'NEW'
#exclude2='a'
#exclude3='b'
#exclude4='c'
#exclude5='d'
Of which I want to replace 'NEW' with other text -e.g., new line, to yield
new line
#exclude2='a'
#exclude3='b'
#exclude4='c'
#exclude5='d'
The examples above did not work.
'NEW' was not replaced with the new text e.g. new line
Note: I dont just want to replace NEW but rather 'NEW' , which is why I have problems with my usual methods processing batches of files.
ASKER
The text files 100's of them contain the following text.
'NEW'
#exclude2='a'
#exclude3='b'
#exclude4='c'
#exclude5='d'
Of which I want to replace 'NEW' with other text -e.g., new line, to yield
new line
#exclude2='a'
#exclude3='b'
#exclude4='c'
#exclude5='d'
The examples above did not work.
'NEW' was not replaced with the new text e.g. new line
Note: I dont just want to replace NEW but rather 'NEW' , which is why I have problems with my usual methods processing batches of files.
'NEW'
#exclude2='a'
#exclude3='b'
#exclude4='c'
#exclude5='d'
Of which I want to replace 'NEW' with other text -e.g., new line, to yield
new line
#exclude2='a'
#exclude3='b'
#exclude4='c'
#exclude5='d'
The examples above did not work.
'NEW' was not replaced with the new text e.g. new line
Note: I dont just want to replace NEW but rather 'NEW' , which is why I have problems with my usual methods processing batches of files.
# the syntax in the file listed in you last comment is completely different to what you described before
# so it's no suprise that my (and probably all other) suggestions do not work !!
#
# following works for exactly the example you gave above:
find . -type f -exec perl -i.old -pe 's/'"'"'NEW'"'"'/new line/' {} \;
# if it does not work, please copy&paste this example to a file, and try again, twice ...
# so it's no suprise that my (and probably all other) suggestions do not work !!
#
# following works for exactly the example you gave above:
find . -type f -exec perl -i.old -pe 's/'"'"'NEW'"'"'/new line/' {} \;
# if it does not work, please copy&paste this example to a file, and try again, twice ...
ASKER
I posed it as a general example i.e. replacing ANY line of text with another. It was found that SED cannot replace lines encapsulated in quotation. I then specialised it to a more explicit example to deal with the remainig problem of quotations which is where we are now.
I just made it more explicit.
The perl string contains a problem as above
[root@gateway test]# find . -type f -exec perl -i.old -pe 's/'"'"'NEW'"'"'/new line/' {}
find: missing argument to `-exec'
[root@gateway test]#
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I just copied your string and executed it.... Still there is an argument missing to exec switch as mentioned above
[Liebrecht@gateway test]$ find . -type f -exec perl -i.old -pe 's/'"'"'NEW'"'"'/new line/' {}
find: missing argument to `-exec'
[Liebrecht@gateway test]$
[Liebrecht@gateway test]$ find . -type f -exec perl -i.old -pe 's/'"'"'NEW'"'"'/new line/' {}
find: missing argument to `-exec'
[Liebrecht@gateway test]$
ASKER
I just copied your string and executed it.... Still there is an argument missing to exec switch as mentioned above
[Liebrecht@gateway test]$ find . -type f -exec perl -i.old -pe 's/'"'"'NEW'"'"'/new line/' {}
find: missing argument to `-exec'
[Liebrecht@gateway test]$
[Liebrecht@gateway test]$ find . -type f -exec perl -i.old -pe 's/'"'"'NEW'"'"'/new line/' {}
find: missing argument to `-exec'
[Liebrecht@gateway test]$
ASKER
I just copied your string and executed it.... Still there is an argument missing to exec switch as mentioned above
[Liebrecht@gateway test]$ find . -type f -exec perl -i.old -pe 's/'"'"'NEW'"'"'/new line/' {}
find: missing argument to `-exec'
[Liebrecht@gateway test]$
[Liebrecht@gateway test]$ find . -type f -exec perl -i.old -pe 's/'"'"'NEW'"'"'/new line/' {}
find: missing argument to `-exec'
[Liebrecht@gateway test]$
ASKER
My apologies,
I did not use the semicolon.
It works fine
I did not use the semicolon.
It works fine
do sed 's/something/something-els
< "$file" > "$file.tmp" \
&& mv "$file.tmp" "$file"
done