I wish to replace a string, (or append to one), in a text file.
There are many instances of that string in my file but I only wish to modify a single instance. To identify the line I know that it will always be found on the line below one which includes a unique string.
My thoughts are:
find line with unique string " cargo_mass:"
find line directly below it, containing non-unique string " base_color:"
replace line with txt " base_color: (1, 1, 1)"
Thought I might try Aacini's method for a little practice. See inline comments for explanation. In the future, please show the efforts you have put into solving the problem on your own.
#echo off & setlocal
set "txtfile=notes.txt"
set "search=cargo_mass:"
set "nextline=base_color: (1, 1, 1)"
setlocal enabledelayedexpansion
rem // find number of line containing search string
for /f "delims=:" %%I in ('findstr /n /i "!search!" ^<"!txtfile!"') do set /a "num=%%I"
if not defined num ( echo(!search! not found in !txtfile! & goto :EOF )
rem // read txtfile
< "!txtfile!" (
rem // from beginning to found line, output each line untouched
for /L %%I in (1,1,%num%) do (
set line=
set /P "line="
echo(!line!
)
rem // consume a line of input and output the replacement txt
set /P "="
echo(!nextline!
rem // output the rest of the file
findstr "^"
) > "~!txtfile!"
move /y "~!txtfile!" "!txtfile!"
Related
I have a list.txt file like this little sample:
banana.zip
xxx / yyy / zzz
orange.zip
kkklllmmm
lemon.zip
abc / def / ghi
apple.zip
xxx / yyy / zzz
pineaple.zip
kkklllmmm
I need to filter the .zip lines based on the line below.
So, if I choose the string kkklllmmm, the result needs to be:
orange.zip
pineaple.zip
I have started a batch script, but it's incomplete:
#echo off
set "input_list=list.txt"
set "string=kkklllmmm"
set "output_list=output.txt"
for /f "delims=:" %%# in ('findstr /n /c:"%string%" "%input_list%"') do (
set "line=%%#"
set /a "lineBefore=line-1"
for /f "delims=:" %%a in ("[CONTENT OF %lineBefore%]") do echo %%a>%output_list%)
I would appreciate a help to fix this code and make it work.
Thanks.
to explicate Squashman's suggestion:
#echo off
setlocal enabledelayedexpansion
set "input_list=list.txt"
set "string=kkklllmmm"
set "output_list=output.txt"
(for /f "delims=" %%# in (%input_list%) do (
REM if current line is your searchstring, then echo the previous line:
if "%%#"=="%string%" echo !line!
REM set the variable to the current line:
set "line=%%#"
)) > "%output_list%"
Note: I used empty delimiter to read the whole line. If you intentionally used : for some reason (aside "some character that isn't in the file to read the whole line"), just reimplement it.
Edit:
How did you get the previous line? by setting the line variable after comparing %%#(the current line), so with each turn of the loop at the end line is the current line, but on the next turn at the beginning, it will be the "previous line" (and become the "current line" only at the end of the turn)
I need to find more than one string: a bit more complicated: add a second for to process each search string. Note the syntax of the set "string=..." line. To also process strings with spaces, this syntax is mandatory. (if there were no spaces, it would simplify to set "string=string1 string2 string3"), but to process the spaces, each substring has to be quoted too:
set "string="string 1" "string 2" "string 3"")
#echo off
setlocal enabledelayedexpansion
set "input_list=list.txt"
set "string="kkklllmmm" "xxx / yyy / zzz""
set "output_list=output.txt"
(for /f "delims=:" %%# in (%input_list%) do (
for %%a in (%string%) do (
echo %%#|findstr /xc:%%a >nul && echo !line!
)
set "line=%%#"
)) > "%output_list%"
The outer for (%%#) gets one line at a time.
The inner for processes each substring in the list at a time.
The next line echoes the line and tries to find the substring (switches
x=compare the complete line,
c=search for the literal string (including the spaces, else it would try to find each word in the string, e.g. xxx, /, yyy etc.)
&& executes the command echo !line! only, if the previous command succeeded (findstr found a match)
You use a PowerShell command:
powershell -nop -c "sls List.txt -Patt 'kkklllmmm' -Co 1,0|%{$_.Context.PreContext}">output.txt
Or wrap it into a batch:
#echo off
setlocal enabledelayedexpansion
set "input_list=list.txt"
set "string=kkklllmmm"
set "output_list=output.txt"
(powershell -nop -c "sls '%input_List%' -Patt '%string%' -Co 1,0|%%{$_.Context.PreContext}"
)>"%output_list%"
Sample output
orange.zip
pineaple.zip
i currently have this command for a batch file
for /F "skip=1 tokens=1 delims=\n" %i in (stats.txt) do echo %i
with the contents of stats.txt being
Title = Subaru's Great Rehab Strategy
URL = http://dynasty-scans.com/chapters/subarus_great_rehab_strategy
Tags = Subaru x Tsukasa[|]Yuri[|]
No. of Pages = 3
^ NOTE: the final line is actually blank
the idea of the line of code is to return the 2nd line with URL. the end goal would be that i would run this line in some sort of loop going though a series of ~12000+ stats.txt files and collecting all the URL lines into a single file
but when i run the command i get this
as you can see it has skipped the first line but it's cutting off where the n in dynasty and outputting the last 3 lines.
now if i remove delims=\n i get the same 3 lines but i don't get the first word before the space which seems to indicate that the value of delims is what splits a line into "tokens" which then i just grab the first one (and space must be the default)
when i go into notepad++, open the Find and Replace Dialog, turn Search Mode to extended and look for "\r\n" i get taken to the end of each line which is why i chose delims to be \n assuming this would then make the entire line one token
So my question is How can i get all of the 2nd line only of my stats.txt file?
The for /f loop already treats the carriage return and / or line feed as an end-of-line. No need to specify it as a delimiter. With delims=\n you're actually saying that all literal backslashes and letter n's should be treated as token delimiters. If you want the whole line, what you want is "skip=1 delims=".
Just out of habit, when reading the contents of a file with a for /f loop, I find it useful to enable usebackq just in case the filename / path contains a space or ampersand. That allows you to quote the filename to protect against such potential treachery.
#echo off
setlocal
for /F "usebackq skip=1 delims=" %%I in ("stats.txt") do if not defined URL set "URL=%%~I"
echo %URL%
Put into context, to use this to read many files named stats.txt and output the URLs into a single collection, enclose the whole thing in another for loop and enable delayed expansion.
#echo off
setlocal
>URLs.txt (
for /R %%N in ("*stats.txt") do (
for /F "usebackq skip=1 delims=" %%I in ("%%~fN") do (
if not defined URL set "URL=%%~I"
)
setlocal enabledelayedexpansion
echo(!URL!
endlocal
set "URL="
)
)
echo Done. The results are in URLs.txt.
If you want to strip the "URL = " from the beginning of each line and keep only the address, you could try changing your for /F parameters to "usebackq skip=1 tokens=3" if all the files follow the same format of URLSpace=Spacehttp://etc.. If you can't depend on that, or if any of the URLs might contain unencoded spaces, you could also change echo(!URL! to echo(!URL:*http=http!
You don't need to use a FOR /F loop, you can also read it with a SET /P
setlocal EnableDelayedExpansion
< stats.txt (
set /p line1=
set /p URL_Line=
)
echo(!URL_Line!
Try this from the command line:
(for /F "tokens=1* delims=:" %i in ('findstr "URL" stats*.txt') do echo %j) > output.txt
the idea ... is to return the 2nd line with URL
If you want to insert this line in a Batch file, just double the percent signs.
Try this from the prompt:
(for /f "tokens=1*delims=]" %a in ('find /v /n "" *.csv^|findstr /l /b "[2]"') do #echo %b)>u:\r1.txt
Where - I used *.csv for testing (substitute your own filemask) and I used u:\r1.txt for the result - substitute as seems fit (but don't output to a file tat fits your selected filemask !)
It works by prefixing each line in each file with a bracketed number [n] (find - /n=and number /v lines that do not match "" - an empty string); then selecting those lines that /l - literally /b at the beginning of the line match "[2]".
The result is all of the second-lines of the files, preceded by the literal "[2]". All we need to do then is tokenise the result, first token up to delimiter "]" will be "[2" assgned to %%a and remainder-of line (token *) will be assigned to %%b
Have you tried
for /F "skip=1 tokens=1 delims=\n" %i in (stats.txt) do echo %i && goto :eof
I haven't tested it as I don't have access to a Windows machine at the moment, but that should exit the for-loop after the first iteration, which is what you want.
I am trying to print Line 4, Col 21-50 out of a text file, can this be simply done under Windows somehow? I've been trying to do this:
FOR /F "usebackq tokens=1 delims=-" %G IN (%COMPUTERNAME%.txt) DO ECHO %G
This is just working out terribly. Can't I just print a specific set of lines?
I need this script to be run on multiple computers, ideally I'd like to convert it to a variable for use with slmgr -ipk, maybe someone has a better suggestion?
Contents of text file (I want the XXXXX-XXXXX-XXXXX-XXXXX-XXXXX portion):
==================================================
Product Name : Windows 7 Professional
Product ID : 00371-OEM-9044632-95844
Product Key : XXXXX-XXXXX-XXXXX-XXXXX-XXXXX
Installation Folder : C:\Windows
Service Pack : Service Pack 1
Computer Name : LIBRA
Modified Time : 6/4/2015 7:26:54 PM
==================================================
if you want only the "Product Key" line you can try with
type %COMPUTERNAME%.txt|find /i "Product Key"
or
for /f "tokens=2 delims=:" %%# in (' type %COMPUTERNAME%.txt^|find /i "Product Key"') do echo %%#
For the task at hand, npocmaka's answer is the best suitable approach, as it does not insist on a fixed position of the string to extract from the file.
However, I want to provide a variant that sticks to a certain position.
The following code extracts the string placed at columns 21 to 50 in line 4 of file list.txt (the result is echoed (enclosed in "") and stored in variable LINE_TXT (without ""):
#echo off
for /F "tokens=1,* delims=:" %%L in (
'findstr /N /R ".*" "list.txt"'
) do (
if %%L equ 4 (
set "LINE_TXT=%%M"
goto :NEXT
)
)
:NEXT
if defined LINE_TXT set "LINE_TXT=%LINE_TXT:~20,29%"
echo."%LINE_TXT%"
The goto :NEXT command terminates the for /F loop at the given line; this is not mandatory but will improve performance for huge files (as long as the given line number is quite small).
To be more flexible, the following code can be used (define the string position in the initial set block):
#echo off
rem Define the string position here:
set FILE_TXT="list.txt"
set LINE_NUM=4
set COL_FROM=21
set COL_UPTO=50
setlocal EnableDelayedExpansion
set /A COL_UPTO-=COL_FROM
set /A COL_FROM-=1
for /F "tokens=1,* delims=:" %%L in (
'findstr /N /R ".*" %FILE_TXT%'
) do (
if %%L equ %LINE_NUM% (
set "LINE_TXT=%%M"
if defined LINE_TXT (
set "LINE_TXT=!LINE_TXT:~%COL_FROM%,%COL_UPTO%!"
)
goto :NEXT
)
)
:NEXT
endlocal & set "LINE_TXT=%LINE_TXT%"
echo."%LINE_TXT%"
Both of the above code snippets rely on the output of findstr /N /R ".*", which returns every line that matches the regular expression .*, meaning zero or more characters, which in turn is actually true for every line in the file; however, the switch /N defines to prefix each line with its line number, which I extract and compare with the originally defined one.
Here is another variant which uses for /F to directly loop through the content (lines) of the given text file, without using findstr:
#echo off
for /F "usebackq skip=3 eol== delims=" %%L in (
"list.txt"
) do (
set "LINE_TXT=%%L"
goto :NEXT
)
:NEXT
if defined LINE_TXT set "LINE_TXT=%LINE_TXT:~20,29%"
echo."%LINE_TXT%"
This method has got the better performance, because there is the skip option which skips parsing of and iterating through all lines (1 to 3) before the line of interest (4), opposed to the findstring variant.
However, there is one disadvantage:
for /F features an eol option which defines a character interpreted as line comment (and defaults to ;); there is no way to switch this option off as long as delims= defines no delimiters (last position in option string), which is mandatory here to return the line as is; so you have to find a character that does not appear as the first one in any line (I defined = here because your sample text file uses this as header/footer character only).
To extract a string from line 1, remove the skip option as skip=0 results in a syntax error.
Note that goto :NEXT is required here; otherwise, the last (non-empty) line of the file is extracted.
Although for /F does not iterate any empty lines in the file, this is no problem here as the skip option does not check the line content and skip over empty lines as well.
Finally, here is one more approach using more +3 where no text parsing is done. However, a temporary file is needed here to pass the text of the desired line to the variable LINE_TXT:
#echo off
set LINE_TXT=
more +3 "list.txt" > "list.tmp"
set /P LINE_TXT= < "list.tmp"
del /Q "list.tmp"
if defined LINE_TXT set "LINE_TXT=%LINE_TXT:~20,29%"
echo."%LINE_TXT%"
exit /B 0
This method avoids for /F and therefore the problem with the unwanted eol option as mentioned in the above solution. But this does not handle tabs correctly as more substitutes them with spaces (8 indent spaces as per default and configurable by the /Tn switch where n is the number of spaces).
I am not very good at batch scripting which is why I need help with a task as simple as this.
What I want to do is to scan a file, look for a line matching a specific pattern (need not be regexp) and when it is found, change it.
The line I'm looking for looks like this:
<ApplicationVersion>1.29.586.5771</ApplicationVersion>
And I want to change it to this:
<ApplicationVersion>1.31.633.6832</ApplicationVersion>
Of course, the numbers may be something else. Is there a nice way of doing this in batch without changing anything else in the file?
Install the Find And Replace Text command line utility and then you can simply enter..
fart yourfile.txt 1.29.586.5771 1.31.633.6832
This works for you and will also preserve the empty lines in your input file.....
#Echo OFF
REM Set These Variables
SET "InFile=InputFile.txt"
SET "OutFile=Outputfile.txt"
SET "Replace=1.29.586.5771"
SET "ReplaceWith=1.31.633.6832"
REM Get Total Lines Number [including empty lines]
FOR /F %%A IN ('TYPE "%InFile%"^|find /v /c ""') DO SET "Till=%%A"
REM Create The OutputFile with changes
SETLOCAL EnableDelayedExpansion
<"!InFile!" (
FOR /L %%a IN (1 1 0) DO SET /p "="
FOR /L %%A IN (1 1 %Till%) DO (
SET "line="
SET /P "line="
IF "!line!x" == "x" ( Echo.
) ELSE ( Echo !line:%Replace%=%ReplaceWith%!)
)
)>>"%OutFile%"
ENDLOCAL
I have a text file with the names of computer names and corresponding static i.p. addresses in the following format.
COMPUTER NAME:PC ADDRESS=154.100.1.1 MASK=255.255.254.0
COMPUTER NAME:PC2 ADDRESS=100.100.1.1 MASK=255.255.254.0
I would like to take the values from each line and put them as variables in a batch file for use later. Is this possible? The overall goal is to have the values from this easily edited text file to be used in netsh commands in another batch file.
I've looked around and found ways to take lines of a text file and place them in one variable using the snippet below. However, I do not know how to create multiple variables from one line. If someone could help me with this I'd greatly appreciate it!
#echo o
setlocal enabledelayedexpansion
set Counter=1
for /f %%x in (D:\COMP_T.txt) do (
set "comp!Counter!=%%x"
set /a Counter+=1
)
This should work:
#echo off
setlocal EnableDelayedExpansion
set "Count=1"
for /f "tokens=1,2,3,4,5,6,7 delims==: " %%A in (C:\File.txt) do (
set "%%A[!Count!]=%%C"
set "%%D[!Count!]=%%E"
set "%%F[!Count!]=%%G"
set /a "Count+=1"
)
:: Call other batch script here.
endlocal
Example Output:
COMPUTER[1]=PC
COMPUTER[2]=PC2
ADDRESS[1]=154.100.1.1
ADDRESS[2]=100.100.1.1
MASK[1]=255.255.254.0
MASK[2]=255.255.254.0
Here is a solution that avoids the need for delayed expansion. It uses FINDSTR to insert a line number followed by : at the beginning of each line. The search string of "^" is guaranteed to match every line in the file.
The only other issue is to set TOKENS and DELIMS to parse the line properly.
#echo off
setlocal
for /f "tokens=1,4,6,8 delims=:= " %%A in ('findstr /n "^" "d:\comp_t.txt"') do (
set "comp%%A=%%B"
set "addr%%A=%%C"
set "mask%%A=%%D"
set "counter=%%A"
)
To use the set of variables in another batch file, line by line, just parse the lines as done in other answers here, and call the other batch file with the metavariables.
#echo off
for /f "tokens=1,2,3,4,5,6,7 delims==: " %%a in ('type "File.txt" ') do (
echo "computer_name=%%c"
echo "address=%%e"
echo "mask=%%g"
Call "batch script" "%%c" "%%e" "%%g"
)