Replacing exact match

Hi All,

My Input file contains a 1000�s of lines in which I have to replace a
a string to the other. Here the problem is, I have the lines in my Input as below.

Cable Yes && !Pay TV && !ADS
\noUE  \Label="Cable Yes && !Pay TV && !ADS"

I want to replace exactly the string Cable Yes && !Pay TV && !ADS with Cable Yes && !Pay TV && !ADS && !MDS .
I do not want to replace when my string is preceding with some other words or ending with some other words.

More clearly I want to replace if it is like line 1 of my Input. I do not want to replace if it is like line 2 of my Input.

I have tried the below codes, but not able to get the results

sed 's/"Cable Yes && !Pay TV && !ADS"/"Cable Yes && !Pay TV && !ADS && !MDS"/' file

nawk '{sub(/\<Cable Yes && !Pay TV && !ADS \>/,"Cable Yes && !Pay TV && !ADS && !MDS")}1' file

sed "s/\< Cable Yes && !Pay TV && !ADS \>/Cable Yes && !Pay TV && !ADS && !MDS /" file

Can anyone help me on this ?

Regards,
am24

Hello am24,

Could you please try following and let me know if this helps you.

awk -vs1="Cable Yes && \!Pay TV && \!ADS" '($0 == s1){$0="Yes && !Pay TV && !ADS && !MDS"}1' Input_file  2>/dev/null

I have put 2>/dev/null for not to show the warnings/errors on standard output.

Thanks,
R. Singh

1 Like

How about

sed '/^Cable Yes && !Pay TV && !ADS$/ s/$/ \&\& !MDS/' file
Cable Yes && !Pay TV && !ADS && !MDS
\noUE  \Label="Cable Yes && !Pay TV && !ADS"

EDIT: or

awk '{sub(/^Cable Yes && !Pay TV && !ADS$/, "& \&\& !MDS")}1' file
Cable Yes && !Pay TV && !ADS && !MDS
\noUE  \Label="Cable Yes && !Pay TV && !ADS"

Try:

sed 's/^Cable Yes && !Pay TV && !ADS$/& \&\& !MDS/' file

--edit--

Similar answer already given by RudiC

Hi Ravinder,

Thanks for the code. I have tried it and getting the below error.

nawk -vs1="Cable Yes && !Pay TV && !ADS" '($0 == s1){$0="Cable Yes && !Pay TV && !ADS && !MDS"}1' cf.cr.crmm.amu 2>/dev/null
Pay: Event not found

I have got the same error message when i used the sed commands mentioned in my post.

Regards,
am24

This is due to the bash history expansion "feature", which can be switched off with

set +H

Or you can use single quotes, rather than double quotes, which will protect the exclamation marks from expansion by bash .

1 Like

Hello am24,

You should escape the strings as \!Pay TV and \!ADS , if you see my post it is exactly provided like as follows.

awk -vs1="Cable Yes && \!Pay TV && \!ADS" '($0 == s1){$0="Yes && !Pay TV && !ADS && !MDS"}1' Input_file 2>/dev/null

Thanks,
R. Singh

1 Like

Hi Ravinder, you appear to have left out "Cable " in the output...

It could be shortened a little:

awk -v s1='Cable Yes && !Pay TV && !ADS' '$0==s1 {$0=$0 " && !MDS"}1' Input_file
2 Likes

Hi All,

Thanks for your time. I have tried all the codes provided by you all. I am getting the same error message

Pay: Event not found

Ravinder, I have used the exact code given by you. But still the same error.
Also as per Scrutinizer comments, have used single quotes instead of double quotes. Still the same error.

I am working in Ksh shell. I do not have any idea about bash shell

Regards,
am24

Hello am24,

Could you please try this and let me know if this helps.

awk '{num=split("Cable Yes && !Pay TV && !ADS", array," ");for(i=1;i<=num;i++){if($0 ~ /^Cable/ && array==$i && $0 ~ /ADS$/){count++}};if(count==NF){$0=$0 " && !MDS"};count=""} 1'   Input_file

Output will be as follows.

Cable Yes && !Pay TV && !ADS && !MDS
chumma Cable Yes && !Pay TV && !ADS xycc
\noUE  \Label="Cable Yes && !Pay TV && !ADS"
 

As always on a Solaris/SunOS system, change awk to /usr/xpg4/bin/awk , /usr/xpg6/bin/awk , or nawk .
EDIT: Improving above solution as follows.

awk '{num=split("Cable Yes && !Pay TV && !ADS", array," ");for(i=1;i<=num;i++){if(array==$i){count++}};if(count==NF){$0=$0 " && !MDS"};count=""} 1'  Input_file

Thanks,
R. Singh

1 Like

Hi Ravinder,

I had tried the code. But same below error:

nawk '{num=split("Cable Yes && !Pay TV && !ADS", array," ");for(i=1;i<=num;i++){if(array==$i){count++}};if(count==NF){$0=$0 " && !MDS"};count=""} 1'  Input_file
Pay: Event not found

Scrutinizer, I have tried your code as well. But same error message.

I am trying to do it. But not getting the results. If anyone find the solution please post.

Regards,
am24

Are you sure that's a ksh error message? It strongly reminds me of an unsuccessful bash history search...
The proposals in post#3 (hopefully) don't interfere with history expansion...

Yes Rudi. I am working in Solaris system and using ksh shell.

I have tried all the codes above and facing the same error message.

Pay: Event not found

Regards,
am24

What output do you get from the command:

ps -t $(tty)

Hi Don,

Here the output below.

ps -t $(tty)
Variable syntax

Regards,
am24

OK. Run the command:

tty

which will give you something like /dev/terminal . Then run the command:

ps -t terminal

where terminal is the string after /dev/ in the output from the tty command and show us the output from that.

Hi Don,

Please find the below:

tty
/dev/pts/20
ps -t /pts/20
   PID TTY         TIME CMD
 13637 /pts/20     0:00 ps
 29563 /pts/20     0:00 csh

Regards,
am24

Do you see any inconsistency here between your claim that you are running ksh and the output above that clearly shows that you are running csh as your shell?

To change from running csh to actually run ksh , issue the command:

exec ksh

then, try one of the commands that was suggested above in this thread that was failing due to csh command history substitutions.

1 Like

Yes Don. I did not understand why it showed csh in there while i am working in ksh shell.
Anyways, i will try as you suggested and will let you know the results.

Thanks & Regards,
am24

---------- Post updated at 01:57 AM ---------- Previous update was at 01:50 AM ----------

Hi Don,

Just need clarification. when i give the command exec ksh , the prompt chnaged to $ . where as earlier it is -> .
So the shell till now i worked is csh , is it ? Now with the command exec ksh i have moved to ksh shell. Am i correct ?

Regards,
am24

---------- Post updated at 02:08 AM ---------- Previous update was at 01:57 AM ----------

Hi Don,

I have given the command exec ksh . Then i tried all of the codes suggested by each one of you. I did not get any error like earlier but there was no required modifications done on my Input(Output is same as the Input).

could you please suggest me how can i proceed further on this ?

Thanks & Regards,
am24

Again: difficult to believe. I'd sooner believe that all of the proposals have been tested before being posted here. Please give some evidence of your assertion by posting those commands running and their output.