Solved

Shell script with awk to extract substring

Posted on 2011-03-15
13
3,766 Views
Last Modified: 2013-12-26
I am modifying my .profile on an AIX system so it will capture my IP Address into a variable and then export it as my env variable DISPLAY.

Below is code that works but the last octet of the IP address can be one, two or three digits long.  If it is less than three digits, I want to suppress the trailing space before I concatenate additional stuff.

The value of the variables are as follows:
SSH_CLIENT is an ip address and  additional information as follows:
SSH_CLIENT=192.168.110.31 1234 56

with the code below, I am able to extract into my_ipadr as follows:
my_ipadr=192.168.110.31 :0.0 (with a trailing space after the 31 and before the:0.0)

When I concatenate the last part, the space becomes a problem. WHen it is assigned to the DISPLAY variable it looks like this:
DISPLAY=192.168.110.31 :0.0
complete with the space between 31 and :0.0

How can I "trim" the space off prior to concatenation?  I can't change the substr command so it only picks up two numbers because it is possible for it to be three numbers.  and if it is only one number then I will have two spaces.

I have toyed with the # (sharp) character, but have only gotten errors.

Thanks in advance.

my_ipadr=$(echo $SSH_CLIENT | awk -F. '{print $1"."$2"."$3"."substr($4,1,3)":0.0"}')
echo $my_ipadr
export DISPLAY=$my_ipadr

Open in new window

0
Comment
Question by:Fhnorth
  • 5
  • 5
  • 3
13 Comments
 
LVL 31

Accepted Solution

by:
farzanj earned 125 total points
ID: 35143012
A quick fix

my_ipadr=$(echo $SSH_CLIENT | awk -F. '{print $1"."$2"."$3"."substr($4,1,3)":0.0"}' | sed 's/ //g')
0
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 35143019
my_ipadr=$(echo $SSH_CLIENT | awk '{print $1 "0:0"}')
0
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 35143046
Sorry, typo:

my_ipadr=$(echo $SSH_CLIENT | awk '{print $1 ":0.0"}')
0
 
LVL 68

Assisted Solution

by:woolmilkporc
woolmilkporc earned 125 total points
ID: 35143181
And the simplest method:

export DISPLAY=${SSH_CLIENT%%" "*}:0.0

or, if you prefer:

my_ipadr=${SSH_CLIENT%%" "*}:0.0
echo $my_ipadr
export DISPLAY=$my_ipadr

wmp
0
 

Author Comment

by:Fhnorth
ID: 35143183
farzanj,

Your script works perfectly.
I am not educated in sed, so I need to ask will this work even if the last octet of the IP address is three numbers?  For example:
192.168.110.1101234 56

I believe your solution is exactly what I need... but first I need to ask a question of woolmilkporc...


woolmilkporc,

Your script works perfectly also.

So I may have a problem with using the -F switch?  I am new to awk.  It would appear that if I omit the -F switch, it parses the string differently.  Does it depend upon finding a space, using the space as a delimiter?  I notice that with my original script with the -F, it uses the "." (period) as a delimiter.

If that is the case, when the last octet is three numbers, it will look like this:
192.168.110.1101234 56 (with no space between the fourth octet and the additional number at the end of the string (1234 56).  Is this going to present a problem?
0
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 35143203
Without -F the space is the delimiter.

$1 is the string up to, but not including, the first space, and $1 will of course contain nothing that follows this space (that would be $2 etc.)
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:Fhnorth
ID: 35143205
woolmilkporc,

I posted my last comment at the same time you posted yours.  So, with the easier "one liner", I have to ask the same question...
0
 

Author Comment

by:Fhnorth
ID: 35143217
Well then it won't work with no space after the fourth octet, as in my example in my second post...?
0
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 35143244
The string in your second post will never appear in $SSH_CLIENT (there will always be a space following the IP address, regardless of the number of digits in the fourth octet).

So why would you construct a problem where there is actually none?

My second solution uses "inline variable editing"

%%" "* means delete (starting from the right) the first space and all that follows,
0
 

Author Comment

by:Fhnorth
ID: 35143281
I did not know that there would always be a space between the fourth octet and the other numbers.  I am obviously new to the SSH_CLIENT environment variable as well.

Thanks to both of you.  You both have excellent solutions and I am awarding each of you half of the points.

Thanks again!  I REALLY appreciate the help.
0
 
LVL 31

Expert Comment

by:farzanj
ID: 35143347
My sed command would work no matter what.  It is only erasing any spaces, anywhere, nothing else.
0
 

Author Comment

by:Fhnorth
ID: 35143441
farzanj,

Yes, I know!  Your script is the one that I favor and am using.  I favor it for that VERY reason.  I have tested both of them and found that with woolmilkporc's script, it has a problem if there is no space for a delimiter.  He assures me that the SSH_CLIENT variable will never have a space, but I'd rather be safe than sorry.

Thanks much!
0
 
LVL 31

Expert Comment

by:farzanj
ID: 35143611
You are most welcome.  Glad you liked it.

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Utilizing an array to gracefully append to a list of EmailAddresses
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now