LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   To get one part of a string (https://www.linuxquestions.org/questions/linux-newbie-8/to-get-one-part-of-a-string-4175436220/)

Vi_rod 11-08-2012 03:41 AM

To get one part of a string
 
Hi,
I am trying to compare 2 strings and get the numbers not present in string 2 but are PRESENT in string 1.

var2 = total no.
var3 = part of the same string
i want var 5 to be part of var2 which is not in var3.


echo "total no :"$var2

var3=`echo $var2 | awk '{ print substr( $0, length($0) - 9, length($0) ) }' `
echo "contact no :"$var3

var5=`echo $var2 | awk '{ print substr( $var2-$var3 ) }' `
echo "isd code :"$var5
-- IM NOT SURE ABOUT HOW TO GO ABOUT THIS...
Please help

evo2 11-08-2012 03:59 AM

Hi,

I'm not sure if I understood correctly, but does the following little python script do what you want?
Code:

#!/usr/bin/python
import sys
if len(sys.argv)<3:
    sys.exit(2)
c=''
for x in sys.argv[1]:
    both=False
    for y in sys.argv[2]:
        if x == y:
            both=True
    if not both:
        c+=x
print c

It takes the two strings as arguments on the command line. If this does what you want, I'm sure someone could get the same result in an awk or perl oneliner.

Evo2.

evo2 11-08-2012 04:13 AM

This version is a little more compact.
Code:

#!/usr/bin/python
import sys
if len(sys.argv)<3:
    sys.exit(2)
c=''
for x in sys.argv[1]:
    if sys.argv[2].find(x)<0:
        c+=x
print c


Vi_rod 11-08-2012 08:15 AM

err.. the idea was to get the isd code from the entire mobile number.

var2 has the entire number .

var3=`echo $var2 | awk '{ print substr( $0, length($0) - 9, length($0) ) }' `
echo "contact no :"$var3
and in Var3 - i get the 10 digits starting from the RHS. (backwards)

the rest will be the ISD code ( which i want in var5).. but i am not able to get output for var 5. var5 is where i need help to extract.

var5=`echo $var2 | awk '{ print substr( $var2-$var3 ) }' `
echo "isd code :"$var5


what should i change in var5?

David the H. 11-08-2012 01:41 PM

Could you please show us an actual example or two of the text stored in both variables, and what you want from them?

I have a feeling that you are making this much more complicated than it needs to be. I'm betting that you can probably do what you want with a simple parameter substitution or two, or some other form of built-in string manipulation.


But as a first bit of advice, $(..) is highly recommended over `..`. Backticks are generally deprecated.


And please use ***[code][/code]*** tags around your code and data, to preserve the original formatting and to improve readability. Do not use quote tags, bolding, colors, "start/end" lines, or other creative techniques.

Edit: As a concept example, assuming bash, and assuming that you want to simply remove the last ten digits from the number:
Code:

$ var=1234567890987654321

$ echo ${var:(-10)}
0987654321

$ echo "${var%${var:(-10)}}"
123456789

Or even simply:

Code:

$ echo "${var%??????????}"
123456789


evo2 11-08-2012 05:56 PM

Hi,

Quote:

Originally Posted by Vi_rod (Post 4824980)
err.. the idea was to get the isd code from the entire mobile number.

You'll need to be be more specific than that.
Quote:

var2 has the entire number .

var3=`echo $var2 | awk '{ print substr( $0, length($0) - 9, length($0) ) }' `
echo "contact no :"$var3
and in Var3 - i get the 10 digits starting from the RHS. (backwards)

the rest will be the ISD code ( which i want in var5).. but i am not able to get output for var 5. var5 is where i need help to extract.

var5=`echo $var2 | awk '{ print substr( $var2-$var3 ) }' `
echo "isd code :"$var5


what should i change in var5?
Are you saying that you want everything but the last 10 characters in a string? If so, bash can do it.
Code:

a="xxyyzz00112233445566778899"
echo ${a:0:$((${#a}-10))}

Evo2.

Vi_rod 11-09-2012 05:00 AM

Code:

$ echo "${var%${var:(-10)}}"
worked !

Thanks David,evo2 - yes i was wrong and making it more complicated than it actually is.

And ill make sure to follow those rules before posting.


All times are GMT -5. The time now is 01:20 AM.