如何将具有特定单词的文本文件的行读取到数组并获取该数组最后一行的前四个字符?
How to read lines of a text file with specific word to an array and get the first four characters of the last line in that array?
我需要将包含特定单词 "Release" 的 txt 文件的行读取到数组中,并获取数组中最后一行的前四个字符。下面是我使用过的代码。我的输入将是文本文件路径。文本文件中的条目如下所示。
1234 Debug Build1Rel Build2Dbg
1234 Release Build1Dbg Build2Dbg
1235 Release Build1Rel Build2Dbg
1235 Debug Build1Dbg Build2Dbg
1236 Release Build1Rel Build2Dbg
1236 Debug Build1Dbg Build2Dbg
我需要的输出是Release最后一行的前四个字符(1236)。感谢大家的及时支持。但现在我需要在 powershell 中使用相同的代码。
Linq 的一些东西可能很有趣:
var output = File
.ReadLines("TextFilePath")
.Select(l => l.Split(new[]{' '}, StringSplitOptions.RemoveEmptyEntries))
.Where(a => a.Length > 2)
.Where(a => a[1] == "Release")
.Select(a => a.Last().Take(4));
假设您想要行
最后一段的前四个字符
我觉得很简单,像这样:
var result = File
.ReadLines(TextFilePath)
.Where(c => c.Contains("Release"))
.Last()
.Substring(0,4);
不需要读取整个文件如果我们只需要第一次出现:
const string fileName = "input.txt", searchWord = "Release";
const int charsAtEnd = 4;
var result = string.Empty; //Holds the result
using (var reader = new StreamReader(fileName)) {
//This is where we'll keep the lines we read
string line;
//Keep reading lines until ReadLine() returns null
while ((line = reader.ReadLine()) != null) {
//Check if the line contains the word we need
if (line.Contains(searchWord) && line.Length > charsAtEnd) {
//Set the result to the last chars of the line
result = line.Substring(line.Length - charsAtEnd);
//Exit the loop early if needed
break;
}
}
}
第 1 步: 使用 File.ReadAllLines()
方法将所有行读入字符串数组。
第 2 步: 根据需要从字符串末尾反转数组。
第 3 步: 使用 Contains()
方法搜索词 "Release"
第 4 步: 如果找到字符串 "Release",则使用 Substring()
方法从完整字符串中获取前 4 个字符
试试这个:
static string ReadData()
{
string [] lines = File.ReadAllLines(@"F:\InputFile.txt");
Array.Reverse(lines);
foreach(string line in lines)
{
if(line.Contains("Release"))
{
return line.Substring(0, 4);
}
}
return "";
}
以下代码帮助我读取了最后一行的前四个字符和第三个制表符之后的字符,并将详细信息写入 XML 文件。
#Required parameters to set
param(
[Parameter(Position=0,Mandatory=$true)] [string]$ControlFilePath,
[Parameter(Position=1,Mandatory=$true)] [string]$MatchPattern,
[Parameter(Position=2,Mandatory=$true)] [string]$OutControlFile
)
If (Test-path $OutControlFile) {
Remove-Item $OutControlFile
}
$LastLine= get-content $ControlFilePath -ReadCount 1000 | foreach { $_ -match "$MatchPattern" } | Select-Object -Last 1
$BuildNumberDigit=$LastLine.Substring(0,4)
$BuildNumberFormat=($LastLine -split "`t")[2].substring(0)
# Create The Document
$XmlWriter = New-Object System.XMl.XmlTextWriter($OutControlFile,$Null)
# Set The Formatting
$xmlWriter.Formatting = 'Indented'
$xmlWriter.Indentation = 1
$XmlWriter.IndentChar = "`t"
# Write the XML Decleration
$xmlWriter.WriteStartDocument()
# Write the Document
$xmlWriter.WriteStartElement("BuildVersions")
$xmlWriter.WriteStartElement("property")
$xmlWriter.WriteAttributeString("name","BuildNumber")
$xmlWriter.WriteAttributeString("value","$BuildNumberDigit ")
$xmlWriter.WriteEndElement() # Closing Property
$xmlWriter.WriteStartElement("property")
$xmlWriter.WriteAttributeString("name","BuildNumberFormat")
$xmlWriter.WriteAttributeString("value","$BuildNumberFormat")
$xmlWriter.WriteEndElement() # Closing Property
$xmlWriter.WriteEndElement() # Closing BuildVersions
# End the XML Document
$xmlWriter.WriteEndDocument() #Closing Documents
# Finish The Document
$xmlWriter.Finalize
$xmlWriter.Close()
ControlFilepath ==> 相关代码所在的 txt 文件的路径 exists\FileName
MatchPattern ==> 要在 txt 文件中搜索的模式
OutControlFile ==> XML file\Filename
的路径
所以输出 XML 文件看起来像
<BuildVersions>
<property name="BuildNumberDigit" value="1236 " />
<property name="BuildNumberFormat" value="Build1Rel" />
</BuildVersions>
我需要将包含特定单词 "Release" 的 txt 文件的行读取到数组中,并获取数组中最后一行的前四个字符。下面是我使用过的代码。我的输入将是文本文件路径。文本文件中的条目如下所示。
1234 Debug Build1Rel Build2Dbg
1234 Release Build1Dbg Build2Dbg
1235 Release Build1Rel Build2Dbg
1235 Debug Build1Dbg Build2Dbg
1236 Release Build1Rel Build2Dbg
1236 Debug Build1Dbg Build2Dbg
我需要的输出是Release最后一行的前四个字符(1236)。感谢大家的及时支持。但现在我需要在 powershell 中使用相同的代码。
Linq 的一些东西可能很有趣:
var output = File
.ReadLines("TextFilePath")
.Select(l => l.Split(new[]{' '}, StringSplitOptions.RemoveEmptyEntries))
.Where(a => a.Length > 2)
.Where(a => a[1] == "Release")
.Select(a => a.Last().Take(4));
假设您想要行
最后一段的前四个字符我觉得很简单,像这样:
var result = File
.ReadLines(TextFilePath)
.Where(c => c.Contains("Release"))
.Last()
.Substring(0,4);
不需要读取整个文件如果我们只需要第一次出现:
const string fileName = "input.txt", searchWord = "Release";
const int charsAtEnd = 4;
var result = string.Empty; //Holds the result
using (var reader = new StreamReader(fileName)) {
//This is where we'll keep the lines we read
string line;
//Keep reading lines until ReadLine() returns null
while ((line = reader.ReadLine()) != null) {
//Check if the line contains the word we need
if (line.Contains(searchWord) && line.Length > charsAtEnd) {
//Set the result to the last chars of the line
result = line.Substring(line.Length - charsAtEnd);
//Exit the loop early if needed
break;
}
}
}
第 1 步: 使用 File.ReadAllLines()
方法将所有行读入字符串数组。
第 2 步: 根据需要从字符串末尾反转数组。
第 3 步: 使用 Contains()
方法搜索词 "Release"
第 4 步: 如果找到字符串 "Release",则使用 Substring()
方法从完整字符串中获取前 4 个字符
试试这个:
static string ReadData()
{
string [] lines = File.ReadAllLines(@"F:\InputFile.txt");
Array.Reverse(lines);
foreach(string line in lines)
{
if(line.Contains("Release"))
{
return line.Substring(0, 4);
}
}
return "";
}
以下代码帮助我读取了最后一行的前四个字符和第三个制表符之后的字符,并将详细信息写入 XML 文件。
#Required parameters to set
param(
[Parameter(Position=0,Mandatory=$true)] [string]$ControlFilePath,
[Parameter(Position=1,Mandatory=$true)] [string]$MatchPattern,
[Parameter(Position=2,Mandatory=$true)] [string]$OutControlFile
)
If (Test-path $OutControlFile) {
Remove-Item $OutControlFile
}
$LastLine= get-content $ControlFilePath -ReadCount 1000 | foreach { $_ -match "$MatchPattern" } | Select-Object -Last 1
$BuildNumberDigit=$LastLine.Substring(0,4)
$BuildNumberFormat=($LastLine -split "`t")[2].substring(0)
# Create The Document
$XmlWriter = New-Object System.XMl.XmlTextWriter($OutControlFile,$Null)
# Set The Formatting
$xmlWriter.Formatting = 'Indented'
$xmlWriter.Indentation = 1
$XmlWriter.IndentChar = "`t"
# Write the XML Decleration
$xmlWriter.WriteStartDocument()
# Write the Document
$xmlWriter.WriteStartElement("BuildVersions")
$xmlWriter.WriteStartElement("property")
$xmlWriter.WriteAttributeString("name","BuildNumber")
$xmlWriter.WriteAttributeString("value","$BuildNumberDigit ")
$xmlWriter.WriteEndElement() # Closing Property
$xmlWriter.WriteStartElement("property")
$xmlWriter.WriteAttributeString("name","BuildNumberFormat")
$xmlWriter.WriteAttributeString("value","$BuildNumberFormat")
$xmlWriter.WriteEndElement() # Closing Property
$xmlWriter.WriteEndElement() # Closing BuildVersions
# End the XML Document
$xmlWriter.WriteEndDocument() #Closing Documents
# Finish The Document
$xmlWriter.Finalize
$xmlWriter.Close()
ControlFilepath ==> 相关代码所在的 txt 文件的路径 exists\FileName
MatchPattern ==> 要在 txt 文件中搜索的模式
OutControlFile ==> XML file\Filename
的路径所以输出 XML 文件看起来像
<BuildVersions>
<property name="BuildNumberDigit" value="1236 " />
<property name="BuildNumberFormat" value="Build1Rel" />
</BuildVersions>