Extract section of a text file - text

I am trying to write a batch file that will extract lines 6000 to 6999 in a give text file. From googling I have come accross the following code - however this is giving me a blank output file.
#echo off
SetLocal EnableDelayedExpansion
type nul > nodedata.txt
set StartText=6000
set EndText=7000
set Flag=0
for /f "tokens=* delims=" %%a in ('type out.txt') do (
if /i "%StartText%" EQU "%%a" (set Flag=1)
if /i "%EndText%" EQU "%%a" (set Flag=0)
if !Flag! EQU 1 echo %%a >> nodedata1.txt
)
Any ideas as to where I am going wrong?

Here is a quick and simple pure batch solution
for /l %%a in (6000,1,6999) do (
more C:\file.txt +%%a >>C:\extracted.txt
)

You should install unxutils and then see answers for this question... Windows is just not made for text processing...
A Windows user...

This is a Batch solution that run faster...
#echo off
SetLocal EnableDelayedExpansion
set count=0
(for /F "skip=5999 delims=" %%a in (out.txt) do (
echo %%a
set /A count+=1
if !count! equ 1000 goto endLoop
)
) > nodedata1.txt
:endLoop

Related

Batch - Get System Language of current running System

I am trying to get the current setted system-language under use of wmic,
but i don´t know why my code here is kind of "broken"...
The readback info from wmiccontains a lot of spaces and CRLF´s.
Therefore i wanted to cut all that out in the loop, but the syntax is not proper in my example...
#echo off
SETLOCAL ENABLEDELAYEDEXPANSION
:MAIN
setlocal
call :Get_System_Language "lang"
echo lang=%lang%
pause
exit /b 0
:Get_System_Language
setlocal
::Windows Language Code Identifier
::1031 German Germany
::2057 English United Kingdom
::1033 English United States
set "language=NUL"
set /a num_languages=3
for /f "tokens=1,2 delims==" %%a in ('wmic os get OSLanguage /Value') do (
if "%%a" equ "OSLanguage" (
set "item_tmp=%%b"
set "value=!item_tmp:=!"
if "!value!" equ "1031" (
set "language=german_germany"
)
if "!value!" equ "2057" (
set "language=english_british"
)
if "!value!" equ "1033" (
set "language=english_american"
)
(endlocal
if "%~1" neq "" (set "%~1=!language!")
)
exit /b 0
)
)
exit /b 1
Do you see the Problem?
Thank´s a lot ;-)
Split by = then one more loop to remove CRLF
Example:
#echo off
for /f "tokens=2*delims==" %%i in ('wmic os get OSLanguage /value') do for %%f in (%%i) do (
if "%%~f" == "1031" set "Lang=german_germany"
if "%%~f" == "2057" set "Lang=english_british"
if "%%~f" == "1033" set "Lang=english_american"
)
echo %Lang%
Whilst not a direct answer to your question, I have decided to post this as yourself or future users may find it useful in a similar situation.
#For /F "Delims==" %%G In ("(Set $) 2>NUL") Do #Set "%%G="
#For /F "Tokens=1,* Delims=: " %%G In ('
%SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe -NoProfile -Command
"[CultureInfo]::InstalledUICulture | Select-Object"^
" LCID, KeyboardLayoutId, Name, DisplayName, ThreeLetterWindowsLanguageName"'
) Do #Set "$%%G=%%H"
#(Set $) 2>NUL
#Pause
The last two lines are included to hopefully display the names of any variables defined, and their values. The first line is to ensure that none of those are predefined before enumerating.

DOS Batch script to convert string 2 hex

How can I convert sring to hex in DOS Batch script?
For example, convert "abcd" to "61626364".
Since, 'a' is 0x61...
I tried to find a solution from web, a day, but could not find my answer.
#echo off
setlocal EnableDelayedExpansion
rem Store the string in chr.tmp file
set /P "=%~1" < NUL > chr.tmp
rem Create zero.tmp file with the same number of Ascii zero characters
for %%a in (chr.tmp) do fsutil file createnew zero.tmp %%~Za > NUL
rem Compare both files with FC /B and get the differences
set "hex="
for /F "skip=1 tokens=2" %%a in ('fc /B chr.tmp zero.tmp') do set "hex=!hex!%%a"
del chr.tmp zero.tmp
echo %hex%
Output example:
C:\> test.bat abcd
61626364
:stringToHex
#echo off
del tmp.hex >nul 2>nul
del tmp.str >nul 2>nul
if "%~1" equ "" (
echo no string passed
exit /b 1
)
echo|set /p=%~1 >tmp.str
::(echo(%~1)>tmp.str
rem certutil -dump tmp.str
certutil -encodehex tmp.str tmp.hex >nul
setlocal enableDelayedExpansion
set "hex_str="
for /f "usebackq tokens=2 delims= " %%A in ("tmp.hex") do (
set "line=%%A"
set hex_str=!hex_str!!line:~0,48!
set hex_str=!hex_str: =!
)
set hex_str=%hex_str:~0,-2%
echo !hex_str!
!!Mind that stackoverflow editor might corrupt tab character.tokens=2 delims= " there should be single TAB after delims.
requires the string passed as an argument.Take a look also at debnham's :hexDump function which you can use instead of certutil.

How to delete or skip multiline string in a file using batch files

Ex-
Input file-File1.txt contains-
multiline
String
Mango
Orange
String
Orange
then out put should be in File2.txt-
multiline,
Mango
#ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
SET "prevline="
(
FOR /f "delims=" %%a IN ('type q22913138.txt^|sort') DO (
IF DEFINED prevline IF "%%a"=="!prevline!" ECHO(%%a
SET "prevline=%%a"
)
)>tempfile.txt
type q22913138.txt|FINDSTR /v /i /x /g:tempfile.txt >newfile.txt
DEL tempfile.txt 2>nul
GOTO :EOF
I used a file named q22913138.txt containing your data for my testing.
Produces newfile.txt
I've made some minor adjustments.
The problem appears to be that your data file has no newline after the last Orange. Unfortunately, it's very difficult to see precisely what the format is on a text post.
for every word in file1 (count occurence of this word and if only one (write to file2)):
#echo off
for /f "delims=" %%i in ("file1.txt") do (
for /f "tokens=2 delims=:" %%n in ('find /c "%%i" "file1.txt"') do (
if %%n equ 1 echo %%i >>"file2.txt"
)
)
edit: ("Is there any optimize way to print out the string which exist more than one times in your file with the number of occurence- input file--file1.txt "
#echo off
echo.>file2.txt
for /f "delims=" %%i in (file1.txt) do (
for /f "tokens=2 delims=:" %%n in ('find /c "%%i" "file1.txt"') do (
if %%n equ 1 (
rem echo %%i >>"file2.txt"
) else (
find "%%i" "file2.txt">nul ||echo %%i occures %%n times >>"file2.txt"
)
)
)

run a script to rename several words in a txt file

I need your help.
I have a txt file in a directory (folder) and need to run a script to rename several words, eg.
where LX4XAB to LX4xab
and where is XS3X44 to Xs3x44
and another word
until the end save the file with another name.
Can you help me? Thank you.
Here you go:
#echo off
setlocal enabledelayedexpansion
(for /f "tokens=*" %%f in (input1.txt) do (
set "line=%%f"
set "line=!line:LX4XAB=LX4xab!"
set "line=!line:XS3X44=Xs3x44!"
echo(!line!
)) > newfile.txt
Revision 1
Here is how you can do it with multiple files and doing the naming the way you asked for.
#echo off
setlocal enabledelayedexpansion
cd /d C:\Temp
for %%a in (*.txt) do (
echo %%~nxa|Find /i "_new">nul
if errorlevel 1 (
(for /f "tokens=*" %%f in (%%a) do (
set "line=%%f"
set "line=!line:LX4XAB=LX4xab!"
set "line=!line:XS3X44=Xs3x44!"
echo(!line!
)) > %%~na_new.txt
)
echo %%~nxa|Find /i "_new">nul
if errorlevel 1 ren %%~nxa %%~na.old
)

Using DOS batch script: find a line in a properties file and replace text

Trying to replace a string in a properties file on a certain line by using a batch file. I know that this can be done WITHOUT the use of a temp file, as I have seen it before, but forgotten how to do it.
I know that if I have a var.properties file that contains this:
CLASSPATH=bsh.jar;other.jar
VARTEST=dummy
ANOTHERVAR=default
I am trying to update the CLASSPATH value in the .properties file without changing the order of the properties file.
This is a properties file and so I believe the answer would be related to using:
for /f "tokens=1,2* delims==" %%i in (var.properties) do (
#echo Key=%%i Val=%%j
)
Instead of findstr use find with the /v and /i switches on "classpath". This will OMIT returning the line with classpath in it, then you can echo what you want in the file along w/VARTEST=dummy
SET NEWVAL=CLASSPATH=test.jar
SET FILE=think.properties
FOR /F "USEBACKQ tokens=*" %%A IN (`TYPE "%FILE%" ^|FIND /V /I "classpath"`) DO (
ECHO CLASSPATH=%NEWVAL%>>"%FILE%"
ECHO %%A>>"%FILE%"
)
EDIT:
SETLOCAL ENABLEDELAYEDEXPANSION
SET NEWVAL=test.jar
SET OLDFILE=OLD_think.properties
SET NEWFILE=think.properties
SET COUNT=1
MOVE "%NEWFILE%" "%OLDFILE%"
FOR /F "USEBACKQ tokens=*" %%A IN (`TYPE "%OLDFILE%" ^|FIND /C /I "classpath"`) DO (
SET LINE=%%A
)
FOR /F "USEBACKQ tokens=*" %%A IN (`FIND /V "" ^<"%OLDFILE%"`) DO (
IF %COUNT% NEQ %LINE% (ECHO %%A>>"%NEWFILE%") ELSE (ECHO %NEWVAL%>>"%NEWFILE%")
SET /a COUNT=!COUNT!+1
)
Basically states,
rename think.properties to OLD_think.properties
read OLD_think.properties and find the line number with string
"classpath" in it and set it to variable LINE
Find all lines in OLD_think.properties, and echo them into think.properties. once you reach the line where your "CLASSPATH" string existed, it inserts the new line you wanted to replace it with, and everything else stays the same.
I finally broke down and accepted a method using a "temp" file. Using delayed expansion with the '!' character solved my question. Much of this success was due to input by mecaflash .
You can call this script with: CALL Script.bat PropKey NewPropValue Filename
#echo off
:: script for updating property files
SETLOCAL EnableExtensions
SETLOCAL EnableDelayedExpansion
if "%3"=="" (
ECHO Script will optionally accept 3 args: PropKey PropVal File
SET PROPKEY=UseCompression
SET PROPVAL=false
SET FILE=config.properties
) ELSE (
SET PROPKEY=%1
SET PROPVAL=%2
SET FILE=%3
)
FINDSTR /B %PROPKEY% %FILE% >nul
IF %ERRORLEVEL% EQU 1 GOTO nowork
MOVE /Y "%FILE%" "%FILE%.bak"
FOR /F "USEBACKQ tokens=*" %%A IN (`TYPE "%FILE%.bak" ^|FIND /N /I "%PROPKEY%"`) DO (
SET LINE=%%A
)
FOR /F "tokens=1,2* delims=]" %%S in ("%LINE%") DO SET LINE=%%S
SET /A LINE=%LINE:~1,6%
SET /A COUNT=1
FOR /F "USEBACKQ tokens=*" %%A IN (`FIND /V "" ^<"%FILE%.bak"`) DO (
IF "!COUNT!" NEQ "%LINE%" (
ECHO %%A>>"%FILE%"
) ELSE (
ECHO %PROPKEY%=%PROPVAL%>>"%FILE%"
ECHO Updated %FILE% with value %PROPKEY%=%PROPVAL%
)
SET /A COUNT+=1
)
GOTO end
:nowork
echo Didn't find matching string %PROPKEY% in %FILE%. No work to do.
pause
:end

Resources