LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 02-12-2016, 02:27 PM   #1
brh1
Member
 
Registered: Sep 2012
Location: Texas
Posts: 42

Rep: Reputation: Disabled
Need a one-liner to change one digit in a variable


Good day.

I need advice with assigning a partner hostname to a variable based on the current hostname, using standard tools like awk, sed, cut or tr?

Ie: if the current hostname is LM5SE-J00C01H0, I would like to assign a partner hostname to a new variable, ie: $PARTNER becomes LM5SE-J00C02H0, and visa versa, if $HOSTNAME=LM5SE-J00C02H0, $PARTNER becomes LM5SE-J00C01H0.

Thanks in advance,
Bjoern
 
Old 02-12-2016, 02:34 PM   #2
Habitual
LQ Veteran
 
Registered: Jan 2011
Location: Abingdon, VA
Distribution: Catalina
Posts: 9,374
Blog Entries: 37

Rep: Reputation: Disabled
What have you tried?
 
Old 02-12-2016, 03:01 PM   #3
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,836
Blog Entries: 1

Rep: Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251
Quote:
Originally Posted by brh1 View Post
Good day.

I need advice with assigning a partner hostname to a variable based on the current hostname, using standard tools like awk, sed, cut or tr?

Ie: if the current hostname is LM5SE-J00C01H0, I would like to assign a partner hostname to a new variable, ie: $PARTNER becomes LM5SE-J00C02H0, and visa versa, if $HOSTNAME=LM5SE-J00C02H0, $PARTNER becomes LM5SE-J00C01H0.

Thanks in advance,
Bjoern

As mentioned above, what have you tried? Apart from that, are these the only two possible hostnames? If so, you don't need any of the above mentioned tools, just a conditional statement.
If there are more possible hostnames, your question does not provide enough details.
 
Old 02-15-2016, 10:10 AM   #4
brh1
Member
 
Registered: Sep 2012
Location: Texas
Posts: 42

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by sycamorex View Post
As mentioned above, what have you tried? Apart from that, are these the only two possible hostnames? If so, you don't need any of the above mentioned tools, just a conditional statement.
If there are more possible hostnames, your question does not provide enough details.
Thanks for the replies. I will supply more information:

When I log into any of our systems, I use a virtual IP address to access either of 2 nodes depending which one is the master node at the time. There are times I would like to ssh to the partner node and therefore I have to determine which node I'm on before accessing the partner. However, the partner isn't clearly defined in the /etc/hosts file. Also, the hostnames could be any combination of letters and numbers around the '01' and '02' with the 'H0' on the end the only unchanged pair. For example LM5SE-J00C01H0 and LM5SE-J00C02H0 could be SH2VF-K00C01H0 and SH2VF-K00C02H0 on another system.

I think I did find a solution (seems a weekend R & R helps sorting the brain cells):
Code:
PREFIX=`hostname|cut -c1-11`;THISNUM=`hostname|cut -c 12`;case "$THISNUM" in 1 ) PARTNUM=2 ;; * ) PARTNUM=1 ;; esac ; PARTNER=$PREFIX$PARTNUM"H0" ; ssh $PARTNER
Bjoern
 
  


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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Cannot assign variable in shell one liner mojofender Linux - Newbie 4 12-12-2013 07:32 PM
[SOLVED] bash script problem -- pass complex *one-liner* to a variable RandyTech Linux - Newbie 12 01-27-2013 05:01 PM
[SOLVED] Awk - pattern for replace (change digit value) webhope Programming 7 04-26-2010 10:15 PM
Substitue single-digit, two-digit, and 3-digit numbers with text using sed dmason165 Programming 13 08-07-2009 10:38 AM
perl: how to insert numerical digit immediately after regexp backreference variable? chadwick Programming 8 05-19-2008 12:49 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 07:19 AM.

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