- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Re: change contents in files using grep ?
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
Discussions
Discussions
Forums
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
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
тАО07-03-2008 10:52 PM
тАО07-03-2008 10:52 PM
I have a directory with 30+ files that look like this >>
"binary
get "syutfoft/forec1p" tfile1.zip
quit"
I want to change syufoft to syhfoft on each file automatically.
How can this be done ??
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-03-2008 11:22 PM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-03-2008 11:27 PM
тАО07-03-2008 11:27 PM
Re: change contents in files using grep ?
>change contents in files using grep ?
check man grep and you get this:
"grep, egrep, fgrep - search a file for a pattern"
so with grep you cannot change the content of a file...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-03-2008 11:57 PM
тАО07-03-2008 11:57 PM
Re: change contents in files using grep ?
for file in /directory/of/your/files/* ; do
echo "1,\$s/syufoft/syhfoft/\nw\nq" |ed -s $file
done
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-04-2008 04:44 AM
тАО07-04-2008 04:44 AM
Re: change contents in files using grep ?
> Laurent: perl is useless there
I think you meant to answer the poster's original question of can you "change contents in files using grep?".
Obviously, that answer is "no". However, Perl's inplace update ('-i') as Oviwan used, *will* effect the change.
If you simply use '-i' without the argument ".old" as '-i.old', then no backup copy of the original file suffixed with ".old" will be made.
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-04-2008 12:16 PM
тАО07-04-2008 12:16 PM
Re: change contents in files using grep ?
All you can do with grep is use -v to remove whole lines.
To change files, you typically use sed(1) (or Oviwan's and JRF's perl).
for file in $(ls /directory/of/your/files) ; do
sed 's/syufoft/syhfoft/' $file > $file.new
mv $file.new $file
done
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-04-2008 01:02 PM
тАО07-04-2008 01:02 PM
Re: change contents in files using grep ?
The attached script will do exactly what your want. I wrote when I had the same task as you described. It has several options including showing the before and after lines that are changed. Without the -w, you get to preview exactly what will be changed.
Usage: chgafile [ -w ] [ -q ] [ -s ] old-string new-string [ filename(s) ]
-w to write changes (default = preview only)
-q quiet mode (no before/after lines)
-s silent (no output at all except for file or STDOUT)
-s also turns on -q for no output at all
old-text and new-text are the strings
one or more files to change or none for STDIN
Bill Hassell, sysadmin
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-10-2008 05:07 PM
тАО07-10-2008 05:07 PM
Re: change contents in files using grep ?
for file in /directory/of/your/files/* ; do
echo "1,\$s/syufoft/syhfoft/\nw\nq" |ed -s $file
done
??
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-10-2008 05:22 PM
тАО07-10-2008 05:22 PM
Re: change contents in files using grep ?
for file in $(ls /directory/of/your/files) ; do
perl -i.old -ple "s/syufoft/syhfoft/" $file
done
>> hi this script created the .old file but didnt change the conents of each file.
still remains with syufoft instead of syhfoft
?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-10-2008 05:29 PM
тАО07-10-2008 05:29 PM
Re: change contents in files using grep ?
the following script worked like a dream ..
for file in $(ls /directory/of/your/files) ; do
perl -i -ple "s/syufoft/syhfoft/" $file