programing tip

cmd.exe에 대한 sed like utility가 있습니까?

itbloger 2020. 6. 18. 21:30
반응형

cmd.exe에 대한 sed like utility가 있습니까? [닫은]


Windows 명령 줄 ( cmd.exe )을 사용하여 프로그래밍 방식으로 파일 내용을 편집하고 싶습니다 . * nix에는 이 작업을위한 sed 가 있습니다. 창문에 유용한 기능이 있습니까?

편집 : 기본 명령 줄 솔루션을 찾고 있습니다.


오늘 powershell은 저를 구했습니다.

들어 grep있다 :

get-content somefile.txt | where { $_ -match "expression"}

그리고 sed거기에는 :

get-content somefile.txt | %{$_ -replace "expression","replace"}

자세한 내용은 Zain Naboulsis 블로그 항목을 참조하십시오 .


sed (및 그 ilk)는 여러 유닉스 명령 패키지에 포함되어 있습니다.

아무것도 설치하지 않고 시스템이 Windows Server가 아닌 경우 스크립팅 언어 (예 : VBScript)를 사용할 있습니다. 아래는 총에 맞지 않는 갑상선입니다. 커맨드 라인은 다음과 같습니다

cscript //NoLogo sed.vbs s/(oldpat)/(newpat)/ < inpfile.txt > outfile.txt

여기서 oldpat 및 newpat는 Microsoft vbscript 정규식 패턴 입니다. 분명히 나는 ​​대체 명령 만 구현하고 몇 가지 사항을 가정했지만 sed명령을 더 똑똑하고 이해하기 위해 육체를 만들 수있었습니다 .

Dim pat, patparts, rxp, inp
pat = WScript.Arguments(0)
patparts = Split(pat,"/")
Set rxp = new RegExp
rxp.Global = True
rxp.Multiline = False
rxp.Pattern = patparts(1)
Do While Not WScript.StdIn.AtEndOfStream
  inp = WScript.StdIn.ReadLine()
  WScript.Echo rxp.Replace(inp, patparts(2))
Loop

UnxUtilsGNUWin32 와 마찬가지로 Win32 용 sed를 제공합니다 .


아무것도 설치하고 싶지 않다면 (다른 컴퓨터에서 실행될 솔루션 / 프로그램 등에 스크립트를 추가한다고 가정합니다) vbs 스크립트를 만들 수 있습니다 (replace.vbs).

Const ForReading = 1
Const ForWriting = 2

strFileName = Wscript.Arguments(0)
strOldText = Wscript.Arguments(1)
strNewText = Wscript.Arguments(2)

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile(strFileName, ForReading)

strText = objFile.ReadAll
objFile.Close
strNewText = Replace(strText, strOldText, strNewText)

Set objFile = objFSO.OpenTextFile(strFileName, ForWriting)
objFile.Write strNewText
objFile.Close

그리고 당신은 이것을 다음과 같이 실행합니다 :

cscript replace.vbs "C:\One.txt" "Robert" "Rob"

"bill weaver"에서 제공하는 sed 버전과 비슷하지만이 문자가 특수 ( '> </) 문자 측면에서 더 친숙하다고 생각합니다.

Btw, 나는 이것을 쓰지 않았지만 어디서 왔는지 기억할 수 없다.


There is Super Sed an enhanced version of sed. For Windows this is a standalone .exe, intended for running from the command line.


Try fart.exe. It's a Find-and-replace-text utility that can be used in command batch programs.

http://sourceforge.net/projects/fart-it/


> (Get-content file.txt) | Foreach-Object {$_ -replace "^SourceRegexp$", "DestinationString"} | Set-Content file.txt

This is behaviour of

sed -i 's/^SourceRegexp$/DestinationString/g' file.txt

You could install Cygwin (http://www.cygwin.com/) and use sed from there.


You could try powershell. There are get-content and set-content commandlets build in that you could use.


I use Cygwin. I run into a lot of people that do not realize that if you put the Cygwin binaries on your PATH, you can use them from within the Windows Command shell. You do not have to run Cygwin's Bash.

You might also look into Windows Services for Unix available from Microsoft (but only on the Professional and above versions of Windows).


edlin or edit

plus there is Windows Services for Unix which comes with many unix tools for windows. http://technet.microsoft.com/en-us/interopmigration/bb380242.aspx

Update 12/7/12 In Windows 2003 R2, Windows 7 & Server 2008, etc. the above is replaced by the Subsystem for UNIX-Based Applications (SUA) as an add-on. But you have to download the utilities: http://www.microsoft.com/en-us/download/details.aspx?id=2391


You could look at GNU Tools, they provide (amongst other things) sed on windows.


There is a helper batch file for Windows called repl.bat which has much of the ability of SED but doesn't require any additional download or installation. It is a hybrid batch file that uses Jscript to implement the features and so is swift, and doesn't suffer from the usual poison characters of batch processing and handles blank lines with ease.

Download repl from - https://www.dropbox.com/s/qidqwztmetbvklt/repl.bat

The author is @dbenham from stack overflow and dostips.com

Another helper batch file called findrepl.bat gives the Windows user much of the capabilty of GREP and is also based on Jscript and is likewise a hybrid batch file. It shares the benefits of repl.bat

Download findrepl from - https://www.dropbox.com/s/rfdldmcb6vwi9xc/findrepl.bat

The author is @aacini from stack overflow and dostips.com


As far as I know nothing like sed is bundled with windows. However, sed is available for Windows in several different forms, including as part of Cygwin, if you want a full POSIX subsystem, or as a Win32 native executable if you want to run just sed on the command line.

Sed for Windows (GnuWin32 Project)

If it needs to be native to Windows then the only other thing I can suggest would be to use a scripting language supported by Windows without add-ons, such as VBScript.


Cygwin works, but these utilities are also available. Just plop them on your drive, put the directory into your path, and you have many of your friendly unix utilities. Lighterweight IMHO that Cygwin (although that works just as well).


I needed a sed tool that worked for the Windows cmd.exe prompt. Eric Pement's port of sed to a single DOS .exe worked great for me.

It's pretty well documented.


This works on Vista Ultimate, not sure about Pro.

sed -f commandfilename.cmd file1 > file2

참고URL : https://stackoverflow.com/questions/127318/is-there-any-sed-like-utility-for-cmd-exe

반응형