Windows Batch script tp process flat files -
i want in writing windows batch script. have delimeted text file data shown below:
1|2|3|4|5|6 1|2|3|4|5|6 1|2|3|4|5|6 1|2|3|4|5|6 1|2|3|4|5|6 1|2|3|4|5|6.
i want write script can edit text file remove first , last fields of data. output text file containing data :
2|3|4|5 2|3|4|5 2|3|4|5 2|3|4|5 2|3|4|5 2|3|4|5.
please me.
thanks in advance.
regards, vibhor
it possible write pure batch solution, write robust solution can handle throw @ quite tricky, , final result slow.
it quite easy accomplish task jscript , regular expressions, not knows technologies (although there more know jscript , regex know batch!)
i have written hybrid jscript/batch utility called repl.bat called command line or batch file. performs regular expression search , replace on stdin , writes result stdout. works on modern versions of windows, starting xp.
your task , efficiently accomplished using repl.bat
assuming every line has 6 values (empty values ok, has 5 delimiters):
type test.txt|repl "^[^|]*\|(.*)\|[^|]*$" "$1" >test.txt.new move /y test.txt.new test.txt
if lines missing values @ end (missing values assumed empty):
type test.txt|repl "(^[^|]*(\|[^|]*){4})\|[^|]*$" "$1"|repl "^[^|]*\|?(.*)$" "$1" >test.txt.new move /y test.txt.new test.txt
here repl.bat utility. should placed in same folder batch script , file, or better yet placed in folder resides in path. full documentation built script.
@if (@x)==(@y) @end /* harmless hybrid line begins jscript comment ::************ documentation *********** ::: :::repl search replace [options [sourcevar]] :::repl /? ::: ::: performs global search , replace operation on each line of input ::: stdin , prints result stdout. ::: ::: each parameter may optionally enclosed double quotes. double ::: quotes not considered part of argument. quotes required ::: if parameter contains batch token delimiter space, tab, comma, ::: semicolon. quotes should used if argument contains ::: batch special character &, |, etc. special character ::: not need escaped ^. ::: ::: if called single argument of /? prints documentation ::: stdout. ::: ::: search - default case sensitive jscript (ecma) regular ::: expression expressed string. ::: ::: jscript regex syntax documentation available @ ::: http://msdn.microsoft.com/en-us/library/ae5bf541(v=vs.80).aspx ::: ::: replace - default string used replacement ::: each found search expression. full support provided ::: substituion patterns available jscript replace method. ::: $ literal can escaped $$. empty replacement string ::: must represented "". ::: ::: replace substitution pattern syntax documented @ ::: http://msdn.microsoft.com/en-us/library/efy6s3e6(v=vs.80).aspx ::: ::: options - optional string of characters used alter behavior ::: of repl. option characters case insensitive, , may ::: appear in order. ::: ::: - makes search case-insensitive. ::: ::: l - search treated string literal instead of ::: regular expression. also, $ found in replace ::: treated $ literals. ::: ::: b - search must match beginning of line. ::: used literal searches. ::: ::: e - search must match end of line. ::: used literal searches. ::: ::: v - search , replace represent name of environment ::: variables contain respective values. undefined ::: variable treated empty string. ::: ::: m - multi-line mode. entire contents of stdin read , ::: processed in 1 pass instead of line line. ^ anchors ::: beginning of line , $ anchors end of line. ::: ::: x - enables extended substitution pattern syntax support ::: following escape sequences: ::: ::: \\ - backslash ::: \b - backspace ::: \f - formfeed ::: \n - newline ::: \r - carriage return ::: \t - horizontal tab ::: \v - vertical tab ::: \xnn - ascii (latin 1) character expressed 2 hex digits ::: \unnnn - unicode character expressed 4 hex digits ::: ::: escape sequences supported when l option used. ::: ::: s - source read environment variable instead of ::: stdin. name of source environment variable ::: specified in next argument after option string. ::: ::************ batch portion *********** @echo off if .%2 equ . ( if "%~1" equ "/?" ( findstr "^:::" "%~f0" | cscript //e:jscript //nologo "%~f0" "^:::" "" exit /b 0 ) else ( call :err "insufficient arguments" exit /b 1 ) ) echo(%~3|findstr /i "[^smilebvx]" >nul && ( call :err "invalid option(s)" exit /b 1 ) cscript //e:jscript //nologo "%~f0" %* exit /b 0 :err >&2 echo error: %~1. use repl /? help. exit /b ************* jscript portion **********/ var env=wscript.createobject("wscript.shell").environment("process"); var args=wscript.arguments; var search=args.item(0); var replace=args.item(1); var options="g"; if (args.length>2) { options+=args.item(2).tolowercase(); } var multi=(options.indexof("m")>=0); var srcvar=(options.indexof("s")>=0); if (srcvar) { options=options.replace(/s/g,""); } if (options.indexof("v")>=0) { options=options.replace(/v/g,""); search=env(search); replace=env(replace); } if (options.indexof("l")>=0) { options=options.replace(/l/g,""); search=search.replace(/([.^$*+?()[{\\|])/g,"\\$1"); replace=replace.replace(/\$/g,"$$$$"); } if (options.indexof("b")>=0) { options=options.replace(/b/g,""); search="^"+search } if (options.indexof("e")>=0) { options=options.replace(/e/g,""); search=search+"$" } if (options.indexof("x")>=0) { options=options.replace(/x/g,""); replace=replace.replace(/\\\\/g,"\\b"); replace=replace.replace(/\\b/g,"\b"); replace=replace.replace(/\\f/g,"\f"); replace=replace.replace(/\\n/g,"\n"); replace=replace.replace(/\\r/g,"\r"); replace=replace.replace(/\\t/g,"\t"); replace=replace.replace(/\\v/g,"\v"); replace=replace.replace(/\\x[0-9a-fa-f]{2}|\\u[0-9a-fa-f]{4}/g, function($0,$1,$2){ return string.fromcharcode(parseint("0x"+$0.substring(2))); } ); replace=replace.replace(/\\b/g,"\\"); } var search=new regexp(search,options); if (srcvar) { wscript.stdout.write(env(args.item(3)).replace(search,replace)); } else { while (!wscript.stdin.atendofstream) { if (multi) { wscript.stdout.write(wscript.stdin.readall().replace(search,replace)); } else { wscript.stdout.writeline(wscript.stdin.readline().replace(search,replace)); } } }
Comments
Post a Comment