How to extract hostname from file using awk? | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


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

How to extract hostname from file using awk?

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 02-05-2013
surender reddy surender reddy is offline
Registered User
 
Join Date: Jan 2013
Last Activity: 4 March 2013, 3:04 AM EST
Posts: 36
Thanks: 10
Thanked 0 Times in 0 Posts
How to extract hostname from file using awk?

Hi

iam having file with below lines of text


Code:
[local]pun-ras-bng-mhs-01#cont bsnl.in
[local]enk-ras-bng-cse-01#cont bsnl.in

how to extract the host name and store in a variable and to print output using awk command

output will be


Code:
HOSTNAME
pun-ras-bng-mhs-01
enk-ras-bng-cse-01


Tnx in advance.

Last edited by Scrutinizer; 02-05-2013 at 06:07 AM.. Reason: code tags
Sponsored Links
    #2  
Old 02-05-2013
./hari.sh's Avatar
./hari.sh ./hari.sh is offline
Registered User
 
Join Date: Jul 2007
Last Activity: 17 January 2014, 5:27 AM EST
Posts: 97
Thanks: 6
Thanked 4 Times in 4 Posts
Here is how you can get the hostname in between "]" and "#"

awk -vRS="#" 'RT{gsub(/.*]/,"");print}' filename
Sponsored Links
    #3  
Old 02-05-2013
surender reddy surender reddy is offline
Registered User
 
Join Date: Jan 2013
Last Activity: 4 March 2013, 3:04 AM EST
Posts: 36
Thanks: 10
Thanked 0 Times in 0 Posts
Hi below error is coming after executing the script



Code:
root@blr-svr-oclan-01 # awk -vRS="#" 'RT{gsub(/.*]/,"");print}' x 
awk: syntax error near line 1
awk: bailing out near line 1
root@blr-svr-oclan-01 #


Last edited by Scrutinizer; 02-05-2013 at 06:08 AM.. Reason: code tags
    #4  
Old 02-05-2013
./hari.sh's Avatar
./hari.sh ./hari.sh is offline
Registered User
 
Join Date: Jul 2007
Last Activity: 17 January 2014, 5:27 AM EST
Posts: 97
Thanks: 6
Thanked 4 Times in 4 Posts
If you are in Solaris, use nawk instead of awk
Sponsored Links
    #5  
Old 02-05-2013
surender reddy surender reddy is offline
Registered User
 
Join Date: Jan 2013
Last Activity: 4 March 2013, 3:04 AM EST
Posts: 36
Thanks: 10
Thanked 0 Times in 0 Posts
iam using solaris 10.the code given is running with nawk with out any error
but no output displayed


Code:
root@blr-svr-oclan-01 # more x
[local]pun-ras-bng-mhs-01#cont bsnl.in
[local]enk-ras-bng-cse-01#cont bsnl.in
root@blr-svr-oclan-01 # nawk -vRS="#" 'RT{gsub(/.*]/,"");print}' x 
root@blr-svr-oclan-01




Moderator's Comments:
Please use code tags for code and data

Last edited by Scrutinizer; 02-05-2013 at 06:09 AM.. Reason: code tags
Sponsored Links
    #6  
Old 02-05-2013
RudiC RudiC is offline Forum Advisor  
Registered User
 
Join Date: Jul 2012
Last Activity: 29 July 2014, 9:50 AM EDT
Location: Aachen, Germany
Posts: 3,914
Thanks: 62
Thanked 928 Times in 880 Posts
As I'm not sure what "RT" in the awk program stands for (being an uninitialized and thus empty variable prohibiting the print), try this small modification of ./hari.sh's script:
Code:
$ awk -vFS="#" 'BEGIN{print "HOSTNAME"} {gsub(/.*]/,"");print $1}' file
HOSTNAME
pun-ras-bng-mhs-01
enk-ras-bng-cse-01

Use nawk, of course...

Last edited by RudiC; 02-05-2013 at 06:13 AM.. Reason: use nawk
Sponsored Links
    #7  
Old 02-05-2013
surender reddy surender reddy is offline
Registered User
 
Join Date: Jan 2013
Last Activity: 4 March 2013, 3:04 AM EST
Posts: 36
Thanks: 10
Thanked 0 Times in 0 Posts
Hi

its working with nawk but output not as desired. extra words are coming in output



Code:
root@blr-svr-oclan-01 # nawk -vFS="#" 'BEGIN{print "HOSTNAME"} {gsub(/.*]/,"");print $1}' x

output


Code:
HOSTNAME
pun-ras-bng-mhs-01#cont
enk-ras-bng-cse-01#cont


Last edited by Scrutinizer; 02-05-2013 at 06:25 AM.. Reason: code tags
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
extract part of hostname in a script gubbu Shell Programming and Scripting 1 11-23-2011 01:51 PM
[ask]awk in csh to extract content from file animesharma Shell Programming and Scripting 12 07-28-2011 04:25 AM
awk extract a string from a file jredx Shell Programming and Scripting 8 03-19-2010 03:19 AM
how to extract info from a file using awk on9west Shell Programming and Scripting 3 01-13-2009 07:29 AM
Content extract of a file using awk nr_shan Shell Programming and Scripting 5 12-19-2007 04:22 AM



All times are GMT -4. The time now is 02:50 PM.