1752848 Members
3793 Online
108790 Solutions
New Discussion юеВ

Using sed with wildcards

 
SOLVED
Go to solution
Jeff Gyurko
Frequent Advisor

Using sed with wildcards


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.
20 REPLIES 20
Pat Lieberg
Valued Contributor

Re: Using sed with wildcards

How about:

sed 's/"[^"]*"/"replacement string"/' filename
curt larson_1
Honored Contributor

Re: Using sed with wildcards

how about trying something in awk

awk -F"\"" '
/TXT=/ {printf("%s\"%s",$1,"yourtext");
for ( i=3;iprint $NF;
}'

sed could be something like this

sed -e 's/^\(TXT=getsearch ("\)[:alnum:]*\(", "This is always.*$\)/\1yourtext\2/
Jeff Gyurko
Frequent Advisor

Re: Using sed with wildcards

Pat,

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.
Jeff Gyurko
Frequent Advisor

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.
TwoProc
Honored Contributor

Re: Using sed with wildcards

Jeff,

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.
We are the people our parents warned us about --Jimmy Buffett
Pat Lieberg
Valued Contributor

Re: Using sed with wildcards

oops, good catch. I overlooked that compeltely.

Glad I at least got you on the right track.
James R. Ferguson
Acclaimed Contributor

Re: Using sed with wildcards

HI Jeff:

How about:

perl -ne 's!(.+),(.+),(.+)!"newstring",$2,$3!;print' yourfile

...where "newstring" is the replacement for the THISISIT.

Regards!

...JRF...
Hein van den Heuvel
Honored Contributor

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.





Jeff Gyurko
Frequent Advisor

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.