Replacing exact match


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replacing exact match
# 29  
Old 05-05-2016
Hi Ravinder,

I have tried it but there is no difference with the earlier output i got. Its the same output from above code.

Regards,
am24
# 30  
Old 05-05-2016
This then maybe?

I believe it matches your latest requirements for both Cable Yes && Pay TV && !ADS and Cable Yes && !Pay TV && !ADS IF it is the only thing occurring on a line with for the rest only spaces and/or the text\noUE behind it

Code:
sed 's/^\([[:blank:]]*Cable Yes && !\{0,1\}Pay TV && !ADS\) \{8\}\([[:blank:]]*\(\\noUE\)\{0,1\}[[:blank:]]*\)$/\1 \&\& !MDS\2/' file

or perhaps you mean:
Code:
sed 's/^\([[:blank:]]*Cable Yes && !\{0,1\}Pay TV && !ADS\)[[:blank:]]\{1,8\}/\1 \&\& !MDS/'  file


Last edited by Scrutinizer; 05-05-2016 at 12:36 PM..
This User Gave Thanks to Scrutinizer For This Post:
# 31  
Old 05-06-2016
Hi Scrutinizer,

Thanks for teh reply.I have tried the above codes but the replacement did not happen.

Regards,
am24
# 32  
Old 05-06-2016
Hello am24,

Could you please try following script, I am just enabling the trace utility here in script, hope set -x option works here. Because I tried this code in bash and it is working fine for me.
Code:
set -x
awk '{
        match($0,/^[[:space:]]+/);
        W=substr($0,RSTART,RLENGTH);
        sub(/^[[:space:]]+/,X,$0);
        num=split("Cable Yes && Pay TV && !ADS", array," ");
        for(i=1;i<=num;i++){
                                if(array[i]==$i){
                                                        count++;
                                                        Q=Q?Q OFS $i:$i
                                                }
                           };
        if(count==NF-1)    {
                                $0=Q " && !MDS" OFS $NF
                           };
        count="";
        Q="";
        num=split("Cable Yes && !Pay TV && !ADS", array," ");
        for(i=1;i<=num;i++){
                                if(array[i]==$i){
                                                        count++;
                                                        U=U?U OFS $i:$i
                                                }
                           };
        if(count==NF-1)    {
                                $0=U " && !MDS" OFS $NF
                           };
        count="";
        U="";
        $0=W $0
     }
        1
   '    Input_file

So this option should show us where problem lies actually, show us the output then and let us know how it goes.

Thanks,
R. Singh
This User Gave Thanks to RavinderSingh13 For This Post:
# 33  
Old 05-06-2016
Hi Ravinder,

Thank you. When i tried the code above you mentioned,the below lines were appeared on window.

Code:
awk: syntax error near line 2
awk: illegal statement near line 2
awk: syntax error near line 4
awk: illegal statement near line 4
awk: syntax error near line 9
awk: illegal statement near line 9
awk: syntax error near line 21
awk: illegal statement near line 21
awk: syntax error near line 32
awk: bailing out near line 32

Regards,
am24
# 34  
Old 05-06-2016
Hello am24,

Change awk to nawk and then run the same script with traces and without traces(just remove command set -x from it). Let me know how it goes then.

Thanks,
R. Singh

Last edited by RavinderSingh13; 05-06-2016 at 06:54 AM..
# 35  
Old 05-06-2016
Hi Ravinder,

I have run the code using nawk and got the below output.


Code:
          Cable Yes & Pay \exclusive
               Cable Yes && Pay TV && DBS                                                \noUE
               Cable Yes && Pay TV && (Satellite Dish || Master Antenna)        \noUE  \Label="Cable Yes && Pay TV && Other ADS"
Cable Yes && Pay TV && !ADS && !MDS \noUE
          Cable Yes & No Pay \exclusive
               Cable Yes && !Pay TV && DBS                                               \noUE
               Cable Yes && !Pay TV && (Satellite Dish || Master Antenna)       \noUE  \Label="Cable Yes && !Pay TV && Other ADS"
Cable Yes && !Pay TV && !ADS && !MDS \noUE
          No Cable & No Pay \exclusive
               !Cable Yes && !Pay TV && DBS                                              \noUE
               !Cable Yes && !Pay TV && (Satellite Dish || Master Antenna)      \noUE  \Label="!Cable Yes && !Pay TV && Other ADS"
               !Cable Yes && !ADS                                                        \noUE  \Label="!Cable Yes && !Pay TV && !ADS"
$

Regards,
am24

---------- Post updated at 05:35 AM ---------- Previous update was at 04:57 AM ----------

Hi Ravinder,

Please find the Input , Code , Output below:


Code:
$ cat input
+ cat input
          Cable Yes & Pay \exclusive
               Cable Yes && Pay TV && DBS                                                \noUE
               Cable Yes && Pay TV && (Satellite Dish || Master Antenna)        \noUE  \Label="Cable Yes && Pay TV && Other ADS"
               Cable Yes && Pay TV && !ADS                                               \noUE
          Cable Yes & No Pay \exclusive
               Cable Yes && !Pay TV && DBS                                               \noUE
               Cable Yes && !Pay TV && (Satellite Dish || Master Antenna)       \noUE  \Label="Cable Yes && !Pay TV && Other ADS"
               Cable Yes && !Pay TV && !ADS                                              \noUE
          No Cable & No Pay \exclusive
               !Cable Yes && !Pay TV && DBS                                              \noUE
               !Cable Yes && !Pay TV && (Satellite Dish || Master Antenna)      \noUE  \Label="!Cable Yes && !Pay TV && Other ADS"
               !Cable Yes && !ADS                                                        \noUE  \Label="!Cable Yes && !Pay TV && !ADS"

Code:
$ final.sh
+ final.sh
+ nawk {
        match($0,/^[[:space:]]+/);
        W=substr($0,RSTART,RLENGTH);
        sub(/^[[:space:]]+/,X,$0);
        num=split("Cable Yes && Pay TV && !ADS", array," ");
        for(i=1;i<=num;i++){
                                if(array[i]==$i){
                                                        count++;
                                                        Q=Q?Q OFS $i:$i
                                                }
                           };
        if(count==NF-1)    {
                                $0=Q " && !MDS" OFS $NF
                           };
        count="";
        Q="";
        num=split("Cable Yes && !Pay TV && !ADS", array," ");
        for(i=1;i<=num;i++){
                                if(array[i]==$i){
                                                        count++;
                                                        U=U?U OFS $i:$i
                                                }
                           };
        if(count==NF-1)    {
                                $0=U " && !MDS" OFS $NF
                           };
        count="";
        U="";
        $0=W $0
     }
        1
    input
+ 1> output

Code:
$ cat output
+ cat output
          Cable Yes & Pay \exclusive
               Cable Yes && Pay TV && DBS                                                \noUE
               Cable Yes && Pay TV && (Satellite Dish || Master Antenna)        \noUE  \Label="Cable Yes && Pay TV && Other ADS"
Cable Yes && Pay TV && !ADS && !MDS \noUE
          Cable Yes & No Pay \exclusive
               Cable Yes && !Pay TV && DBS                                               \noUE
               Cable Yes && !Pay TV && (Satellite Dish || Master Antenna)       \noUE  \Label="Cable Yes && !Pay TV && Other ADS"
Cable Yes && !Pay TV && !ADS && !MDS \noUE
          No Cable & No Pay \exclusive
               !Cable Yes && !Pay TV && DBS                                              \noUE
               !Cable Yes && !Pay TV && (Satellite Dish || Master Antenna)      \noUE  \Label="!Cable Yes && !Pay TV && Other ADS"
               !Cable Yes && !ADS                                                        \noUE  \Label="!Cable Yes && !Pay TV && !ADS"

The string is getting replaced but the space issue was there.

Thanks & Regards,
am24

---------- Post updated at 05:40 AM ---------- Previous update was at 05:35 AM ----------

Alos the set -x option is included in the script final.sh . when i am executing it its not diplaying set -x but if i do cat final.sh then set -x can be seen.

Regards,
am24

---------- Post updated at 06:03 AM ---------- Previous update was at 05:40 AM ----------

I would like to mention below points:

I have tried the code by keeping the code in final.sh (as ascript). When i did not used nawk (means when i am using awk ), i got the the awk error lines which i posted in earlier post. Also the file did not modified.

When i used nawk (with set -x and without set -x ) in the script, the file got modified.but again the space issue was there.

Regards,
am24

Last edited by am24; 05-06-2016 at 07:07 AM..
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