TransWikia.com

How to find and replace string data in text file

Super User Asked by user354113 on November 27, 2021

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

2014-08-06 18:06:15 e:sharedfilename1.Shared orphan entry BM-100
2014-08-06 18:46:15 e:sharedfilename222.Shared orphan entry BM-214
2014-08-06 18:53:15 e:sharedfilename92.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?

4 Answers

Windows command prompt

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

results in

18:53:15 e:sharedfilename92.Shared

(or)

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

result in

e:sharedfilename92.Shared

Answered by cybernard on November 27, 2021

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:sharedfilename1.Shared
e:sharedfilename222.Shared
e:sharedfilename92.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.

Answered by James Mertz on November 27, 2021

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

Answered by nixda on November 27, 2021

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.

Answered by AFH on November 27, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP