Replacing exact match


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replacing exact match
# 1  
Old 05-04-2016
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.

Code:
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

Code:
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
# 2  
Old 05-04-2016
Hello am24,

Could you please try following and let me know if this helps you.
Code:
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
This User Gave Thanks to RavinderSingh13 For This Post:
# 3  
Old 05-04-2016
How about
Code:
sed '/^Cable Yes && !Pay TV && !ADS$/ s/$/ \&\& !MDS/' file
Cable Yes && !Pay TV && !ADS && !MDS
\noUE  \Label="Cable Yes && !Pay TV && !ADS"


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


Last edited by RudiC; 05-04-2016 at 08:47 AM..
# 4  
Old 05-04-2016
Try:
Code:
sed 's/^Cable Yes && !Pay TV && !ADS$/& \&\& !MDS/' file

--edit--

Similar answer already given by RudiC
# 5  
Old 05-04-2016
Hi Ravinder,

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

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

Code:
Pay: Event not found

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

Regards,
am24
# 6  
Old 05-04-2016
Quote:
Originally Posted by am24
[..]
Code:
Pay: Event not found

I have got the same error message when i used the sed commands mentioned in my post.
[..]
This is due to the bash history expansion "feature", which can be switched off with
Code:
set +H

Or you can use single quotes, rather than double quotes, which will protect the exclamation marks from expansion by bash.
This User Gave Thanks to Scrutinizer For This Post:
# 7  
Old 05-04-2016
Quote:
Originally Posted by am24
Hi Ravinder,
Thanks for the code. I have tried it and getting the below error.
Code:
nawk -vs1="Cable Yes && !Pay TV && !ADS" '($0 == s1){$0="Cable Yes && !Pay TV && !ADS && !MDS"}1' cf.cr.crmm.amu 2>/dev/null

Code:
Pay: Event not found

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

You should escape the strings as \!Pay TV and \!ADS, if you see my post it is exactly provided like as follows.
Code:
awk -vs1="Cable Yes && \!Pay TV && \!ADS" '($0 == s1){$0="Yes && !Pay TV && !ADS && !MDS"}1' Input_file 2>/dev/null

Thanks,
R. Singh
This User Gave Thanks to RavinderSingh13 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to update file based on partial match in field1 and exact match in field2

I am trying to create a cronjob that will run on startup that will look at a list.txt file to see if there is a later version of a database using database.txt as the source. The matching lines are written to output. $1 in database.txt will be in list.txt as a partial match. $2 of database.txt... (2 Replies)
Discussion started by: cmccabe
2 Replies

2. Shell Programming and Scripting

Help with nawk (Exact Match)

I have a file with the contents below "lets say the name of the file is abcxyz" shown at the end of this. I am using nawk to find the exact ip address and the 6 lines after the match is found using the following nawk statement /usr/bin/nawk "/111.46.14.107/,printed==6 { ++printed; print; }"... (7 Replies)
Discussion started by: knijjar
7 Replies

3. UNIX for Dummies Questions & Answers

Exact match question

Hi guys, I am using Centos 6.3. Actually I posted similar question but I still have some minor problem need be fixed. I have two files, file1:target: gi|57529786|ref|NM_001006513.1| mfe: -31.4 kcal/mol p-value: 0.006985 target: gi|403048743|ref|NM_001271159.1| mfe: -29.6 kcal/mol p-value:... (11 Replies)
Discussion started by: yuejian
11 Replies

4. Shell Programming and Scripting

Exact match using sed

I would like replace all the rows in a file if a row has an exact match to number say 21 in a tab delimited file. I want to delete the row only if it has 21 any of the rows but it should not delecte the row that has 542178 or 563421. I tried this sed '/\<21\>/d' ./inputfile > output.txt ... (7 Replies)
Discussion started by: Kanja
7 Replies

5. Shell Programming and Scripting

Replacing exact directory variable

I am making a script to relocate a project file. I have all of the variables in place and everything is working except: the first variable changes everytime it passes thru a loop. The second is a constant value. when I run that it does what I want...but incorrectly. It is finding... (2 Replies)
Discussion started by: gentlefury
2 Replies

6. Shell Programming and Scripting

Exact match and #

Hi friends, i am using the following grep command for exact word match: >echo "sachin#tendulkar" | grep -iw "sachin" output: sachin#tendulkar as we can see in the above example that its throwinng the exact match(which is not the case as the keyword is sachin and string is... (6 Replies)
Discussion started by: neelmani
6 Replies

7. Shell Programming and Scripting

sed - replacing on the right of a pattern and looking for exact word?

how would you get SED to do the following, say you have the following lines in a text file: user=tigger some text some text some text some text some text some text user=ted some text some text some text some text some text some text user=thekingofrockandroll you want to find any line... (15 Replies)
Discussion started by: rich@ardz
15 Replies

8. Shell Programming and Scripting

exact string match ; search and print match

I am trying to match a pattern exactly in a shell script. I have tried two methods awk '/\<mpath${CURR_MP}\>/{print $1 $2}' multipath perl -ne '/\bmpath${CURR_MP}\b/ and print' /var/tmp/multipath Both these methods require that I use the escape character. I am guessing that is why... (8 Replies)
Discussion started by: bash_in_my_head
8 Replies

9. Shell Programming and Scripting

Exact Word Match

I'm trying to find a exact word match but couldn't do it. ABC ABC_NE Searching for ABC_NE tried grep -w </ABC_NE/> grep "^ABC_NE$" but didn't worked , any awk variants would also help. ---------- Post updated at 08:40 AM ---------- Previous update was at 06:48 AM ---------- I... (2 Replies)
Discussion started by: dinjo_jo
2 Replies

10. Shell Programming and Scripting

How to get exact match sentences?

Hi, I have sentences like this: $sent= Protein modeling studies reveal that the RG-rich region is part of a three to four strand antiparallel beta-sheet, which in other RNA binding protein functions as a platform for nucleic acid interactions. Heterogeneous nuclear ribonucleoparticle... (19 Replies)
Discussion started by: vanitham
19 Replies
Login or Register to Ask a Question