Help with text modification and displaying

I have a file storing some text
and another file storing some numbers
I want to display characters other than the specified place of strings

one.txt

xyz abc 233 skfo 4r443
sfs abc abcd sd fsdf  sdfd 
abc 11 abc 33 abc dsaf

two.txt

Nt_djd_k='5-6,7-9'
Nt_hh_l='3-6,7-8'
a=`grep 'Nt_djd_k' two.txt | cut -d "=" -f2 | sed "s/'//g"`

echo $a

cut -c$a one.txt

o/p:

abc 2
abc a
11 ab

o/p required:

xyz 33 skfo 4r443
sfs bcd sd fsdf  sdfd
abc bc 33 abc dsaf

Please use code tags as required by forum rules!

Does your cut provide the --complement option?

cut -c$a --complement one.txt
xyz 33 skfo 4r443
sfs bcd sd fsdf sdfd
abc c 33 abc dsaf
1 Like

I am getting this error

echo "ABCD" | cut -c2 --complement
cut: Not a recognized flag: -
Usage: cut -b List [-n] [File...]
   or: cut -c List [File...]
   or: cut -f List [-d Character] [-s] [File...]

In my system cut doesn't support --complement

Help me with any other option

Hello rahulsk,

Following may help you in same, here I am considering that your two.txt file may help more than 2 lines.

awk 'FNR==NR{match($0,/\=.*/);A[++i]=substr($0,RSTART+2,RLENGTH-3);sub(/\,/,"-",A);next} {if(FNR%2==0){q=FNR>i?i:FNR;{split(A[q], Y,"-");C=substr($0,1,Y[1]-1);if(Y[2]==Y[3]-1){print C OFS substr($0,Y[4]+1)} else {C=C OFS substr(B,Y[2]+1,Y[3]-Y[2]-1);;C=C OFS substr($0,Y[4]);print C;C="";}}}} {if(FNR%2!=0){q=FNR>i?i:FNR;{split(A[q], Y,"-");C=substr($0,1,Y[1]-1);if(Y[2]==Y[3]-1){print C OFS substr($0,Y[4]+1)} else {C=C OFS substr(B,Y[2]+1,Y[3]-Y[2]-1);;C=C OFS substr($0,Y[4]);print C;C="";}}}}' two.txt one.txt

Output will be as follows.

xyz  33 skfo 4r443
sf abcd sd fsdf  sdfd
ab bc 33 abc dsaf
 

Also here I want to mention that lets say there are 2 lines(as your shown Input_file) in two.txt file then 1st line rule(to cut the strings from 5-6,7-9) will be applied to all lines in one.txt which are not divided by 2 means all odd lines of one.txt and similarly 2nd line of two.txt rule (to cut the strings from 3-6,7-8) will be applied to all line of one.txt's all even lines, also considering that your two.txt file has only 2 conditions like (5-6,7-9)always, let me know if this helps you.

EDIT: Adding a non-one liner for of solution on same.

awk 'FNR==NR{
                match($0,/\=.*/);
                A[++i]=substr($0,RSTART+2,RLENGTH-3);
                sub(/\,/,"-",A);
                next
            }
            {
                if(FNR%2==0){
                                q=FNR>i?i:FNR;{ B=$0
                                                split(A[q], Y,"-");
                                                C=substr($0,1,Y[1]-1);
                                                if(Y[2]==Y[3]-1){
                                                                        print C OFS substr($0,Y[4]+1)
                                                                }
                                                else            {     
                                                                        C=C OFS substr(B,Y[2]+1,Y[3]-Y[2]-1);
                                                                        C=C OFS substr(B,Y[4]+1);
                                                                        print C;
                                                                        C="";
                                                                }
                                              }
                            }
             }
             {
                if(FNR%2!=0){
                                q=FNR>i?i:FNR;{
                                                split(A[q], Y,"-");
                                                C=substr($0,1,Y[1]-1);
                                                if(Y[2]==Y[3]-1){
                                                                        print C OFS substr($0,Y[4]+1)
                                                                }
                                                else            {
                                                                        C=C OFS substr(B,Y[2]+1,Y[3]-Y[2]-1);
                                                                        C=C OFS substr($0,Y[4]+1);
                                                                        print C;
                                                                        C="";
                                                                }
                                               }
                            }
             }
    ' two.txt one.txt
 

Thanks,
R. Singh

awk -F, '
$0 ~ SRCH       {gsub (/^.*=\047|\047$/, "")
                 for (i=1; i<=NF; i++)  {n = split ($i, T, "-")
                                         for (m=T[1]; m<=T[n]; m++) D[++j]=m
                                        }
                }
FNR == NR       {next
                }
                {for (i=1; i<=j; i++) $(D) = ""
                }
1
' SRCH="Nt_djd_k" file2 FS="" OFS="" file1
xyz 33 skfo 4r443
sfs bcd sd fsdf  sdfd 
abc c 33 abc dsaf