Changing : with pipe(|) - windows

I have a lot of text files that I need to append the name of the file at the end. I have just DOS commands available. I managed to append the name at the beginning using:
findstr "^" *.txt >new.dat.
Unfortunately I cannot append to the end using findstr "$" *.txt new1.dat.
I cannot understand why the $ (which should be [line position : end of line') is not working.
The second thing I'm trying to replace : (colon character) inside the file with the | (pipe). I use the script:
#echo off setLocal EnableDelayedExpansion
for /f "tokens=* delims= : %%a in (new1.dat) do ( echo "%%a| >>new2.dat ).
The file looks like this:
batch-2016-03-14-08-50-05.txt:6530635|GB|150316|6530635.png
batch-2016-03-14-08-50-08.txt:6530636|GB|150316|6530636.png
I just want to replace the : with the pipe (|)

Related

Copy the line after the findstr result

I have a file with contents like this:
<key>CFBundleName</key>
<string>${PRODUCT_NAME}</string>
<key>CFBundlePackageType</key>
<string>APPL</string>
<key>CFBundleShortVersionString</key>
<string>8.10</string>
<key>CFBundleURLTypes</key>
To extract the line with CFBundleShortVersionString, I can use:
(for /f "delims=," %a in ('type "Info.pList" ^|findstr "CFBundleShortVersionString"') do #echo %a) > "CFBundleShortVersionString.txt"
gives output
<key>CFBundleShortVersionString</key>
But how do I extract the line below that please? i.e.
<string>8.10</string>
I am using the command prompt in Windows 10.
Thanks,
Chris.
If you only need those two lines let's just use this trick
You can paste this into the CLI:
SET "_Found="
#for /f "delims=," %a in ('
Type "Info.pList"
') do #(
IF NOT DEFINED _Found (
ECHO=%a|find "CFBundleShortVersionString" && (
SET "_Found=1"
)
) ELSE (
ECHO=%a
SET "_Found="
)
)>> "CFBundleShortVersionString.txt"
This will output every match and the first line after each match
You could first determine the number of the line containing the search string, then read the file and skip that number of lines, like shown in the following batch-file:
#echo off
setlocal EnableExtensions DisableDelayedExpansion
rem // Initialise variables:
set "LineNumber=" & set "LineString="
rem // Let `findstr` precede the matching line(s) with line number plus a colon:
for /F "delims=:" %%L in ('findstr /N "CFBundleShortVersionString" "Info.pList"') do (
rem // Fetch the line number (remove `if defined LineNumber` if you want the last match):
if not defined LineNumber set /A "LineNumber=%%L"
)
rem // Check whether the search string has been found:
if defined LineNumber (
rem // Read the file but skip lines up to the one containing the search string:
for /F "usebackq skip=%LineNumber% delims=" %%S in ("Info.pList") do (
rem // This captures the next (non-empty) line following the one containing the search string:
if not defined LineString set "LineString=%%S"
)
)
rem // Return result:
echo This is the extracted line: "%LineString%"
endlocal
exit /B
Here is one way to use a regex to get the next line. Select-String is not sed or awk, but it can do some things.
FOR /F "delims=" %%A IN ('powershell -NoLogo -NoProfile -Command ^
"(Get-Content -Path C:\src\t\Get-NextLine.txt -Raw |" ^
"Select-String -Pattern 'CFBundleShortVersionString.*\n(.*)\n').Matches.Groups[1].Value"') DO (
SET "NEXT_LINE=%%~A"
)
ECHO NEXT_LINE is "%NEXT_LINE%"
If the goal is to get 8.10 from the line, a little more work on the -Pattern could do that.
NB: If ECHO %NEXT_LINE% is used, the redirection characters <> will be interpreted. That is why the last output line uses QUOTATION MARK characters.

Extract the first quoted string of each line of a text file

I want to read through a text file with a lot of lines.
in the beginning of each line, i have a string between quotes, then a coma and then the rest of the lines, Ex.:
"CBL003","C3/C5 // <>SdcdUB","",0,1,"PfcdDT_gerergv","",0,"","",0,"","",0,"","",0,"","",0,"","",0,"","",0,"","",0,"","",0,"","",
"CBL004","C3<.<C7 // <>SqsxUB","",0,1,"PDzesdxT_esfdczec","",0,"","",0,"","",0,"","",0,"","",0,"","",0,"","",0,""
What I want is to read through the file, and extract the first line put it in a different text file and name it with the first strings of the line that it contains. Ex.:
In the above example, I should have the text file CBL003.txt that contains:
"CBL003","C3/C5 // <>SdcdUB","",0,1,"PfcdDT_gerergv","",0,"","",0,"","",0,"","",0,"","",0,"","",0,"","",0,"","",0,"","",0,"","",
and a second file text CBL004.txt that contains :
"CBL004","C3<.<C7 // <>SqsxUB","",0,1,"PDzesdxT_esfdczec","",0,"","",0,"","",0,"","",0,"","",0,"","",0,"","",0,""
I already have a code that read through each line :
FOR /F "tokens=*" %%a IN (C:\SourceFile.txt) DO (
ECHO %%a
)
But I don't know how to extract the first part of each line
You need tokens=1 because you want %%a to be set to only the first token, and you need delims=, to specify the comma as the delimiter. You can remove the quotes, if you want, by using %%~a. Type for /? for help.
FOR /F "tokens=1 delims=," %%a IN (C:\SourceFile.txt) DO (
ECHO %%~a
)

Replace a line in a text file if it matches a pattern

I'm trying to loop a specific file and see if any line contains a certain word or text and I want to replace the whole line. I am not sure how I am suppose to do it.
Right now this what I have:
#echo off
setlocal enabledelayedexpansion
FOR /F "usebackq delims=" %%G IN ("C:\folder\myfile.properties") DO (
Set Line="transaction.sic.lettreEnvironnementBackend"
IF %%G == %Line% (
replace that line with new text
)
)
pause
endlocal
#echo off
setlocal enabledelayedexpansion
Set "Line=transaction.sic.lettreEnvironnementBackend"
(
FOR /F "usebackq delims=" %%G IN ("C:\folder\myfile.properties") DO (
IF "%%G"=="%Line%" (
echo replacement line
) else (echo %%G)
)
)>replacement_filename
pause
endlocal
Note that the replacement filename should not be the source filename. Once tested, move the replacement file to the original filename if required.
Note also that the instruction will exactly match the contents of line - there's no allowance for any other characters on the line.
The syntax SET "var=value" (where value may be empty) is used to ensure that any stray trailing spaces are NOT included in the value assigned.
Also easy in PowerShell.
Get-Content .\OutputFile.txt |
% { if ($_ -eq 'Warning message') { 'NEW WARNING' } else { $_ } }

How to rename file with pattern using batch script

For example I have a .txt file named pair.txt.
Example:
AAA_BBB_CCC_DDD_EEE_FFF_GGG_HHH.idoc.xml AAAA
AAA_BBB_CCC_DDD_EEE_FFF_111_222.idoc.xml BBBB
AAA_BBB_CCC_DDD_EEE_FFF_333_444.idoc.xml CCCC
Now this file contains 2 columns of filenames. First column will be the pattern to rename the second column. Now I want to use the right side of 6th and 7th "_" as pattern. The final filenames of the files in the second column must be:
AAAA.GGG_HHH
BBBB.111_222
CCCC.333_444
As you noticed, I didn't include the .idoc.xml part. Now I want to put the code within this for statement:
for /f "tokens=1,2" %%a in ('type c:\user\pair.txt') do (
echo Renaming file : %%b
)
How will I able to do this?
for /f "usebackq tokens=1,2" %%a in ("c:\user\pair.txt") do (
for /f "tokens=7,8 delims=_." %%c in ("%%a") do (
echo Renaming file : %%b = %%b.%%c_%%d
)
)
Use a second for command to split the first column and then use the adecuated tokens

Batch Script to manipulate an existing textfile

I am totally new on writing batch script,busy with the tutorials with the example below I can learn a thing or two.I really need help to write a batch script to insert a line of text at the middle of existing text file.
For example given the file myfile.txt with the contents:
a
bcd
efg
hjiklmnop
q
rs
t
uvwxyz
The the command ./put-in-middle.sh "=== === ===" myfile.txt
should modify the file to:
a
bcd
efg
hjiklmnop
=== === ===
q
rs
t
uvwxyz
#echo off
rem Count the number of lines in the file with FIND
for /F %%a in ('find /C /V "" ^< %2') do set numLines=%%a
rem Get the number of middle line
set /A middle=numLines/2
rem Process all lines, use FINDSTR /N to insert line numbers
for /F "tokens=1* delims=:" %%a in ('findstr /N "^" %2') do (
rem Echo the original line
echo/%%b
rem If the line is the middle one...
if %%a equ %middle% (
rem Insert the new line
echo %~1
)
)
Create previous Batch file as put-in-middle.bat and execute it this way:
put-in-middle "=== === ===" myfile.txt
Notes:
Previous program does not check for errors, like missing parameters. This checking may be added, if you wish.
The slash in the command echo/%%b is inserted to avoid the message "ECHO is on" if the line is empty. If the line may contain the string "/?", then the command should be changed to echo(%%b to avoid that the echo help be displayed in this case (the left parentheses is the only character that do that).
If the file contains Batch special characters, like < > | & ), the echo/%%b command fail. In this case, a special processing of files lines must be added. The same point apply to the new inserted line.
Previous program just display in the screen the new file. If you want to replace the original file, the output must be redirected to an auxiliary file and replace the original one at end:
.
(for /F "tokens=1* delims=:" %%a in ('findstr /N "^" %2') do (
. . .
)) > auxiliar.txt
move /Y auxiliar.txt %2
Using sed and assuming even number of lines:
sed $(( $(wc input -l | cut -d' ' -f1) / 2))'a=== === ===' input
And this is the script version put-in-middle.sh:
line=$1
file=$2
sed $(( $(wc $file -l | cut -d' ' -f1) / 2))"a$line" $file

Resources