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?

Bala

I have a requirement to read the lines of a txt file with a specific word "Release" to an array and get the first four characters of the last line in the array. Below is the code which I have used. My inputs will be textfilepath. Entries in the text file are as below.

1234 Debug  Build1Rel Build2Dbg
1234 Release  Build1Dbg Build2Dbg
1235 Release  Build1Rel Build2Dbg
1235 Debug Build1Dbg Build2Dbg
1236 Release  Build1Rel Build2Dbg
1236 Debug Build1Dbg Build2Dbg

Output I need is the first four characters (1236) in last line of Release. Thanks all for your prompt support. But now I need the same code in powershell.

Bala

Following code helped me to read the first four characters and the characters after third tab of last line and write the details to a XML file.

 #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 ==> Path to the txt file where the code in question exists\FileName

MatchPattern ==> Pattern to search for in the txt file

OutControlFile ==> Path to the XML file\Filename

So the output XML file looks like

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

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Get first and last 100 characters of a text file

分類Dev

How do I get code to read both first and last and values of a randomly generated array?

分類Dev

How to print first four characters of first and last filenames in a directory

分類Dev

How to read the first word in a text file and display it in a richTextBox

分類Dev

How to ignore all lines after a specific word on processing lines of a text file in a FOR loop?

分類Dev

Read text file in Perl word by word instead of line by line

分類Dev

Java How to read text file line by line and get that line's text separately (length text)

分類Dev

how to delete the last line in a text file with 100M lines without having to rewrite the whole file?

分類Dev

how to compare first word in a line with the last word using sed?

分類Dev

Reading text file with padas to get the specific lines

分類Dev

Get the first and last item in an Array - JS

分類Dev

How to [constantly] read the last line of a file?

分類Dev

How to read only the second last line of a file

分類Dev

read previous lines from specific line in a file using python

分類Dev

How to read a text file as lines are added

分類Dev

How to read and data from text file to array structure in c programming?

分類Dev

Searching for a Specific Word in a Text File and Displaying the line its on

分類Dev

awk - How to print the number of characters for the first n lines in a file?

分類Dev

How to read data corresponds to specific line numbers from a 60GB text file in python?

分類Dev

Ada - How Can I Get the 'First' and 'Last' Attributes of a Two-Dimensional Array?

分類Dev

How to get text values to an array?

分類Dev

Get # of contiguous hits and their first/last index in a NumPy array

分類Dev

How to read a text file from a url line by line

分類Dev

how to get local characters inside json array

分類Dev

How to get first word of every line and pipe it into dmenu script

分類Dev

Java how to read a txt file and make each line it's own string array

分類Dev

How to Replace text ONCE one a specific line in text file?

分類Dev

Get specific word from text

分類Dev

Python How to replace a particular word in a particular line in a text file?

Related 関連記事

  1. 1

    Get first and last 100 characters of a text file

  2. 2

    How do I get code to read both first and last and values of a randomly generated array?

  3. 3

    How to print first four characters of first and last filenames in a directory

  4. 4

    How to read the first word in a text file and display it in a richTextBox

  5. 5

    How to ignore all lines after a specific word on processing lines of a text file in a FOR loop?

  6. 6

    Read text file in Perl word by word instead of line by line

  7. 7

    Java How to read text file line by line and get that line's text separately (length text)

  8. 8

    how to delete the last line in a text file with 100M lines without having to rewrite the whole file?

  9. 9

    how to compare first word in a line with the last word using sed?

  10. 10

    Reading text file with padas to get the specific lines

  11. 11

    Get the first and last item in an Array - JS

  12. 12

    How to [constantly] read the last line of a file?

  13. 13

    How to read only the second last line of a file

  14. 14

    read previous lines from specific line in a file using python

  15. 15

    How to read a text file as lines are added

  16. 16

    How to read and data from text file to array structure in c programming?

  17. 17

    Searching for a Specific Word in a Text File and Displaying the line its on

  18. 18

    awk - How to print the number of characters for the first n lines in a file?

  19. 19

    How to read data corresponds to specific line numbers from a 60GB text file in python?

  20. 20

    Ada - How Can I Get the 'First' and 'Last' Attributes of a Two-Dimensional Array?

  21. 21

    How to get text values to an array?

  22. 22

    Get # of contiguous hits and their first/last index in a NumPy array

  23. 23

    How to read a text file from a url line by line

  24. 24

    how to get local characters inside json array

  25. 25

    How to get first word of every line and pipe it into dmenu script

  26. 26

    Java how to read a txt file and make each line it's own string array

  27. 27

    How to Replace text ONCE one a specific line in text file?

  28. 28

    Get specific word from text

  29. 29

    Python How to replace a particular word in a particular line in a text file?

ホットタグ

アーカイブ