?
Solved

Simple file editing question

Posted on 2002-05-31
18
Medium Priority
?
171 Views
Last Modified: 2013-12-16
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.
0
Comment
Question by:dryzone
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 11
  • 4
  • 2
  • +1
18 Comments
 
LVL 14

Expert Comment

by:chris_calabrese
ID: 7047795
find . -type f | while read file
do  sed 's/something/something-else/' \
      < "$file" > "$file.tmp" \
    && mv "$file.tmp" "$file"
done
   
0
 
LVL 1

Author Comment

by:dryzone
ID: 7047859
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'/exclude1=
0
 
LVL 14

Expert Comment

by:chris_calabrese
ID: 7048156
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'/exclude1=/"
0
Will your db performance match your db growth?

In Percona’s white paper “Performance at Scale: Keeping Your Database on Its Toes,” we take a high-level approach to what you need to think about when planning for database scalability.

 
LVL 1

Author Comment

by:dryzone
ID: 7048287
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'/exclude1=Half.com/' < "$file" > "$file.tmp" && mv "$file.tmp" "$file"
                       done
0
 
LVL 1

Author Comment

by:dryzone
ID: 7048969
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.
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 7049815
find . -type f -exec perl -i -pe 's/(.*entry=)something/$1something-else/' {} \;
0
 
LVL 3

Expert Comment

by:DVB
ID: 7053073
Quote the #: \#
0
 
LVL 1

Author Comment

by:dryzone
ID: 7054316
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/' {} \;
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 7054341
> 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/' {} \;
0
 
LVL 1

Author Comment

by:dryzone
ID: 7055078
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.
0
 
LVL 1

Author Comment

by:dryzone
ID: 7055093
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.
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 7055888
# 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 ...
0
 
LVL 1

Author Comment

by:dryzone
ID: 7056597

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]#

0
 
LVL 51

Accepted Solution

by:
ahoffmann earned 320 total points
ID: 7056670
find . -type f -exec perl -i.old -pe 's/'"'"'NEW'"'"'/new line/' {} \;
0
 
LVL 1

Author Comment

by:dryzone
ID: 7057172
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]$
0
 
LVL 1

Author Comment

by:dryzone
ID: 7057204
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]$
0
 
LVL 1

Author Comment

by:dryzone
ID: 7057238
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]$
0
 
LVL 1

Author Comment

by:dryzone
ID: 7057329
My apologies,
I did not use the semicolon.
It works fine
0

Featured Post

Moving data to the cloud? Find out if you’re ready

Before moving to the cloud, it is important to carefully define your db needs, plan for the migration & understand prod. environment. This wp explains how to define what you need from a cloud provider, plan for the migration & what putting a cloud solution into practice entails.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

SSH (Secure Shell) - Tips and Tricks As you all know SSH(Secure Shell) is a network protocol, which we use to access/transfer files securely between two networked devices. SSH was actually designed as a replacement for insecure protocols that sen…
Join Greg Farro and Ethan Banks from Packet Pushers (http://packetpushers.net/podcast/podcasts/pq-show-93-smart-network-monitoring-paessler-sponsored/) and Greg Ross from Paessler (https://www.paessler.com/prtg) for a discussion about smart network …
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial
Suggested Courses
Course of the Month11 days, 16 hours left to enroll

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question