- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - Linux
- >
- Scripting Help
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-14-2007 04:09 PM
06-14-2007 04:09 PM
I need to grep for a specific pattern in all the files in a directory and change them to a new pattern.
I tried with sed.
for i in
do
sed 's/old/new $i > /newdirectory/$i
done
it works but the problem is that the files are being truncated when piping to the new file
is there a way i can change the pattern in the same file itself?
thanks in advance
Solved! Go to Solution.
- Tags:
- sed
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-14-2007 07:17 PM
06-14-2007 07:17 PM
Solutionsed 's/old/new/' $i > /newdirectory/$i
You aren't piping but redirecting. What was the truncation like?? Number of lines or width of them? Long long are they??
Changing a pattern the file can be done with perl, search some previous thread.
Some of them show using ex(1) and ed(1) to do this.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-14-2007 07:32 PM
06-14-2007 07:32 PM
Re: Scripting Help
i have some scripts which does it as follows:
e.g.
for file in `find . -name "*.htm"`
do
echo "Converting $file ..."
cat $file | sed s/$1/$2/g> /tmp/newfile
cp /tmp/newfile $file
done
hope this helps too!
kind regards
yogeeraj
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-14-2007 07:33 PM
06-14-2007 07:33 PM
Re: Scripting Help
you should perhaps add "g" to your sed statement, e.g.:
sed 's/old/new/g' $i > /tmp/$i
else you will only have the first occurrance replaced.
regards,
John K.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-14-2007 07:37 PM
06-14-2007 07:37 PM
Re: Scripting Help
another way to do it would be using PERL:
perl -pi -e 's/old/new/g' $file
you can replace the sed command in the previous script with this...
hope this helps too!
kind regards
yogeeraj
- Tags:
- Perl
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-14-2007 07:37 PM - edited 09-29-2011 03:39 AM
06-14-2007 07:37 PM - edited 09-29-2011 03:39 AM
Re: Scripting Help
A perl solution was just added to:
http://h30499.www3.hp.com/t5/System-Administration/Pattern-replacing/m-p/5053121
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-17-2007 02:23 PM
06-17-2007 02:23 PM
Re: Scripting Help
if you intend to change occurances in multiple files within a directory I'd create a for loop, otherwise simply use a perl command.
MULTIFILES
for fn in `ls`
do
perl -pi -e s/old/new/g $fn
done
Single files
perl -pi -e s/old/new/g filename
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-17-2007 08:07 PM
06-17-2007 08:07 PM
Re: Scripting Help
only a sugegstion:
for i in $(grep old *)
do
sed 's/old/new' $i > /newdirectory/$i
done
This will increase teh speed in cas eyou have a lot of files in your dir, and yoy could also refine you grep to get only ascii file, etc.
HTH,
Art