LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 11-08-2012, 03:41 AM   #1
Vi_rod
Member
 
Registered: Dec 2011
Posts: 42

Rep: Reputation: Disabled
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
 
Old 11-08-2012, 03:59 AM   #2
evo2
LQ Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and CentOS
Posts: 6,724

Rep: Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705
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.
 
Old 11-08-2012, 04:13 AM   #3
evo2
LQ Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and CentOS
Posts: 6,724

Rep: Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705
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
 
Old 11-08-2012, 08:15 AM   #4
Vi_rod
Member
 
Registered: Dec 2011
Posts: 42

Original Poster
Rep: Reputation: Disabled
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?
 
Old 11-08-2012, 01:41 PM   #5
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
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

Last edited by David the H.; 11-08-2012 at 02:06 PM. Reason: fixt typo & as stated
 
Old 11-08-2012, 05:56 PM   #6
evo2
LQ Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and CentOS
Posts: 6,724

Rep: Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705
Hi,

Quote:
Originally Posted by Vi_rod View Post
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.
 
Old 11-09-2012, 05:00 AM   #7
Vi_rod
Member
 
Registered: Dec 2011
Posts: 42

Original Poster
Rep: Reputation: Disabled
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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] get part of a string in bash xeon123 Linux - Newbie 3 03-10-2011 10:04 AM
[SOLVED] extract part of string himu3118 Programming 4 05-07-2010 07:13 AM
[SOLVED] C - How to put a specific arbitrary part of a string into it's own string? golmschenk Programming 9 04-19-2010 08:27 PM
Extract part of a string steven.c.banks Linux - General 7 05-07-2008 07:18 AM
C: Extracting part of a string trevorv Programming 3 08-29-2007 04:36 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 06:40 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration