Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

probably a very basic 'sed' question

Status
Not open for further replies.

MHThomas

Technical User
Nov 25, 2002
26
GB
Hi,
I wonder if any body can help me with a simple data manipulation problem. I am using Korn shell on AIX and need to select the first section of a variable name, keeping everything before the first '.' and dumping everything afterwards. As an example I would want the following script (with the blank filled in):

#!/bin/ksh
x="aaa.bbb.ccc.com"
y=#######unknown (sed?) statement#######
echo output: $y


To produce the following results:

output: aaa

I'm sure this is simple, but I just can't get any joy.

Cheers,

Mark T.
 
One way is
Code:
y=`echo $x | awk -F. '{print $1}'`
 
Thank you both for your amazingly quick responses. I'll buy you both a pint the next time I see you :)

Cheers,

Mark T.
 
#!/bin/ksh

x="aaa.bbb.ccc.com"
y="${x%%.*}"

echo "[$x] [$y]"


vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
If you wanted to use sed...

y=`echo $x | sed 's/^\(.*\)\.*$/\1/'`
 
sed's regex are greedy.

y=`echo $x | sed 's/^\([^.][^.]*\).*$/\1/'

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top