How to find and replace string data in text file

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?

Super User Asked by user354113 on November 27, 2021

4 Answers

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


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

result in


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:


Steps to creating the python script:

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

    python 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!

Related Questions

Multistep ssh – is it possible?

3  Asked on December 22, 2020 by vahagn-tumanyan


Microsoft Word: Table of Figures Ordering

2  Asked on December 21, 2020 by scoopsahaagendazs


not able to open my downloads through terminal in Mac OS

0  Asked on December 21, 2020 by daiwik-kashyap


Chrome high processor usage on Asus Tuf A15

0  Asked on December 20, 2020 by geeky-gurdeep


Lost files since renamed folder

1  Asked on December 20, 2020 by asklep


Renaming disks: from D: to F:

1  Asked on December 20, 2020 by user2925716


Windows 10 Taskbar Icons Blinking (Compressing)

1  Asked on December 19, 2020 by guy-thomas


Mysql Workbench import CSV 0 record imported

1  Asked on December 19, 2020 by zhenyu


Ask a Question

Get help from others!

© 2022 All rights reserved.