查找对象值使用它来替换在另一个对象之前找到的字符串并保存到powershell中的txt文件

时间:2016-05-27 21:46:52

标签: powershell

我有一个这样的日志文件:

[2016/05/27 23:46:13.554-04:00][VERBOSE] 5.0.0 : AzCopy /Source:c:\test /Dest:https://mywebsite.blob.core.windows.net/monitoring /DestKey:****** /S /SetContentType /V:C:\azcopy1.log
[2016/05/27 23:46:14.179-04:00][VERBOSE] Start transfer: testfolder\file1.txt => testfolder/file1.txt
[2016/05/27 23:46:14.320-04:00][VERBOSE] Start transfer: testfolder\file2.png => testfolder/file2.png
[2016/05/27 23:46:14.320-04:00][VERBOSE] Start transfer: testfolder\file3.bmp => testfolder/file3.bmp
[2016/05/27 23:46:15.195-04:00][VERBOSE] Finished transfer: testfolder\file1.txt => testfolder/file1.txt
[2016/05/27 23:46:15.210-04:00][VERBOSE] Finished transfer: testfolder\file3.bmp => testfolder/file3.bmp
[2016/05/27 23:46:19.335-04:00][VERBOSE] Finished transfer: testfolder\file2.png => testfolder/file2.png
[2016/05/27 23:46:19.351-04:00] Transfer summary:
-----------------
Total files transferred: 3
Transfer successfully:   3
Transfer skipped:        0
Transfer failed:         0
Elapsed time:            00.00:00:05

我需要将这样的结果输出到一个新文件中:

https://mywebsite.blob.core.windows.net/monitoring/testfolder/file1.txt
https://mywebsite.blob.core.windows.net/monitoring/testfolder/file3.bmp
https://mywebsite.blob.core.windows.net/monitoring/testfolder/file2.png

我所能做的就是:

Get-Content C:\azcopy1.log | Where-Object { $_.Contains("Finished transfer:") } | Set-Content C:\listoflinks.txt

但这只会给我这样的结果:

[2016/05/27 23:46:15.195-04:00][VERBOSE] Finished transfer: testfolder\file1.txt => testfolder/file1.txt
[2016/05/27 23:46:15.210-04:00][VERBOSE] Finished transfer: testfolder\file3.txt => testfolder/file3.txt
[2016/05/27 23:46:19.335-04:00][VERBOSE] Finished transfer: testfolder\file2.txt => testfolder/file2.txt

2 个答案:

答案 0 :(得分:1)

尝试这样的事情

$a = '[2016/05/27 23:46:19.335-04:00][VERBOSE] Finished transfer: testfolder\file2.png => testfolder/file2.png'
$a | ? {$_.Contains("Finished transfer:")} | % {'https://mywebsite.blob.core.windows.net/monitoring/' + ($_ -replace '.*=> (.*)', '$1').trim()}

答案 1 :(得分:0)

我对poweshell命令(或一般的任何命令)几乎一无所知,所以我所能做的就是在互联网上查找各个操作的代码并逐个运行它们以获得所需的结果,所以我的代码很长,但它做我需要的,这是我如何设法得到所需的结果:

首先我用root url提取了这行:

Get-Content C:\azcopy1.log | Where-Object { $_.Contains("http") } | Set-Content C:\mainurl.txt

然后我删除了从该行到http字符串的所有内容(我知道此代码旨在使用多行,但我没有找到更好的内容):

$text = ( Get-Content C:\mainurl.txt | Out-String ).Trim() 
$text.Substring(0,$text.IndexOf(' /DestKey')) | Set-Content C:\mainurl.txt
$out = "C:\mainurl.txt"
$lines = get-content "C:\mainurl.txt"
foreach ($line in $lines)
{
    $len = $line.length
    $PipePos = $line.indexof("http")
    $retChars = ($len - $PipePos)
    $return = $line.Substring($len - $retchars, $retChars)
    out-file $out -inputobject $return
}

接下来,我提取了包含上传文件名的所有行:

Get-Content C:\azcopy1.log | Where-Object { $_.Contains("Finished transfer:") } | Set-Content C:\listoflinkstemp.txt

之后我删除了从每一行到string =>

的所有内容
$out = "c:\listoflinks.txt"
$lines = get-content "C:\listoflinkstemp.txt"
foreach ($line in $lines)
{
    $len = $line.length
    $PipePos = $line.indexof("=> ")
    $retChars = ($len - $PipePos)
    $return = $line.Substring($len - $retchars, $retChars)
    out-file $out -inputobject $return -append
}

最后我把主根网址替换为=>上传文件名列表中每一行的字符串:

$mainurl = get-content "C:\mainurl.txt"
$string = get-content "c:\listoflinks.txt"
$string.Replace("=> ","$mainurl/") | Set-Content c:\listoflinks.txt
Remove-Item c:\listoflinkstemp.txt

我确信所有这些都可以用几行代码完成,但正如我所说,我的知识非常有限,我不知道如何缩短代码,我做了什么,完成工作所以...