- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Another way to do...
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
05-05-2004 12:02 AM
05-05-2004 12:02 AM
Another way to do...
At present I'm using the following script sintax to search in quit mode a key-word, then in the line where this key-word be, I need replace a specific string:
ed $FILE <<-! > /dev/null
/$KEYWORD
s/$CURRENT_STRING/$NEW_STRING/
w
q
!
This works fine, but any other secure/one-shot way to do this?
Pls forget Perl.
Rgds.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-05-2004 12:23 AM
05-05-2004 12:23 AM
Re: Another way to do...
this will change each single line that match your search criteria
Regards,
Jean-Luc
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-05-2004 12:27 AM
05-05-2004 12:27 AM
Re: Another way to do...
cat $file | sed 's%$KEYWORD%$CURRENT_STRING/$NEW_STRING%g/' > $file.new
mv $file.new $file
HTH,
Gideon
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-05-2004 12:30 AM
05-05-2004 12:30 AM
Re: Another way to do...
Just a thing, it work perfectly if I use nawk instead of awk (sub is only available with nawk on some platform).
Cheers
Nicolas
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-05-2004 12:34 AM
05-05-2004 12:34 AM
Re: Another way to do...
regards,
Jean-Luc
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-05-2004 12:43 AM
05-05-2004 12:43 AM
Re: Another way to do...
As for the solution:
awk -v cs="$CURRENT_STRING" -v ns="$NEW_STRING" "/$KEYWORD/ {sub(cs,ns)}
{print}" $file
Between the two '/' characters no variable replacement is done, so that's why I used double instead of single quotes. Now $KEYWORD is replaced by the shell. Only problem: $KEYWORD should NOT contain slashes ("/").
Difference between this and your script: the awk solutions (and sed) will replace, on all lines containing $KEYWORD, the first occorrence of $CURRENT_STRING, while your ed solution will only replace the first occurrence in the file.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-05-2004 01:31 AM
05-05-2004 01:31 AM
Re: Another way to do...
Suggestion from Elmar works fine but the $FILE update isn't on-line, I need a solution that replace and write into te same $FILE.
About the $KEYWORD occurrence, never mind about; is unique!
I'm looking for a live update of $FILE
Rgds.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-05-2004 01:38 AM
05-05-2004 01:38 AM
Re: Another way to do...
# perl -pi -e'....'
Why reject it? It's not neccesarily obfuscated. I can also write readable perl :]
Enjoy, Have FUN! H.Merijn
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-05-2004 01:43 AM
05-05-2004 01:43 AM
Re: Another way to do...
create the command file "mycom" :
/KEYWORD
:s/CURRENT_STRING/NEW_STRING/
:wq
run the command :
vi $FILE
Regards,
Jean-Luc
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-05-2004 03:36 AM
05-05-2004 03:36 AM
Re: Another way to do...
Please indicate me your suggestion in Perl
Remember, must be a live update of $FILE!
BRgds
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-05-2004 03:43 AM
05-05-2004 03:43 AM
Re: Another way to do...
that's all. This changes $CURRENT_STRING to $NEW_STRING only ONCE in any line that matches $KEYWORD
If you want to play more safe, and check that the patters are on a word bound, use
# perl -pi -e'm/\b$ENV{KEYWORD}\b/o and s/\b$ENV{CURRENT_STRING}\b/$ENV{NEW_STRING}/o' $FILE
and if you want to change all occurances in the matching lines
# perl -pi -e'm/\b$ENV{KEYWORD}\b/o and s/\b$ENV{CURRENT_STRING}\b/$ENV{NEW_STRING}/og' $FILE
And if you want special characters in $KEYWORD and $CURRENT_STRING to match literally,
# perl -pi -e'm/\b\Q$ENV{KEYWORD}\E\b/o and s/\b\Q$ENV{CURRENT_STRING}\E\b/$ENV{NEW_STRING}/og' $FILE
Enjoy, Have FUN! H.Merijn
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-05-2004 03:48 AM
05-05-2004 03:48 AM
Re: Another way to do...
Tim
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-05-2004 03:50 AM
05-05-2004 03:50 AM
Re: Another way to do...
You could also call awk from vi
ex - filename << EOF
1,$!awk '{ ... }'
wq
EOF
Cheers
Nicolas
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-05-2004 04:01 AM
05-05-2004 04:01 AM
Re: Another way to do...
Any sequential file updating requires a copying of the file to a temp file, removal of the original, and renaming the temp to the original name.
The reason of course is that any changes to a sequential text file requires possible shifting of the data.
perl can make this process invisible, but it still will use the copy and rename process.
my 2 cents...
-- Rod Hills
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-05-2004 07:31 AM
05-05-2004 07:31 AM
Re: Another way to do...
Just verify with ls -i
All the above solution will result in a new inode being used for the file.
Direct update is possible, but is tricky and has rules to follow. Really, you can only reasonably do it if the replacement string is equal in size to the to be replaced string or if there is some slop (spaces) to play with. For sake of the argument fine below an in-place solution. Oh... it's in perl. Oops. (tested, but adatted for readability but not retested)
Hein.
usage: perl update.pl your-file
#--- update.pl ----
$file = shift or die "please provide filename";
open(X,"+<$file") or die "failed to open $file";
$current=tell(X) # "=0". could leave this out.
while (
$next=tell(X);
if (/TEST/) {
seek(X,$current,0);
s/TEST/XXXX/;
print X;
seek(X,$next,0);
}
$current=$next;
}
----
So the script remembers the (byte)address for the next line to be read, then if the line is an update candidate then reseek to the start of the current line, write, and seek back to where we interrupted.
If you know only one update is needed then you can drop the re-positions the the loop will become something like:
while (
if (/KEYWORD/) {
seek(X,$last,0);
s/CURRENT/NEW/;
print X;
last;
}
$current=tell(X);
}
I'll leave making KEYWORD, CURRENT and NEW shell variables as an exercise to the reader :-). hint: shift from argv or read from ENV array