Hi
iam having file with below lines of text
[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
HOSTNAME
pun-ras-bng-mhs-01
enk-ras-bng-cse-01
Tnx in advance.
Here is how you can get the hostname in between "]" and "#"
awk -vRS="#" 'RT{gsub(/.*]/,"");print}' filename
Hi below error is coming after executing the script
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 #
If you are in Solaris, use nawk instead of awk
iam using solaris 10.the code given is running with nawk with out any error
but no output displayed
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
RudiC
February 5, 2013, 6:11am
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:
$ awk -vFS="#" 'BEGIN{print "HOSTNAME"} {gsub(/.*]/,"");print $1}' file
HOSTNAME
pun-ras-bng-mhs-01
enk-ras-bng-cse-01
Use nawk, of course...
Hi
its working with nawk but output not as desired. extra words are coming in output
root@blr-svr-oclan-01 # nawk -vFS="#" 'BEGIN{print "HOSTNAME"} {gsub(/.*]/,"");print $1}' x
output
HOSTNAME
pun-ras-bng-mhs-01#cont
enk-ras-bng-cse-01#cont
awk -F'[]#]' '{print $2}' file
Hi
not getting output as desired.
root@blr-svr-oclan-01 # awk -F'[]#]' '{print $2}' x
output
local]pun-ras-bng-mhs-01#cont bsnl.in
local]enk-ras-bng-cse-01#cont bsnl.in
root@blr-svr-oclan-01 #
Required output will be
HOSTNAME
pun-ras-bng-mhs-01
enk-ras-bng-cse-01
Hi
if iam using nawk. no output is coming
root@blr-svr-oclan-01 # nawk -F'[]#]' '{print $2}' x
On Solaris use /usr/xpg4/bin/awk rather than awk or nawk
1 Like
Hi,
now code is working fine.
root@blr-svr-oclan-01 # /usr/xpg4/bin/awk -F'[]#]' '{print $2}' x
pun-ras-bng-mhs-01
enk-ras-bng-cse-01
root@blr-svr-oclan-01 #
tnx
there are many ways to achieve desired target
<filename cut -d] -f2 | cut -d'#' -f1
awk -F'[]#]' '{ print $2 }' filename
sed 's/.*]\|#.*//g' filename
perl -pe 's/.*\]|#.*//g' filename