locked
Is it possible to find the line number of a file a piece of data is held in and then read the whole line? RRS feed

  • Question

  • I have a program which writes first name, surname and a corresponding number to a file (each person on their own line). Is it possible to give the program the name and number of a person, for it to then find the line number which that data is held in and read/output it? 

    Thanks


    • Edited by Lowen44 Wednesday, December 12, 2018 8:29 PM
    Wednesday, December 12, 2018 8:26 PM

Answers

  • You can read the file witrh one of the File. ...  commands, and then search for the right line.

    Jan [ WhTurner ] The Netherlands

    Thursday, December 13, 2018 6:34 PM
    Answerer
  • With PowerShell, you can do it with select-string (sls) cmdlet.  For example:

    PS> sls Nonki people.txt

    people.txt:2:Nonki Takahashi 39483


    Nonki Takahashi

    Saturday, December 29, 2018 2:01 PM

All replies

  • You can read the file witrh one of the File. ...  commands, and then search for the right line.

    Jan [ WhTurner ] The Netherlands

    Thursday, December 13, 2018 6:34 PM
    Answerer
  • With PowerShell, you can do it with select-string (sls) cmdlet.  For example:

    PS> sls Nonki people.txt

    people.txt:2:Nonki Takahashi 39483


    Nonki Takahashi

    Saturday, December 29, 2018 2:01 PM