[英]How to append last field of a line in a file to a variable on powershell cli?
這是我的文件hello.txt
:
“ ReceiptHandle” =“ hellomyfriend == ++”
我只想將一行的最后一個字段附加到變量中:
$friend = hellomyfriend==++
假設這就是您的hello.txt文件中的全部內容。 以下將為您的$ friend變量分配“”。
$myhash = (gc 'hello.txt') -replace """","" | ConvertFrom-StringData
$friend = $myhash["ReceiptHandle"]
ConvertFrom-StringData使此操作變得容易,因為您的文本已經采用“某物=值”格式。
那么這是怎么回事? 第一,
gc'hello.txt'
正在獲取文件的內容。 我將其封裝在()中,以便可以使用。
-替換“”“”,“”
..刪除周圍的雙引號。 這被傳遞到ConvertFrom-StringData中,后者將字符串轉換為命名鍵/值對[hashtable]。 從那里,我可以通過查詢哈希表來訪問第二部分。
或者,您可以將所有內容放在一行上...
(gc 'hello.txt') -replace """","" | ConvertFrom-StringData | %{$friend = $_["ReceiptHandle"]}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.