2012-11-01 6 views
0

저는 메모장 + +로 이것을 해왔지만 영원히 걸립니다. 폴더에 몇 천 개의 파일이 있고 각 파일의 모든 줄 앞뒤에 텍스트를 추가해야합니다.여러 파일의 모든 줄에 접두어와 접미사 추가 - 명령 줄 사용?

명령 줄에서도 가능합니까? 예를 들어이 들어

는 라인 :

This is my line. 

그리고 나는 그것이처럼 설정하려면 :

<text before>This is my line.<text after> 

이 어떤 도움을 주시면 감사하겠습니다!

답변

0

안된하지만 트릭을 수행해야합니다 상대적으로 느린하지만,

perl -pi -e 's/(.*)/<text before>$1<text after>/' * 
+0

나는 Cygwin에서이 오류를 받고 있어요 : 다른 솔루션의 알고 "을 (를) 찾을 수 없습니다 명령 bash는 : 펄을?" 감사! – user1135462

0

다음 순수 네이티브 배치 스크립트가 작동합니다.

@echo off 
setlocal disableDelayedExpansion 
set "prefix=<text before>" 
set "suffix=<text after>" 
pushd "yourFolder" 
for %%F in (*) do (
    >"%%F.new" (
    for /f "delims=" %%A in ('findstr /n "^" "%%F"') do (
     set "ln=%%A" 
     setlocal enableDelayedExpansion 
     echo !prefix!!ln:*:=!!suffix! 
     endlocal 
    ) 
) 
    move /y "%%F.new" "%%F" >nul 
) 

텍스트 파일을 매우 효율적으로 처리하는 데 사용할 수있는 하이브리드 JScript/Batch 스크립트를 작성했습니다. REPL.BAT 스크립트가 PATH에 어딘가에 있어야한다

@echo off 
pushd "yourFolder" 
for %%F in (*) do (
    type "%%F" | repl "^(.*)" "<text before>$1<text after>" >"%%F.new" 
    move /y "%%F.new" "%%F" >nul 
) 
popd 

: 내 REPL.BAT 유틸리티를 사용하여 스크립트로 간단 할 수있다.

다음은 REPL.BAT 스크립트입니다. XP 이후의 모든 최신 Windows 버전과 호환됩니다. 이 유틸리티는 놀라 울 정도로 강력하고 사용하기 쉽습니다. 스크립트의 거의 절반은 임베디드 문서로 구성됩니다. 명령 프롬프트에서 REPL /?을 입력하면 설명서를 볼 수 있습니다.

@if (@X)==(@Y) @end /* Harmless hybrid line that begins a JScript comment 

::************ Documentation *********** 
::: 
:::REPL Search Replace [Options [SourceVar]] 
:::REPL /? 
::: 
::: Performs a global search and replace operation on each line of input from 
::: stdin and prints the result to stdout. 
::: 
::: Each parameter may be optionally enclosed by double quotes. The double 
::: quotes are not considered part of the argument. The quotes are required 
::: if the parameter contains a batch token delimiter like space, tab, comma, 
::: semicolon. The quotes should also be used if the argument contains a 
::: batch special character like &, |, etc. so that the special character 
::: does not need to be escaped with ^. 
::: 
::: If called with a single argument of /? then prints help documentation 
::: to stdout. 
::: 
::: Search - By default this is a case sensitive JScript (ECMA) regular 
:::   expression expressed as a string. 
::: 
:::   JScript syntax documentation is available at 
:::   http://msdn.microsoft.com/en-us/library/ae5bf541(v=vs.80).aspx 
::: 
::: Replace - By default this is the string to be used as a replacement for 
:::   each found search expression. Full support is provided for 
:::   substituion patterns available to the JScript replace method. 
:::   A $ literal can be escaped as $$. An empty replacement string 
:::   must be represented as "". 
::: 
:::   Replace substitution pattern syntax is documented at 
:::   http://msdn.microsoft.com/en-US/library/efy6s3e6(v=vs.80).aspx 
::: 
::: Options - An optional string of characters used to alter the behavior 
:::   of REPL. The option characters are case insensitive, and may 
:::   appear in any order. 
::: 
:::   I - Makes the search case-insensitive. 
::: 
:::   L - The Search is treated as a string literal instead of a 
:::    regular expression. Also, all $ found in Replace are 
:::    treated as $ literals. 
::: 
:::   E - Search and Replace represent the name of environment 
:::    variables that contain the respective values. An undefined 
:::    variable is treated as an empty string. 
::: 
:::   M - Multi-line mode. The entire contents of stdin is read and 
:::    processed in one pass instead of line by line.^anchors 
:::    the beginning of a line and $ anchors the end of a line. 
::: 
:::   X - Enables extended substitution pattern syntax with support 
:::    for the 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 as 2 hex digits 
:::    \unnnn - Unicode character expressed as 4 hex digits 
::: 
:::    Escape sequences are supported even when the L option is used. 
::: 
:::   S - The source is read from an environment variable instead of 
:::    from stdin. The name of the source environment variable is 
:::    specified in the next argument after the 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 "[^SMILEX]" >nul && (
    call :err "Invalid option(s)" 
    exit /b 1 
) 
cscript //E:JScript //nologo "%~f0" %* 
exit /b 0 

:err 
>&2 echo ERROR: %~1. Use REPL /? to get 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("e")>=0) { 
    options=options.replace(/e/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("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)); 
    } 
    } 
} 
관련 문제