![특정 너비의 파일에서 텍스트 바꾸기](https://rvso.com/image/1254834/%ED%8A%B9%EC%A0%95%20%EB%84%88%EB%B9%84%EC%9D%98%20%ED%8C%8C%EC%9D%BC%EC%97%90%EC%84%9C%20%ED%85%8D%EC%8A%A4%ED%8A%B8%20%EB%B0%94%EA%BE%B8%EA%B8%B0.png)
특정 열 위치에서 어떻게 교체할 수 있나요?
예: 20-26열을 0으로 바꾸고 싶습니다.
에서:
12345678123456781234567812345678123456781234567812345678123456781234567812345678
에게:
12345678123456781234000000345678123456781234567812345678123456781234567812345678
이는 이와 같은 수만 줄을 포함할 수 있는 파일의 각 줄에 대한 것입니다.
답변1
구체적으로 하나를 알지는 못하지만 당신을 위해 하나만 썼습니다. 에 쓰여 있습니다자동. 아래 코드를 사용하여 직접 다운로드하여 컴파일하거나, 컴파일된 버전을 다운로드할 수 있습니다.여기. 변경사항을 원하시면 알려주세요. 제가 어떻게 할 수 있는지 살펴보겠습니다.
편집: 파일에도 이 작업을 수행해야 하는 경우 알려주시면 변경해 드리겠습니다.
편집 #2: 방금 파일 작업을 위해 업데이트했습니다(어떤 형식이 필요한지는 몰랐지만 텍스트/로그 파일 이외의 작업을 수행하는 것이 얼마나 쉬운지는 잘 모르겠습니다). 범위를 벗어난 모든 라인을 보고합니다. 또한 위의 링크를 새로운 컴파일된 버전으로 업데이트했습니다.
#include <GuiConstants.au3>
#include <WindowsConstants.au3>
#include <EditConstants.au3>
#include <GuiEdit.au3>
#include <File.au3>
Dim $s_Name = "FileStringReplacer"
Main()
Func Main()
$h_Parent = GuiCreate($s_Name,400,340)
GuiCtrlCreateLabel("File to Edit",5,5,90,20)
$in_File = GuiCtrlCreateInput("",10,25,380,20,$ES_READONLY)
$bt_Browse = GuiCtrlCreateButton("Find File",10,50,380,25)
GuiCtrlCreateLabel("Request Status",5,80,90,20)
$ed_Status = GuiCtrlCreateEdit("",10,100,380,100,$ES_READONLY + $WS_VSCROLL)
GuiCtrlCreateLabel("Start Point",5,210,50,20)
$in_Start = GuiCtrlCreateInput("",60,210,25,20)
GuiCtrlCreateLabel("Stop Point",95,210,50,20)
$in_Stop = GuiCtrlCreateInput("",150,210,25,20)
GuiCtrlCreateLabel("Text to Insert",5,240,90,20)
$in_String = GuiCtrlCreateInput("",10,260,380,20)
$bt_Replace = GuiCtrlCreateButton("Replace",10,290,380,25)
GuiSetState()
Do
$Msg = GUIGetMsg()
If $Msg = $bt_Browse Then
$h_File = FileOpenDialog("File to Modify",@MyDocumentsDir,"Text (*.txt;*.log)", 3)
GuiCtrlSetData($in_File,$h_File)
EndIf
If $Msg = $bt_Replace Then
GuiCtrlSetData($ed_Status,"")
If GuiCtrlRead($in_File) <> "" Then
If IsInt(Int(GuiCtrlRead($in_Start))) and IsInt(Int(GuiCtrlRead($in_Stop))) and Int(GuiCtrlRead($in_Start)) <> 0 and Int(GuiCtrlRead($in_Stop)) <> 0 Then
GuiCtrlSetState($bt_Browse,$GUI_DISABLE)
GuiCtrlSetState($bt_Replace,$GUI_DISABLE)
$h_File = FileOpen(GuiCtrlRead($in_File))
$i = 1
$s_Line = ""
While 1
$s_Line = FileReadLine($h_File,$i)
If @error Then
ExitLoop
EndIf
If StringLen($s_Line) > GuiCtrlRead($in_Stop) - GuiCtrlRead($in_Start) Then
$stringright = StringTrimLeft($s_Line,GuiCtrlRead($in_Stop))
$stringleft = StringLeft($s_Line,GuiCtrlRead($in_Start) -1 )
$newstring = $stringleft & GuiCtrlRead($in_String) & $stringright
_FileWriteToLine(GuiCtrlRead($in_File), $i, $newstring, 1)
Else
_GUICtrlEdit_AppendText(GuiCtrlGetHandle($ed_Status), @CRLF & "Edit to line " & $i & " failed, specified position is out of range")
EndIf
$i = $i + 1
WEnd
FileClose($h_File)
GuiCtrlSetState($bt_Browse,$GUI_ENABLE)
GuiCtrlSetState($bt_Replace,$GUI_ENABLE)
Else
GuiCtrlSetData($ed_Status,"Error 102" & @CRLF & "Start and stop points must be numbers")
EndIf
Else
GuiCtrlSetData($ed_Status,"Error 101" & @CRLF & "You must specify a file to edit")
EndIf
EndIf
Until $Msg = $GUI_EVENT_CLOSE
GUIDelete($h_Parent)
EndFunc
답변2
어떤 텍스트 편집기에서도 간단할 것입니다. 현재 줄의 gvim에서 명령은 다음과 같을 수 있습니다.020lc6l000000<esc>
정규 표현식을 피하고 싶은 특별한 이유가 있나요?
편집하다
다음은 간단한 정규식을 포함하는 명령문입니다.s/^(.{19}).{6}/${1}000000/
검색한다는 뜻이다
^ start at the beginning of a line
( remember stuff found between parentheses
. any single character
{19} exactly 19 of them
) end of the part we want to remember
.{6} followed by any 6 characters which we won't remember
그리고 그것들을 다음으로 대체하십시오.
${1} the first set of characters we remembered using () - 19 chars
000000 followed by six zeros (replacing the .{6} we ignore)
${1}
일반적으로 다음 과 같이 작성 $1
하지만 그 뒤에 숫자가 오면 {}를 사용하여 기억된 10번째 부분 $10과 첫 번째 부분 $1 뒤에 리터럴 0이 오는 것을 구별해야 합니다.
이렇게 사용됨
perl -pie 's/^(.{19}).{6}/${1}000000/' foo.txt bar.txt any*.txt
원하는 수의 파일에 있는 모든 줄을 내부 편집할 수 있습니다.하지만 먼저 파일 복사본에서 테스트해 보세요!
답변3
답변4
정력아주 쉽게 할 수 있어요.
Vim에서 파일을 열고 다음을 입력하세요.
:% normal 019l6r0<Enter>
:% normal
파일의 모든 줄에서 다음 명령을 수행한다는 의미입니다.
0
의미: 커서를 줄의 시작 부분으로 이동합니다.
19l
의미: 커서를 오른쪽으로 19번 이동
6r0
의미: 다음 6자를 0으로 바꿉니다.
처음에는 명령이 약간 비밀스러워 보일 수도 있다는 것을 알고 있습니다. 하지만 필요한 작업을 수행하려면 몇 가지 매우 간단한 명령만 배우면 됩니다.
0
커서를 줄의 시작 부분으로 이동
$
커서를 줄의 끝으로 이동
h
커서를 왼쪽으로 이동
j
커서를 아래로
k
이동 커서를 위로
l
이동 커서를 오른쪽으로 이동
r
커서 아래의 문자를 누른 다음 문자로 바꿉니다.
해당 명령이 "숫자"번 수행되도록 하려면 명령 앞에 숫자를 입력하십시오.
:% normal <list of commands><Enter>
파일의 모든 줄에 있는 명령 목록을 수행합니다.
여러 가지 편집 내용을 기록한 다음 파일의 모든 줄에서 동일한 편집 내용을 재생할 수도 있습니다.
명령을 기록합니다.
qa<commands>q
그런 다음 파일의 모든 줄에서 명령을 재생하십시오.
:% normal @a<Enter>