ابحث عن الخط وتعديله
-
12-09-2019 - |
سؤال
لقد نشرت واحدة أخرى في هذا، ولكن كان علي تحريرها كثيرا ...
ما هو الشيء bassicly هو أن الدفعة (ربما بما في ذلك VBScript) يمكن أن تجد خط 29 في txtfile ... يجب أن تحرير هذا الخط.
إذا كان السطر 29 مثل هذا: 'الخيار = 21' يجب أن يغيره إلى "الخيار = 22"
المشكلة هي أن هذا الخط موجود أكثر في الملف. لذلك يجب فقط تحرير السطر 29 ...
كيف؟؟؟
يرجى عدم وجود برامج مخصصة ؛؛؛ يجب أن يتم ذلك من قبل كل مستخدم دون تثبيت شيء ما.
المحلول
هذا ليس شيئا تفعله عادة في الدفعة، لكنه واضح إلى حد ما:
@echo off
setlocal enableextensions enabledelayedexpansion
rem the input file
set inputfile=file.txt
rem temporary file for output, we can't just write into the
rem same file we're reading
set tempfile=%random%-%random%.tmp
rem delete the temporary file if it's already there
rem shouldn't really happen, but just in case ...
copy /y nul %tempfile%
rem line counter
set line=0
rem loop through the file
for /f "delims=" %%l in (%inputfile%) do (
set /a line+=1
if !line!==29 (
rem hardcoded, at the moment, you might want to look
rem here whether the line really starts with "options"
rem and just put another number at the end.
echo option=22>>%tempfile%
) else (
echo %%l>>%tempfile%
)
)
del %inputfile%
ren %tempfile% %inputfile%
endlocal
يجب أن يشيرك إلى الاتجاه العام إذا كنت ترغب في تكييفه.
نصائح أخرى
أراك تسأل عن vb
النصي،
استخدام AWK
, ، سوف يذهب شيء مثل هذا،
ربما سوف يساعدك على الكود vb
.
AWK '{IF (FNR == 29) {GSUB (/ الخيار = 21/21 /، "الخيار = 22")؛ طباعة} آخر {طباعة $ 0؛}} 'Input.txt> OXTXT.TXT
لم تحاول ذلك، لذلك قد يكون بعض مواطن الخلل المعتدل ...
- ال
FNR=29
سوف تحقق من المعالجةgsub
فقط على الخط 29gsub
سوف يستبدل أيoption=21
الحوادث على الخط معoption=22
- سيتم تمرير الخطوط الأخرى
إليك VBScript
Set objFS = CreateObject("Scripting.FileSystemObject")
strFile = "c:\test\file.txt"
Set objFile = objFS.OpenTextFile(strFile)
Do Until objFile.AtEndOfStream
linenum = objFile.Line
strLine = objFile.ReadLine
If linenum = 29 Then
strLine = Replace(strLine,"option=21","option=22")
End If
WScript.Echo strLine
Loop
كيف تستعمل:
c:\test> cscript /nologo myscript.vbs > newfile
c:\test> ren newfile file.txt