|
Prev: Extract a substring of n digits from a string
Next: A new Bie question on Types of shells on different flavours
From: awt13a on 28 Oct 2005 06:52 Hello, first of all I'm a novice in shell programming ! Can someone tell me how to modify a local string variable ? example: set MYNEWVAR=$HOSTNAME echo $MYNEWVAR deliveres e.g. unx123456.de.hh.lincroft.de How can I get only the first part of the string (unx123456) written into MYNEWVAR ? Mike
From: Ben on 28 Oct 2005 12:50 awt13a(a)yahoo.de wrote: > echo $MYNEWVAR deliveres e.g. unx123456.de.hh.lincroft.de > > How can I get only the first part of the string (unx123456) written > into MYNEWVAR untested set MYNEWVAR = `hostname | awk -F\. '{print $1}'`
From: Keith Thompson on 28 Oct 2005 15:07
Ben <none(a)none.com> writes: > awt13a(a)yahoo.de wrote: >> echo $MYNEWVAR deliveres e.g. unx123456.de.hh.lincroft.de >> How can I get only the first part of the string (unx123456) written >> into MYNEWVAR > > untested > > set MYNEWVAR = `hostname | awk -F\. '{print $1}'` It's not clear from the original article that $HOSTNAME came from the output of the "hostname" command. This should work: set MYNEWVAR = `echo $HOSTNAME | sed 's/\..*$//'` (I find the use of sed simpler, and therefore better, than the use of awk in this context.) -- Keith Thompson (The_Other_Keith) kst-u(a)mib.org <http://www.ghoti.net/~kst> San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst> We must do something. This is something. Therefore, we must do this. |