1

how to find the words which are between FS or WFS and "{" in a file and insert timestamp after the found word and before "{".

Example:

FS mmmmmmmmmm {

After running the script:

FS mmmmmmmmmm_timestamp {

chiwangc
  • 3,428
  • 16
  • 23
  • 31
B A
  • 69
  • 1
  • 8

1 Answers1

3

Very simple using REPL.BAT - a hybrid JScript/batch utility that performs a regular expression search/replace on stdin and writes the result to stdout. REPL.BAT is pure script that will run on any modern Windows machine from XP onward. This will also be much faster than any pure batch solution.

type "file.txt" | repl "(\bW?FS [^{]*[^{ ])(  *{)" "$1_timestamp$2" >"file.txt.new"
move /y "file.txt.new" "file.txt"

You don't specify, but perhaps you want a timestamp value instead of the string literal "timestamp". You can use any of the methods at How to get current datetime on Windows command line, in a suitable format for using in a filename? to get the current timestamp, or for the ultimate in ease of use and flexibility, you could use my getTimestamp.bat utility - another hybrid JScript/batch utility.

You don't say what format the timestamp should have, nor what degree of precision. I'll assume you want a format that is suitable for a file name - YYYY-MM-DD_hh-mm-ss.

@echo off
setlocal
call getTimetamp -f "{YYYY}-{MM}-{DD}_{HH}-{NN}-{SS}" -r ts
type "file.txt" | repl "(\bW?FS [^{]*[^{ ])(  *{)" "$1_%ts%$2" >"file.txt.new"
move /y "file.txt.new" "file.txt"
Community
  • 1
  • 1
dbenham
  • 119,153
  • 25
  • 226
  • 353
  • Thanks. I am going to adjust it to my real case, but unfortunetaly I can not underestand what $1 and $2 refer to? what is bW? where in the script it checkes FS or WFS? and thanks for your suggestion about Timestamp. – B A Aug 31 '14 at 15:49
  • @BA - Read the REPL.BAT built in help, and see http://msdn.microsoft.com/en-us/library/ae5bf541(v=vs.80).aspx for documentation on supported regex expressions. `$1` and `$2` reference the text that matched the 1st and 2nd parenthesized expressions in the search text. `\b` specifies a word boundry. `W?FS` specifies `FS` with possibly a `W` in front. – dbenham Aug 31 '14 at 17:11