- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - Linux
- >
- Using sed with wildcards
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
тАО08-26-2005 03:37 AM
тАО08-26-2005 03:37 AM
Hi,
I've a need to substitute some string in a line where I do not what the string is currently, only what I need it to be. I've been trying, with no luck to use sed so I'm turning to the group. A sample line follows:
TXT=getsearch ("THISISIT", "This is always the same: ",3);
Now I have a need to replace THISISIT which I will not know with something else that I do know. The TXT= is unique so I could key off that to get the target line I want to modify. I've tried a number of things, but I'm either not qualifying any lines or I get something like TXT=getsearch ("THISISIT"TXT=getsearch ("THISISIT", "This is always the same: ",3);
Hope I've described it well enough.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-26-2005 04:05 AM
тАО08-26-2005 04:05 AM
Re: Using sed with wildcards
sed 's/"[^"]*"/"replacement string"/' filename
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-26-2005 04:07 AM
тАО08-26-2005 04:07 AM
Re: Using sed with wildcards
awk -F"\"" '
/TXT=/ {printf("%s\"%s",$1,"yourtext");
for ( i=3;i
}'
sed could be something like this
sed -e 's/^\(TXT=getsearch ("\)[:alnum:]*\(", "This is always.*$\)/\1yourtext\2/
- Tags:
- awk
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-26-2005 04:25 AM
тАО08-26-2005 04:25 AM
Re: Using sed with wildcards
It worked to a point. It changed any occurance of that to THISISIT. Since there are other instances of it in the file they were all changed to THISISIT.
What I did do, taking your suggestion is add the qualifier PW1 to the sed like:
sed '/PW1=/s/"[^"]*"/"replacement string"/' filename.
It changed the one line with PW1.
Thanks a bunch.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-26-2005 04:32 AM
тАО08-26-2005 04:32 AM
Re: Using sed with wildcards
Sorry Pat, the correct line was
sed '/TXT=/s/"[^"]*"/"replacement string"/' filename.
Not PW1. PW1 was still in my cut/paste buffer.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-26-2005 04:36 AM
тАО08-26-2005 04:36 AM
Re: Using sed with wildcards
I've handled this same problem in the past by generating a sed command file on each occurence of the new "unknown" with a replacement command for sed.
I generate that file into /var/tmp/sed.$$
and then run my sed against that new command file with the "-f" option for sed.
Probably a little clunky, but it has allowed me to get the job done everytime, as it is easy to build rather complex multiple sed command sets into a file and use it a base to change many files using the one command file.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-26-2005 04:53 AM
тАО08-26-2005 04:53 AM
Re: Using sed with wildcards
Glad I at least got you on the right track.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-26-2005 04:53 AM
тАО08-26-2005 04:53 AM
Re: Using sed with wildcards
How about:
perl -ne 's!(.+),(.+),(.+)!"newstring",$2,$3!;print' yourfile
...where "newstring" is the replacement for the THISISIT.
Regards!
...JRF...
- Tags:
- Perl
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-26-2005 05:02 AM
тАО08-26-2005 05:02 AM
Re: Using sed with wildcards
perl -pe 's/"$1"/"new text"/ if /^TXT.*\("(\w+)"/' old > new
$1 gets the (\w+) on a match.
The "" around $1 and new word in the substitute are belts and suspenders.... in case the the old text is 'TXT' or such.
The $1 can be remembered eg: $thisisit=$1
and used for further substitutes in different context in the file.
perl -pe '$thisisit = $1 if /^TXT.*\("(\w+)"/;
s/$thisisit/new text/ if defined ($thisisit)' old > new
Check out the perl -i option for 'in place' edits.
Hein.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-26-2005 06:30 AM
тАО08-26-2005 06:30 AM
Re: Using sed with wildcards
James,
Thanks, but your solution changed every occurance and chopped off any text preceeding what I changed.
ie: every changed line started with ^"THISISIT
Trouble is, unlike sed, my breadth of knowledge in perl is not that much so I can make modifications to your suggestion.