find and edit a txt file with a batch program -
i want edit txt file batch script. have looked online , tried adjust examples suit needs. have no programing skills , have not been able program work.
here problem. have txt file contains @ point in file:
subcase mode buckling eigenvalue 321043 1 2.124238e+00 321043 2 2.169874e+00 321043 3 2.628187e+00 321043 4 2.832127e+00 321043 5 2.968359e+00 321043 6 3.131774e+00
i want change this:
r e l e g e n v l u e s subcase mode buckling eigenvalue 321043 1 2.630623e-01 321043 2 2.676471e-01 321043 3 2.982211e-01
so add "r e l e g e n v l u e s" 2 lines before "subcase mode buckling eigenvalue", without deleting whats in lines before. i.e add 2 new lines in.
i have tried following, printed out first 8 lines new text file
@echo off /f "tokens=* delims=" %%a in (le_panel_7_cut down_i.out) call :change "%%a" exit /b :change set text=%~1 if "%text%"=="subcase mode buckling eigenvalue" ( (echo r e l e g e n v l u e s &echo[ &echo[ &echo[subcase mode buckling eigenvalue)>> d:\fea\newfile.txt ) else ( (echo %text%)>> d:\fea\newfile.out ) exit /b
i tried script looked this, didn't work either.
@echo on setlocal=enabledelayedexpansion /f "delims=" %%a in (le_panel_7_cut_down_i.out) ( set foo=%%a if !foo!=="subcase mode buckling eigenvalue" set foo=( echo r e l e g e n v l u e s &echo[ &echo[ &echo[subcase mode buckling eigenvalue) echo !foo! >> file3.txt)
if correct script appreciated. regards iain
@echo off setlocal enabledelayedexpansion ( /f "delims=" %%z in (' type eigenfile.txt^|findstr /n /r "$" ') ( set "line=%%z" set "line=!line:*:=!" if "!line!"=="subcase mode buckling eigenvalue" ( echo(r e l e g e n v l u e s echo( ) echo(!line! ) )>outfile.txt fc eigenfile.txt outfile.txt goto :eof
this should execute revised specification.
essentially, reproduces each line, including empty lines (the reason findstr /n
, following set ...!..:*:=!"
removes line-number prefixed findstr
). when finds target line, first adds 2 lines specified.
note echo(
has been proved best choice of various echo
variants allow instance, empty variable, reproduced correctly.
i've assumed data in eigenfile.txt
. batch produces outfile.txt
- option rename - , fc show changes made.
Comments
Post a Comment