Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Subtracting part of one field based on another field. 1

Status
Not open for further replies.

mrr

Technical User
May 3, 2001
67
US
I have a data file as follows:
111 11111111 sdfsdfsdfsdfsdf
111 11122222 sdfsdfsdfsdfsdf
111 11133333 sdfsdfsdfsdfsdf
2222 222233333 sdfsdfsdfsdfsdf
2222 222244444 sdfsdfsdfsdfsdf
3 311111 sdfsdfsdfsdfsdf
3 322222 sdfsdfsdfsdfsdf

I want to use a simple match of index to remove from field
2 the prefix which is also the same in field 1 and
then print the line such as
111 11111 sdfsdfsdfsdfsdfsdf
111 22222 sdfsdfsdfsdfsdfsdf
222 33333 sdfsdfsdfsdfsdfsdf
222 44444 sdfsdfsdfsdfsdfsdf
3 11111 sdfsdfsdfsdfsdfsdf
3 22222 sdfsdfsdfsdfsdfsdf
I've tried using finding the length of characters of field 1
and substracting same amount from the leading characters of
field 2, but have not accomplished yet.

Thanks for the help.
 
To remove prefix $1 from $2 :

awk '{ sub("^"$1, "", $2) ; print }' inputfile Jean Pierre.
 
Aigles,
Thanks for the help. Simple script that works great.
 
Aigles,
Your script works as long as field 1 is limited to the same number of characters that you're removing from field 2.
How can I update your script to handle when field 1 and field 2 have leading or trailing characters that match and I want to remove from either the heading or trailing characters of field 2.
for example:

abcd-111 111xxxx ...............
would become
abcd-111 xxxx .....................
or

111-abcd xxxx111 ...................
would become
111-abcd xxxx .......................
or also

111-abcd 111xxxx ..................
would become
111-abcd xxxx ....................
Thanks
 
awk '{ sub($1"$", "", $2) ; print }' inputfile
 
That's another problem.
My solution is more complex X-) :

awk -f Remove.awk inputfile

where Remove.awk is :

function Min(n1, n2i, mn) {
if (n1 > n2)
mn = n1 ;
else
mn = n2 ;
return mn ;
}

function Common (str1, str2, type, l0,l1,l2,p1,p2,i1,i2,strc) {
l1 = length(str1) ;
l2 = length(str2) ;
l0 = Min(l1,l2) ;
p1 = 1 ; i1 = 1 ; # PP default
p2 = 1 ; i2 = 1 ;
if (type == "PS") {
p2 = l2 ; i2 = -1 ;
} else if (type == "SP") {
p1 = l1 ; i1 = -1 ;
} else if (type == "SS") {
p1 = l1 ; i1 = -1 ;
p2 = l2 ; i2 = -1 ;
}
strc = "" ;
while (l0 > 0) {
c1 = substr(str1, p1, 1) ;
c2 = substr(str2, p2, 1) ;
if (c1 != c2) break ;
strc = strc c1 ;
p1 += i1 ;
p2 += i2 ;
}
return strc ;
}

{
sub("^"Common($1,$2,"PP"), "", $2) ; 111xxx 111yyy => yyy
sub("^"Common($1,$2,"SP"), "", $2) ; xxx111 111yyy => yyy
sub(Common($1,$2,"PS")"$", "", $2) ; 111xxx yyy111 => yyy
sub(Common($1,$2,"SS")"$", "", $2) ; xxx111 yyy111 => yyy
print ;
}


I hope there is a simplest solution to this problem.
Jean Pierre.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top