1

I have a .xal document which can be opened with notepad just fine. Below is the part of it that is important to me:

...
magV=1
invert=0
text=01000000237843141114052693183714

[Pri1]
enabled=0
...

I would like to create a script, which would auto replace part of text 140526 in line 128 (it is some old date - 26.05.2014) and replace it with the current date.
For example: today it should be 160816, tommorrow there should be 160817, etc.

As said before text is in line 128, characters are from 24 to 30.

If searched around (SED for WIN, VBA, Powershell,...) but i didn't find anything that could work.

Thank you all in advance!

Ulf Gjerdingen
  • 1,414
  • 3
  • 16
  • 20
kb2487
  • 27
  • 6
  • Still exprementing but $1 = get-content .\examplefile $1[128] will select row 128, then it shouldn't be to hard to use regex or something like that to replace the charatchers – Jakodns Aug 16 '16 at 10:18

2 Answers2

1

works fine if you do a windows batch file and using sed

Basically, use set and built-in DATE environment variable to extract year, month, day as a 6-digit string (DATE contains yy/mm/dd, slashes need to be removed) Then apply sed on line 128 only, creating one group, replacing the group + the 6 next chars by the same group + the new date.

The -i flag allows to perform in-place replacement so input file is updated instead of outputing the result in standard output.

set D=%DATE%
set D=%D:~0,2%%D:~3,2%%D:~6,2%
echo %D%
sed -i "128s/\(text=..................\)....../\1%D%/" input.txt
Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219
0

Here a solution using PowerShell:

$content = Get-Content 'your_file_path'
$content[127] = $content[127] -replace '(.{24}).{6}', ('${{1}}{0}' -f (Get-Date).ToString('yyMMdd'))
$content | Set-Content 'your_file_path'
Martin Brandl
  • 56,134
  • 13
  • 133
  • 172
  • Thank you, i tried your solution but it's not (yet) ok. I debug the code in Powershell ISE and in the midle of debuging i get this -> 01000000237843141111608164052693183714. As you can see there is still previous text (140526) in it and after that today's date is added. The output should be 01000000237843141116081693183714. Ok but in the end when i check output file, there is still the same text on line 128 as it is in input file -> 01000000237843141114052693183714, looks like nothing has been changed at all?? – kb2487 Aug 16 '16 at 11:45
  • I edited my answer. Try it again. – Martin Brandl Aug 16 '16 at 11:47
  • Now all i get in my output file is -> text=01000000237843141111608164052693183714. There need to be other text too. – kb2487 Aug 16 '16 at 12:14
  • Arg, sorry - Im not able to test the script since I don't have an example file. Should be fixed now... – Martin Brandl Aug 16 '16 at 12:22
  • I'm thankful for your help so not complaining at all :) Now it's working great except for one thing mentioned above -> old text (140526 ) should be replaced with new one (160816), now they are side by side if you look my output 010000002378431411*1[160816]40526*93183714. I marked old text between ** and new text between [ ]. – kb2487 Aug 16 '16 at 12:42
  • I edited my answer again to fix that ;-). Please consider to accepting the answer if your question is solved. – Martin Brandl Aug 16 '16 at 12:45
  • Perfect! I really thank you for all your help, i've learned something new today :) – kb2487 Aug 16 '16 at 17:53