How to parse first word of each lines from a file using batch—file [closed]
I have a n number of text files in folder with below format
File1.txt
001@12345 Value1
002@12345 Value2
File2.txt
003@12345 Value3
004@12345 Value4
I need to get the sum of numbers before @ symbol. How do I get this using windows batch script?
windows batch-file cmd
closed as too broad by Gerhard Barnard, Stephan, aschipfl, Squashman, Compo Nov 28 '18 at 16:24
Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
add a comment |
I have a n number of text files in folder with below format
File1.txt
001@12345 Value1
002@12345 Value2
File2.txt
003@12345 Value3
004@12345 Value4
I need to get the sum of numbers before @ symbol. How do I get this using windows batch script?
windows batch-file cmd
closed as too broad by Gerhard Barnard, Stephan, aschipfl, Squashman, Compo Nov 28 '18 at 16:24
Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
Fine. And what is your specific question? Please learn How to Ask here! Remember that StackOverflow is not a free code/script writing service! So you have to try it on your own and come back here when you are stuck, providing a Minimal, Complete, and Verifiable example! Also you need to clarify whether you want the sum per file or over all files...
– aschipfl
Nov 28 '18 at 9:57
add a comment |
I have a n number of text files in folder with below format
File1.txt
001@12345 Value1
002@12345 Value2
File2.txt
003@12345 Value3
004@12345 Value4
I need to get the sum of numbers before @ symbol. How do I get this using windows batch script?
windows batch-file cmd
I have a n number of text files in folder with below format
File1.txt
001@12345 Value1
002@12345 Value2
File2.txt
003@12345 Value3
004@12345 Value4
I need to get the sum of numbers before @ symbol. How do I get this using windows batch script?
windows batch-file cmd
windows batch-file cmd
edited Nov 28 '18 at 11:32
Compo
16.8k3927
16.8k3927
asked Nov 28 '18 at 7:37
RajivRajiv
22217
22217
closed as too broad by Gerhard Barnard, Stephan, aschipfl, Squashman, Compo Nov 28 '18 at 16:24
Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
closed as too broad by Gerhard Barnard, Stephan, aschipfl, Squashman, Compo Nov 28 '18 at 16:24
Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
Fine. And what is your specific question? Please learn How to Ask here! Remember that StackOverflow is not a free code/script writing service! So you have to try it on your own and come back here when you are stuck, providing a Minimal, Complete, and Verifiable example! Also you need to clarify whether you want the sum per file or over all files...
– aschipfl
Nov 28 '18 at 9:57
add a comment |
Fine. And what is your specific question? Please learn How to Ask here! Remember that StackOverflow is not a free code/script writing service! So you have to try it on your own and come back here when you are stuck, providing a Minimal, Complete, and Verifiable example! Also you need to clarify whether you want the sum per file or over all files...
– aschipfl
Nov 28 '18 at 9:57
Fine. And what is your specific question? Please learn How to Ask here! Remember that StackOverflow is not a free code/script writing service! So you have to try it on your own and come back here when you are stuck, providing a Minimal, Complete, and Verifiable example! Also you need to clarify whether you want the sum per file or over all files...
– aschipfl
Nov 28 '18 at 9:57
Fine. And what is your specific question? Please learn How to Ask here! Remember that StackOverflow is not a free code/script writing service! So you have to try it on your own and come back here when you are stuck, providing a Minimal, Complete, and Verifiable example! Also you need to clarify whether you want the sum per file or over all files...
– aschipfl
Nov 28 '18 at 9:57
add a comment |
1 Answer
1
active
oldest
votes
type the files, find relevant lines, put a for /f
around to get the first part before the @
and sum them up:
@echo off
set sum=0
for /f "delims=@" %%a in ('type file*.txt ^|find "@"') do set /a sum+=1%%a-1000
echo %sum%
Note: there are some restrictions: in the current form, it works only with three-digit-numbers and there is a limit for the sum due to the INT32 numbers. (max. 2147483647, but reduced by the trick to overcome numbers treated as octal, when starting with 0
)
This will unfortunately only work with numbers up to7
because of the leading zeros which letsset /A
interpret them as octal ones; alsotype
outputs the file names at STDERR when using wildcards...
– aschipfl
Nov 28 '18 at 10:05
1
@aschipfl ah, yes, I overlooked that. - edited.
– Stephan
Nov 28 '18 at 10:13
1
To deal with the leading zeros you could also place anotherfor /F
loop inside of yours:for /F "delims=@" %%a in ('2^> nul type "file*.txt" ^| find "@"') do for /F "delims=0" %%b in (""%a") do set /A "SUM+=%%b"
– aschipfl
Nov 28 '18 at 11:10
1
@aschipfl, don't you needTOKENS=* delims=0
to strip the leading zeros?
– Squashman
Nov 28 '18 at 16:28
Oh yes, @Squashman, absolutely, sorry for missing that...
– aschipfl
Nov 28 '18 at 16:30
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
type the files, find relevant lines, put a for /f
around to get the first part before the @
and sum them up:
@echo off
set sum=0
for /f "delims=@" %%a in ('type file*.txt ^|find "@"') do set /a sum+=1%%a-1000
echo %sum%
Note: there are some restrictions: in the current form, it works only with three-digit-numbers and there is a limit for the sum due to the INT32 numbers. (max. 2147483647, but reduced by the trick to overcome numbers treated as octal, when starting with 0
)
This will unfortunately only work with numbers up to7
because of the leading zeros which letsset /A
interpret them as octal ones; alsotype
outputs the file names at STDERR when using wildcards...
– aschipfl
Nov 28 '18 at 10:05
1
@aschipfl ah, yes, I overlooked that. - edited.
– Stephan
Nov 28 '18 at 10:13
1
To deal with the leading zeros you could also place anotherfor /F
loop inside of yours:for /F "delims=@" %%a in ('2^> nul type "file*.txt" ^| find "@"') do for /F "delims=0" %%b in (""%a") do set /A "SUM+=%%b"
– aschipfl
Nov 28 '18 at 11:10
1
@aschipfl, don't you needTOKENS=* delims=0
to strip the leading zeros?
– Squashman
Nov 28 '18 at 16:28
Oh yes, @Squashman, absolutely, sorry for missing that...
– aschipfl
Nov 28 '18 at 16:30
add a comment |
type the files, find relevant lines, put a for /f
around to get the first part before the @
and sum them up:
@echo off
set sum=0
for /f "delims=@" %%a in ('type file*.txt ^|find "@"') do set /a sum+=1%%a-1000
echo %sum%
Note: there are some restrictions: in the current form, it works only with three-digit-numbers and there is a limit for the sum due to the INT32 numbers. (max. 2147483647, but reduced by the trick to overcome numbers treated as octal, when starting with 0
)
This will unfortunately only work with numbers up to7
because of the leading zeros which letsset /A
interpret them as octal ones; alsotype
outputs the file names at STDERR when using wildcards...
– aschipfl
Nov 28 '18 at 10:05
1
@aschipfl ah, yes, I overlooked that. - edited.
– Stephan
Nov 28 '18 at 10:13
1
To deal with the leading zeros you could also place anotherfor /F
loop inside of yours:for /F "delims=@" %%a in ('2^> nul type "file*.txt" ^| find "@"') do for /F "delims=0" %%b in (""%a") do set /A "SUM+=%%b"
– aschipfl
Nov 28 '18 at 11:10
1
@aschipfl, don't you needTOKENS=* delims=0
to strip the leading zeros?
– Squashman
Nov 28 '18 at 16:28
Oh yes, @Squashman, absolutely, sorry for missing that...
– aschipfl
Nov 28 '18 at 16:30
add a comment |
type the files, find relevant lines, put a for /f
around to get the first part before the @
and sum them up:
@echo off
set sum=0
for /f "delims=@" %%a in ('type file*.txt ^|find "@"') do set /a sum+=1%%a-1000
echo %sum%
Note: there are some restrictions: in the current form, it works only with three-digit-numbers and there is a limit for the sum due to the INT32 numbers. (max. 2147483647, but reduced by the trick to overcome numbers treated as octal, when starting with 0
)
type the files, find relevant lines, put a for /f
around to get the first part before the @
and sum them up:
@echo off
set sum=0
for /f "delims=@" %%a in ('type file*.txt ^|find "@"') do set /a sum+=1%%a-1000
echo %sum%
Note: there are some restrictions: in the current form, it works only with three-digit-numbers and there is a limit for the sum due to the INT32 numbers. (max. 2147483647, but reduced by the trick to overcome numbers treated as octal, when starting with 0
)
edited Nov 28 '18 at 10:28
answered Nov 28 '18 at 9:26
StephanStephan
35.9k43457
35.9k43457
This will unfortunately only work with numbers up to7
because of the leading zeros which letsset /A
interpret them as octal ones; alsotype
outputs the file names at STDERR when using wildcards...
– aschipfl
Nov 28 '18 at 10:05
1
@aschipfl ah, yes, I overlooked that. - edited.
– Stephan
Nov 28 '18 at 10:13
1
To deal with the leading zeros you could also place anotherfor /F
loop inside of yours:for /F "delims=@" %%a in ('2^> nul type "file*.txt" ^| find "@"') do for /F "delims=0" %%b in (""%a") do set /A "SUM+=%%b"
– aschipfl
Nov 28 '18 at 11:10
1
@aschipfl, don't you needTOKENS=* delims=0
to strip the leading zeros?
– Squashman
Nov 28 '18 at 16:28
Oh yes, @Squashman, absolutely, sorry for missing that...
– aschipfl
Nov 28 '18 at 16:30
add a comment |
This will unfortunately only work with numbers up to7
because of the leading zeros which letsset /A
interpret them as octal ones; alsotype
outputs the file names at STDERR when using wildcards...
– aschipfl
Nov 28 '18 at 10:05
1
@aschipfl ah, yes, I overlooked that. - edited.
– Stephan
Nov 28 '18 at 10:13
1
To deal with the leading zeros you could also place anotherfor /F
loop inside of yours:for /F "delims=@" %%a in ('2^> nul type "file*.txt" ^| find "@"') do for /F "delims=0" %%b in (""%a") do set /A "SUM+=%%b"
– aschipfl
Nov 28 '18 at 11:10
1
@aschipfl, don't you needTOKENS=* delims=0
to strip the leading zeros?
– Squashman
Nov 28 '18 at 16:28
Oh yes, @Squashman, absolutely, sorry for missing that...
– aschipfl
Nov 28 '18 at 16:30
This will unfortunately only work with numbers up to
7
because of the leading zeros which lets set /A
interpret them as octal ones; also type
outputs the file names at STDERR when using wildcards...– aschipfl
Nov 28 '18 at 10:05
This will unfortunately only work with numbers up to
7
because of the leading zeros which lets set /A
interpret them as octal ones; also type
outputs the file names at STDERR when using wildcards...– aschipfl
Nov 28 '18 at 10:05
1
1
@aschipfl ah, yes, I overlooked that. - edited.
– Stephan
Nov 28 '18 at 10:13
@aschipfl ah, yes, I overlooked that. - edited.
– Stephan
Nov 28 '18 at 10:13
1
1
To deal with the leading zeros you could also place another
for /F
loop inside of yours: for /F "delims=@" %%a in ('2^> nul type "file*.txt" ^| find "@"') do for /F "delims=0" %%b in (""%a") do set /A "SUM+=%%b"
– aschipfl
Nov 28 '18 at 11:10
To deal with the leading zeros you could also place another
for /F
loop inside of yours: for /F "delims=@" %%a in ('2^> nul type "file*.txt" ^| find "@"') do for /F "delims=0" %%b in (""%a") do set /A "SUM+=%%b"
– aschipfl
Nov 28 '18 at 11:10
1
1
@aschipfl, don't you need
TOKENS=* delims=0
to strip the leading zeros?– Squashman
Nov 28 '18 at 16:28
@aschipfl, don't you need
TOKENS=* delims=0
to strip the leading zeros?– Squashman
Nov 28 '18 at 16:28
Oh yes, @Squashman, absolutely, sorry for missing that...
– aschipfl
Nov 28 '18 at 16:30
Oh yes, @Squashman, absolutely, sorry for missing that...
– aschipfl
Nov 28 '18 at 16:30
add a comment |
Fine. And what is your specific question? Please learn How to Ask here! Remember that StackOverflow is not a free code/script writing service! So you have to try it on your own and come back here when you are stuck, providing a Minimal, Complete, and Verifiable example! Also you need to clarify whether you want the sum per file or over all files...
– aschipfl
Nov 28 '18 at 9:57