locked
trying to remove last record in file RRS feed

  • General discussion

  •  I have a file were each row is ended with crlf, but there is an extra row created from a file copy process
    that has a 'sub'(when I turn on show all characters).  I can't seem to remove the row from the file.

     Thanks.

    Tuesday, September 24, 2019 6:56 PM

All replies

  • What have you tried so far? Please show your code. (formatted as code please)

    Live long and prosper!

    (79,108,97,102|%{[char]$_})-join''

    Tuesday, September 24, 2019 6:59 PM
  • $test = Get-Content 'c:\all_pastdue_order_datafile_orig.txt'
    $output = $test[0..($test.count - 2)] 
    
    $output | Out-File 'c:\new_past_dueorders_test.txt'

     It's gets rid of the 'sub' on the line but leaves the data row. I want to just delete last record

     Thanks.
    Tuesday, September 24, 2019 7:41 PM
  • Hmmm ... what do you mean with 'sub'? If you want to get rid of some lines from the end of the file you could use Select-Object -SkipLast #. Where # is and integer.

    Live long and prosper!

    (79,108,97,102|%{[char]$_})-join''

    Tuesday, September 24, 2019 8:12 PM
  •  When I look at the file in Notepad++ I can see all lines end with crlf. Then in the last
    line of the file 'sub' but looks like an empty record.

     Thanks.

    Tuesday, September 24, 2019 10:04 PM
  • Hmmm ... ok, and what's the problem with that?

    Live long and prosper!

    (79,108,97,102|%{[char]$_})-join''

    Tuesday, September 24, 2019 10:32 PM
  •  I try to use file to BCP to SQL and it fails due to that record. If I delete record I can load the file.

    Tuesday, September 24, 2019 10:44 PM
  • Sorry I cannot reproduce such a file. Could you upload it to gist ... just a few lines including the last one with the 'sub'?

    I think I got it ... try to do a replace like this:

    $_ -replace '\u001a'
    with that line with the 'sub'

    Live long and prosper!

    (79,108,97,102|%{[char]$_})-join''

    • Edited by BOfH-666 Wednesday, September 25, 2019 7:31 AM
    Wednesday, September 25, 2019 7:15 AM