txt 파일에서 배치 / 찾기 및 편집
-
12-09-2019 - |
문제
배치 파일에서 특정 줄을 찾아이 줄을 편집 할 수있는 배치를 만들고 싶습니다.
예시:
// txt 파일 //
ex1
ex2
ex3
ex4
배치 파일이 'ex3'을 찾아서 이것을 'ex5'로 편집하여 다음과 같이 보이도록하고 싶습니다.
ex1
ex2
ex5
ex4
해결책
기본 Windows 설치에서는 외부 도구를 얻을 필요없이 Batch (cmd.exe) 또는 vbscript를 사용할 수 있습니다. VBScript의 예는 다음과 같습니다.
Set objFS = CreateObject("Scripting.FileSystemObject")
strFile = "c:\test\file.txt"
Set objFile = objFS.OpenTextFile(strFile)
Do Until objFile.AtEndOfStream
strLine = objFile.ReadLine
If InStr(strLine,"ex3")> 0 Then
strLine = Replace(strLine,"ex3","ex5")
End If
WScript.Echo strLine
Loop
myreplace.vbs로 저장하고 명령 줄에 저장하십시오.
c:\test> cscript /nologo myreplace.vbs > newfile
c:\test> ren newfile file.txt
다른 팁
XP 또는 2K3의 명령 줄에서 검색 및 교체 기능 또는 스트림 편집이 없습니다 (Vista 또는 그 이상에 대해 알지 못함). 따라서 Ghostdog가 게시 한 것과 같은 스크립트 또는 SED와 같은 유능한 도구와 같은 스크립트를 사용해야합니다.
이 스크립트가 보여 주듯이 다음과 같은 방법은 다음과 같습니다.
@echo off
SETLOCAL=ENABLEDELAYEDEXPANSION
rename text.file text.tmp
for /f %%a in (text.tmp) do (
set foo=%%a
if !foo!==ex3 set foo=ex5
echo !foo! >> text.file)
del text.tmp
Ghostdog74의 예는 이전에 VBS를 작성한 적이없고 그렇게해야했기 때문에 필요한 것의 핵심을 제공했습니다. 완벽하지는 않지만 누군가가 유용하다고 생각할 경우를 대비하여 예제를 전체 대본으로 살펴 보았습니다.
'ReplaceText.vbs
Option Explicit
Const ForAppending = 8
Const TristateFalse = 0 ' the value for ASCII
Const Overwrite = True
Const WindowsFolder = 0
Const SystemFolder = 1
Const TemporaryFolder = 2
Dim FileSystem
Dim Filename, OldText, NewText
Dim OriginalFile, TempFile, Line
Dim TempFilename
If WScript.Arguments.Count = 3 Then
Filename = WScript.Arguments.Item(0)
OldText = WScript.Arguments.Item(1)
NewText = WScript.Arguments.Item(2)
Else
Wscript.Echo "Usage: ReplaceText.vbs <Filename> <OldText> <NewText>"
Wscript.Quit
End If
Set FileSystem = CreateObject("Scripting.FileSystemObject")
Dim tempFolder: tempFolder = FileSystem.GetSpecialFolder(TemporaryFolder)
TempFilename = FileSystem.GetTempName
If FileSystem.FileExists(TempFilename) Then
FileSystem.DeleteFile TempFilename
End If
Set TempFile = FileSystem.CreateTextFile(TempFilename, Overwrite, TristateFalse)
Set OriginalFile = FileSystem.OpenTextFile(Filename)
Do Until OriginalFile.AtEndOfStream
Line = OriginalFile.ReadLine
If InStr(Line, OldText) > 0 Then
Line = Replace(Line, OldText, NewText)
End If
TempFile.WriteLine(Line)
Loop
OriginalFile.Close
TempFile.Close
FileSystem.DeleteFile Filename
FileSystem.MoveFile TempFilename, Filename
Wscript.Quit
창문에있는 경우 사용할 수 있습니다 방귀 (에프인디 ㅏnd 아르 자형eplace 티ext). 단일 *.exe 파일 (라이브러리가 필요 없음)입니다.
실행하기 만하면됩니다.
fart.exe your_batch_file.bat ex3 ex5
이것은 일종의 물건입니다 sed
(물론 시스템에 SED가 필요합니다).
sed 's/ex3/ex5/g'input.txt> output.txt
유닉스 시스템이나 창이 필요합니다. Cygwin 이것을위한 종류의 플랫폼.
도 있습니다 sed를위한 gnuwin32. (GNUWIN32 설치 및 사용).
언제든지 "far"= "찾기 및 교체"를 사용할 수 있습니다. Java 아래에 작성되었으므로 Java가 작동하는 곳에서 작동합니다 (거의 모든 곳). 디렉토리 및 하위 디렉토리와 협력하고 파일 내에서 검색 및 대체하면 이름을 바꿀 수도 있습니다. 또한 개인 또는 코마 니 모두에 대해 대량 파일 이름을 바꿀 수 있습니다. licence = 무료. 개발자가 매우 빠르고 유지합니다. 여기에서 찾으십시오. http://findandreplace.sourceforge.net/
또한 Grepwin을 사용할 수 있습니다. 거의 동일하게 작동합니다. 여기에서 찾을 수 있습니다. http://tools.tortoisesvn.net/grepwin.html
@echo off
set "replace=something"
set "replaced=different"
set "source=Source.txt"
set "target=Target.txt"
setlocal enableDelayedExpansion
(
for /F "tokens=1* delims=:" %%a in ('findstr /N "^" %source%') do (
set "line=%%b"
if defined line set "line=!line:%replace%=%replaced%!"
echo(!line!
)
) > %target%
endlocal
원천. 그것이 도움이되기를 바라고 있습니다.
당신은 이것을 좋아할 수 있습니다 :
rename %CURR_DIR%\ftp\mywish1.txt text.txt
for /f %%a in (%CURR_DIR%\ftp\text.txt) do (
if "%%a" EQU "ex3" (
echo ex5 >> %CURR_DIR%\ftp\mywish1.txt
) else (
echo %%a >> %CURR_DIR%\ftp\mywish1.txt
)
)
del %CURR_DIR%\ftp\text.txt