I have a text file that looks something like this:
P4657_1
P1352_1
P3126_1
and so on.
I need a batch script that removes the "P" at the beginning and the "_1" at the end.
I found many script examples in this forum but none worked for my needs.
Can anyone help?
sed -E 's/^P//g; s/_1$//g' yourinfile > outfile
by virtue of the first substitution beginning P's get nuked, by the second final _1's get nuked and you are done.
For a list of options for modifying text files, see How can you find and replace text in a file using the Windows command-line environment?.
My favorite option is a hybrid JScript/batch utility I wrote called REPL.BAT. The script does not require installation of any 3rd party executables, and it will run on any modern version of Windows, starting with XP.
Using REPL.BAT, the solution is as simple as:
type "yourFile.txt" | repl "^P(.*)_1$" "$1" >yourFile.txt.new
move /y "yourFile.txt.new" "yourFile.txt" >nul
Thanks to everyone.
I did find a solution elsewhere: http://www.dostips.com/forum/viewtopic.php?f=3&t=1498
the code is very simple:
set "search=P"
set "replace="
for /F "delims=" %%a in (input.txt) DO (
set line=%%a
setlocal EnableDelayedExpansion
>> output2.txt echo(!line:%search%=%replace%!
endlocal
)
You can use echo apparently. So, print your file with type, and for each line, assign the line to a variable, edit the variable, and then echo the variable.
Related
I am totally new to batch scripting for cmd (Windows).
I have installed tesseract to work as a command line OCR tool.
Now I would like to run OCR on 100 images that I have stored in a folder.
How can I do it with batch ?
The command to run tesseract on an image and return the OCR text in a text file is:
"C:\OCR\tesseract" "C:\Image_to_OCR.jpg" "C:\out"
More information: http://chillyfacts.com/convert-image-to-text-using-cmd-prompt/
As you can see, I would probably need to make a for loop whith automatically iterates through the number of pictures and changes the name of the picture in the command accordingly and of course also the output name of the text file... but I don't know how to do it.
Any help would be very appreciated !
EDIT:
As suggested in the answer by Stephan, I could write:
for %%A in (C:\*.jpg) do "C:\OCR\tesseract.exe" "%%~fA" "C:\out"
However, the command line (cmd) only apears quickly and closes imidiatley and nothing happens.
My files are not directly in C:\ but in "C:\Users\James\Desktop\", therefore I wrote the command as such:
for %%A in (C:\Users\James\Desktop\*.jpg) do "C:\OCR\tesseract.exe" "%%~fA" "C:\out"
...but as said before, it does not work somehow.
Also, can I change the output txt name to be the same as the input image name, like so ?
for %%A in (C:\Users\James\Desktop\*.jpg) do "C:\OCR\tesseract.exe" "%%~fA" "%%~fA"
This worked :
I got two great answers! Thanks a lot. The final thing that worked was a mix between both answers:
#Echo off
PushD C:\Program Files (x86)\Tesseract-OCR || (Echo couldn't pushd C:\OCR & Exit /B 1)
for %%A in ("C:\Users\EPFL\Google Drive\EDx PDF Maker\Cellular Mechanisms of Brain Functions\Slides\1\*.jpg") do tesseract.exe "%%~fA" "%%~dpnxA"
I don't know your program C:\OCR\tesseract.exe but I assume it needs supporting tools/files present in the C:\OCR folder, so either you have to set that folder as the current one or have it contained in your path variable.
#Echo off
PushD "C:\OCR" || (Echo couldn't pushd C:\OCR & Exit /B 1)
for %%A in ("C:\Users\James\Desktop\*.jpg") do tesseract.exe "%%~fA" "%%~dpnA.txt"
The "%%~dpnA.txt" will save the text with same drive/path/filename and extension .txt
Use a for loop to iterate over the files:
for %%A in (C:\*.jpg) do "C:\OCR\tesseract.exe" "%%~fA" "C:\out"
%%A is the filenames (one at each run of the loop),
%%~fA is the fully qualified filename (just to be sure).
Read the output of for /? to learn more about those modifiers.
Note: this is batchfile syntax. To use it directly on command line, replace every %% with a single %
If I have a directory with 2 files in it like these:
123456789_File1.pdf
123456789_File2.pdf
I need to run a batch file that lets me read the name of the file only and not it's path. Create another file with the same name and a different extension with a contents that is only the first 9 characters of the name. Below is what I have tried and I cannot figure out why this is not correct? I have done several hours of research over something that seems like it should be simple and I feel like I am just missing something.
FOR /R C:\Test\Files %%F in (*.*) do (set content=%%~nF & echo %content:~0,9%>> %%~nF.txt)
This works for me:
#echo off
Setlocal EnableDelayedExpansion
FOR /R . %%F in (*.*) do (
set content=%%~nF
echo !content:~0,9! %%~nF.txt
)
For future reference, please note that "this is not correct" is not an accurate description of the problem as far as StackOverflow questions and answers are concerned. The precise description of your problem is that in each iteration of the loop, %content% appears to contain the exact same content as it had in the first iteration of the loop, regardless of the value of %%F.
The solution is to enable "delayed expansion" and to use the special ! delimiter instead of % so as to enjoy the benefits of delayed expansion.
How to delete the first line of a file using Batch?
I really need help... I've tried everything but it didn't work. :(
I've tried this :
#echo off
Type test.txt | findstr /I /V /C:"So, lets remove this line" >>Test2.txt
exit
Some tasks are clumsy with pure batch techniques (and mangles characters) - this is a robust solution and uses a helper batch file called findrepl.bat that uses built in jscript-ing from - http://www.dostips.com/forum/viewtopic.php?f=3&t=4697
Place findrepl.bat in the same folder as the batch file.
type "file.txt"|findrepl /v /o:1:1 >"newfile.txt"
Another method is to use more.exe but it has limitations with the number of lines at 64K and it mangles TAB characters.
more +2 <file.txt >newfile.txt
(echo off
for /f "skip=1 tokens=1* delims=" %A in (c:\windows\win.ini) do echo %A
echo on)
You'll need to loop through your file line by line and output each line, except the first one.
From your example it looks like you might be looking for a specific string.
try this question/answer .. it might help you get you on your way.
Okay, so basically, I have a whole list of links in a plain text Notepad file, each link on a seperate line. All I am wanting to do is to add a bit of text before each link, specifically: 127.0.0.1 and a couple of spaces.
So this...
somelink.com
becomes this...
127.0.0.1 somelink.com
By now you've probably already guessed that I am trying to edit the contents of a text file to make it useable as a HOSTS file in Windows.
So I am wanting some batch file code, executable in a .bat file, which basically reads a Notepad text file, and then add "127.0.0.1 " at the beginning of each line with text on it. I am guessing this is probably a very simple piece of code for someone with some knowledge of MS DOS and batch file code, but that most certainly isn't me, and the only batch files I have ever written have been with help like now.
Thanks for any and all help in advance with this, it really is much appreciated.
read HELP FOR and then try this in a command prompt
FOR /F "delims=" %a in (input.txt) do #echo 127.0.0.1 %a >>output.txt
here is some explanation and some considerations to extend it with a little more complete functionality and put it in a BAT file
FOR is the command to iterate over the lines of your input text file. Read microsoft documentation at http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/for.mspx
input.txt is the text file that contains your list of domain names, it must reside in the current directory
output.txt will be the result file that will contain the list of domain names prefixed with 127.0.0.1, it will be created in the current directory
If you want to create a BAT file, you need to move the FOR command and edit it a little bit, changing the %a loop variable names to be %%a.
You can then place the BAT file either in the current directory, where your input resided and where the output will be created.
Alternativelly, you may place your BAT file, elsewhere. In that case, you need to invoke it with its full path.
Or you may even place it in a special directory (I have my own C:\Program Files\CMD) and add it in the PATH system variable. See here www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/path.mspx?mfr=true how you can change your PATH for your current session. And here ss64.com/nt/path.html you may find some explanation on how to make the PATH change permanent.
Also, you might be tempted to add some flexibility to your BAT file, instead of having constant input.txt and output.txt filename, replace them with %1 and %2 which represent the first and second command line parameters of your BAT file.
the user might then want to use files which contain blanks in their filenames. They might specify them surrounding the names with quotes ". In that case, you need to add some incantation usebackq in the FOR command for it to not break havoc when the user uses quotes.
Finally, you will need to decide what to do in case the output text file already exists, you migh want to consider prevent overwriting.
So putting all this pieces together, here is a short BAT file to get you started...
#echo off
if .%2==. goto help
if not exist %1 goto helpno1
if exist %2 goto helpalready2
FOR /F "usebackq delims=" %%a in (%1) do #echo 127.0.0.1 %%a >>%2
goto :eof
:help
echo you need to specify input and output text files
goto :eof
:helpno1
echo %1 not found
goto :eof
:helpalready2
echo %2 already exist
goto :eof
welcome to BAT programming and enjoy!
here we go!
(
Set /p line1=
Set /p line2=
Set /p line3=
Set /p line4=
)<Filename.txt
echo 127.0.0.1 %line1%>Filename.txt
echo 127.0.0.1 %line2%>>Filename.txt
echo 127.0.0.1 %line3%>>Filename.txt
echo 127.0.0.1 %line4%>>Filename.txt
This will Read the first four lines of the text file, and then put in your stuff and each line back into the line it came from.
Have Fun!
In addition to PA.'s answer, if you require a specific number of spaces, you can throw them into a variable and add it to the command as well.
SET spaces= # to the left is 10 spaces
FOR /F "delims=" %a in (input.txt) do #echo 127.0.0.1%spaces%%a>>output.txt
So the output will be
127.0.0.1 somelink.com
Batch-File flavor:
SET spaces= # to the left is 10 spaces
FOR /F "delims=" %%a in (input.txt) do #echo 127.0.0.1%spaces%%%a>>output.txt
I want to dir a folder and get in a variable all the names of the *.csv files.
Does anyone know how can I do this?
Using Bash you can try:
dirlist=`ls -1 *.csv`
echo "$dirlist"
Under windows you can read the following Q and A:
Windows batch files: How to set a variable with the result of a command?
Something like:
for /f "delims=" %%a in ('dir') do #set foobar=%%a
Change 'dir' to what you want. I have no windows machine close by, so cannot test.
I apologize for a duplicate response, but StackOverflow has reputation counters that impede me from certain actions.
When executing directly on the command-prompt, use the following form:
for /f "delims=" %a in ('whoami') do #set myaccount=%a
The form in previous answer is intended when used in a .bat or .cmd file.