How to find and replace string data in text file

2

I'd like to be able to parse a text file which contains data such as:

2014-08-06 18:06:15 e:\shared\filename1.Shared orphan entry BM-100
2014-08-06 18:46:15 e:\shared\filename222.Shared orphan entry BM-214
2014-08-06 18:53:15 e:\shared\filename92.Shared orphan entry BM-674

This is on Windows and I'd like to be able to run a batch file script to remove the date string and text following the .shared file extension. Any ideas?

user354113

Posted 2014-08-06T18:29:06.250

Reputation: 21

Answers

0

You want the Unix utility sed, which handles regular expression edits and is available in numerous Windows versions (just look for sed.exe). If you pass the file name to your batch file, it should contain something like:

sed <"%1" 's/\.Shared .*$'//|sed 's/^.* .* //' >"%1.mod"

This deletes in each line from ".Shared " to the end of line, then from the start of line up to the second blank, and saves the result in the passed file name with .mod appended.

You can of course remove either or both redirections and instead redirect input and/or output when the batch file is invoked.

AFH

Posted 2014-08-06T18:29:06.250

Reputation: 15 470

There are of course more elegant solutions, with a single call to sed, but I thought that the two calls made the actions a little clearer - http://www.grymoire.com/unix/sed.html will tell you all you need to know about sed if you want a better command.

– AFH – 2014-08-06T21:23:31.007

0

If you're on Windows Vista or later, you could use PowerShell

(gc D:\input.txt) | % {$_ -replace "2014", "xxx"} | sc D:\input.txt
  • gc myPath Alias for Get-Content to read in a file
  • | pipe the text to next command
  • () must be used or else the input file is still open when you want to write it back
  • % { ... } Alias for ForEach-Object which iterates through every line
  • -replace LookFor, ReplaceWith replaces your string
  • sc myPath Alias for Set-Content to write the new content back to the same path

Further reading

nixda

Posted 2014-08-06T18:29:06.250

Reputation: 23 233

OutOfMemoryException will occur for large files. Tried with input file 1.75GB – MarkusEgle – 2019-12-02T15:45:59.550

0

Using this python script (which comes built-in with most linux distros and is an easy install for Windows):

import sys

if __name__ == "__main__":
    input_file = sys.argv[1]
    output_file = sys.argv[2]

    with open(input_file) as fp:
        with open(output_file, 'w') as fp_w:
            for line in fp:
                fp_w.write("{}\n".format(line.split(' ')[2]))

You'll get this output:

e:\shared\filename1.Shared
e:\shared\filename222.Shared
e:\shared\filename92.Shared

Steps to creating the python script:

  1. create a file named parser.py (or whatever you want to call it)
  2. copy the code above into the file
  3. run the following command:

    python parser.py test.txt test_output.txt

    This assumes that test.txt is the input file, and test_output.txt is the file path that you want to write the results to.

James Mertz

Posted 2014-08-06T18:29:06.250

Reputation: 24 787

0

Windows command prompt

for /f "tokens=2-3"   %i in (x.txt) do echo %i %j

results in

18:53:15 e:\shared\filename92.Shared

(or)

for /f "tokens=3"   %i in (x.txt) do echo %i

result in

e:\shared\filename92.Shared

cybernard

Posted 2014-08-06T18:29:06.250

Reputation: 11 200