2013-10-15 38 views
0

我正在使用pgScript,並且必須提取有關數據庫所在的服務器的信息。我知道我可以得到IP地址和端口,但我需要服務器的主機名。目前,我有:Postgresql解析pgScript變量以刪除行

declare @machineName; 
declare @I; 
set @I = 0; 
create temp table foo (t text); 
copy foo from '/etc/hosts'; 
set @machineName = 
    select * from foo; 
print @machineName; 
set @machineNameCount = select count(*) from foo; 

我回去:

("127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4") 
("::1   localhost localhost.localdomain localhost6 localhost6.localdomain6") 
("xx.xx.x.xx HOSTNAME") 

我知道我有三條線的工作,在這種情況下,所以我要@machineName使用在運行一個while循環@ machineNameCount,我只想刪除其中包含「127.0.0.1」或「:: 1」的任何行,但我無法弄清楚如何解析這些行。如果我可以看到一行是否包含我可以刪除的行。有任何想法嗎?

回答

0

所以我採取了不同的方法來此則在hosts文件中查找在/ etc;我拉出文件/ etc/sysconfig/network,專門尋找包含'HOSTNAME'的行。

create temp table machineNameTable (col1 text); 
copy machineNameTable from '/etc/sysconfig/network'; 
set @machineName = 
    select * from machineNameTable 
    where col1 ilike '%HOSTNAME%'; 
print @machineName; 
drop table machineNameTable; 

這使我回:

[PGSCRIPT ] ("HOSTNAME=SERVER_NAME") 

希望這可以幫助別人。

0

你可以使用這個bash命令爲包含127.0.0.1:: 1輸出文件的垂線

sed -i -e '/127\.0\.0\.1/d' -e '/::1/d' file 
+0

我不知道如何從pgscript運行bash命令。我必須做所有這些包含在一個pgscript中,我似乎在解析pgscript可以做的事情上相當有限。 –