|
|||||||
| Forums | Search Forums | Register | Forum Rules | Man Pages | Albums | FAQ | Members | Calendar | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here. |
|
|
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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
|
||||
|
||||
|
Here is how you can get the hostname in between "]" and "#"
awk -vRS="#" 'RT{gsub(/.*]/,"");print}' filename |
| Sponsored Links | ||
|
|
#3
|
|||
|
|||
|
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
|
||||
|
||||
|
If you are in Solaris, use nawk instead of awk
|
| Sponsored Links | |
|
|
#5
|
||||
|
||||
|
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
Last edited by Scrutinizer; 02-05-2013 at 06:09 AM.. Reason: code tags |
| Sponsored Links | |
|
|
#6
|
|||
|
|||
|
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-01Use nawk, of course... Last edited by RudiC; 02-05-2013 at 06:13 AM.. Reason: use nawk |
| Sponsored Links | |
|
|
#7
|
|||
|
|||
|
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}' xoutput 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 | ||
|
![]() |
| Thread Tools | Search this Thread |
| 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 |
|
|