如何读取具有特定单词的文本文件行到数组并获取该数组中最后一行的前四个字符?

时间:2015-01-14 13:22:09

标签: powershell

我需要读取带有特定单词“Release”的txt文件的行到数组,并获取数组中最后一行的前四个字符。以下是我使用过的代码。我的输入将是textfilepath。文本文件中的条目如下。

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中使用相同的代码。

5 个答案:

答案 0 :(得分:3)

对我来说这很容易,就像这样:

var result = File
  .ReadLines(TextFilePath)
  .Where(c => c.Contains("Release"))
  .Last()
  .Substring(0,4);

答案 1 :(得分:1)

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));

假设你想要

的最后一段的前四个字符

答案 2 :(得分:0)

以下代码帮助我读取了最后一行第三个标签后的前四个字符和字符,并将详细信息写入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文件的路径,其中存在的代码是\ FileName

MatchPattern ==>要在txt文件中搜索的模式

OutControlFile ==> XML文件的路径\文件名

因此输出XML文件看起来像

<BuildVersions>
    <property name="BuildNumberDigit" value="1236 " />
    <property name="BuildNumberFormat" value="Build1Rel" />
</BuildVersions>

答案 3 :(得分:-1)

无需读取整个文件如果我们只需要第一次出现:

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;
        }
    }
}

答案 4 :(得分:-1)

第1步:使用File.ReadAllLines()方法将所有行读入字符串数组 步骤2:如果您想要字符串末尾的字符串,请反转数组 第3步:搜索字词&#34;发布&#34;使用Contains()方法
第4步:如果字符串&#34;发布&#34;找到然后使用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 "";
    }