字符串整形

从网上拿到hosts记录,然后将它整形输出成address=/www.google.com/127.0.0.1这种格式。

第一个办法有点偷懒,只找出URL列,然后手动在最后加上IP地址。误打误撞学到了Trim的特殊之处。

第二个办法老老实实把URL列和IP列交换位置,然后输出。

<#方法一#>
$result = (Invoke-WebRequest -Uri 'https://raw.githubusercontent.com/vokins/yhosts/master/hosts').content -split "`n"
($result | Select-String -Pattern "^127.0.0.1").Line | foreach { 
  'address=/' + $_.TrimStart('.1270').TrimStart() + '/127.0.0.1'
}
<#方法二#>
$result = (Invoke-WebRequest -Uri 'https://raw.githubusercontent.com/vokins/yhosts/master/hosts').content -split "`n"
($result | Select-String -Pattern "^127.0.0.1").Line | foreach {
  'address=/' + ($_ -split " ")[1] + "/" + ($_ -split " ")[0]
}

 

将诸如"google.com": 1,这样格式的文本整形成server=/google.com/127.0.0.1#5353输出

$result = (Invoke-WebRequest -Uri 'https://raw.githubusercontent.com/1265578519/PAC/master/abc.pac').content -split "`n"
($result | Select-String -Pattern "`".+`":\s1").Line.Trim('",1: ') | foreach {
  'server=/' + $_ + "/127.0.0.1#5353"
}

 

将诸如apnic|CN|ipv4|203.34.39.0|256|20110414|allocated这样格式的文本整形输出成192.168.1.0/24

$result = (Invoke-WebRequest -Uri 'http://ftp.apnic.net/stats/apnic/delegated-apnic-latest').content -split "`n"
($result | Select-String -Pattern ".+CN[|]ipv4[|].+").Line | foreach {
  '-A SHADOWSOCKS -d ' + $_.split('|')[3] + '/' + (32 - [math]::log($_.split('|')[4]) / [math]::Log(2)) + ' -j RETURN'
}

 

posted @ 2017-03-22 10:30  dongdonggeorge  阅读(310)  评论(0编辑  收藏  举报