The UNIX and Linux Forums  


Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
print 10 characters in series cdfd123 Shell Programming and Scripting 1 04-28-2008 01:18 AM
Print the characters in a word chella Shell Programming and Scripting 5 10-17-2007 10:06 AM
How to ignore characters and print only number using unix? cdfd123 UNIX for Dummies Questions & Answers 5 08-03-2007 04:47 AM
Solaris - unknown hostname - how can I change hostname? XNOR UNIX for Dummies Questions & Answers 1 03-29-2007 11:52 PM
AIX cannot print thai characters ivanwee AIX 0 10-18-2006 04:13 AM

Reply
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 10-19-2007
yxian yxian is offline
Registered User
  
 

Join Date: Oct 2007
Posts: 2
Print the first four characters of hostname

Hey,
I'm trying to print the first four characters of the hostname of a computer.

I can get it from using:
hostname -s | sed 's/...........$//'"

but this is when I know how many characters are in the computer name.

I dont understand why some like:
hostname -s | sed '/..../p'
wont work?

Any ideas?

Thanks.
  #2 (permalink)  
Old 10-19-2007
ghostdog74 ghostdog74 is offline Forum Advisor  
Registered User
  
 

Join Date: Sep 2006
Posts: 2,554

Code:
hostname -s|cut -c 1-4

  #3 (permalink)  
Old 10-22-2007
yxian yxian is offline
Registered User
  
 

Join Date: Oct 2007
Posts: 2
Thanks Mate! I have spent a number of hour trying to find this out?
  #4 (permalink)  
Old 10-22-2007
drl's Avatar
drl drl is offline Forum Advisor  
Registered User
  
 

Join Date: Apr 2007
Location: Saint Paul, MN USA / BSD, CentOS, Debian, OS X, Solaris
Posts: 712
Hi.
Quote:
Originally Posted by yxian View Post
... I dont understand why some like:
hostname -s | sed '/..../p'
wont work? ...
Because that sed line says: "If the incoming line has 4 characters then print it."

If you wanted to print just the first 4 with sed, you could use:

Code:
hostname -s | sed 's/^\(....\).*/\1/'

which seems cumbersome compared to the cut, but sed is capable of much more than cut ... cheers, drl
  #5 (permalink)  
Old 10-22-2007
shahnazurs shahnazurs is offline
Registered User
  
 

Join Date: Jul 2005
Posts: 59
can try this also

hostname|awk '{print substr($1,1,4)}'

Thanks.
  #6 (permalink)  
Old 06-15-2009
gorugand gorugand is offline
Registered User
  
 

Join Date: Mar 2009
Posts: 5
Last 4 characters how can i print
  #7 (permalink)  
Old 06-15-2009
vgersh99's Avatar
vgersh99 vgersh99 is online now Forum Staff  
Moderator
  
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,130
Quote:
Originally Posted by gorugand View Post
Last 4 characters how can i print
using a similar idea(s) as above......
Reply

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 08:23 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0