1

I have a text, for example:

cd123aaq54

and I'd like to separate only numbers in another file, so I could have

12354

in the output.

I have been trying a lot of commands like:

Get-Content text.txt | Select-String  -Pattern '[0-9]'

In linux it's much easier, just:

grep -o '[0-9][0-9]*' text >numbers

How can I do this in a Windows cmd shell?

5
  • There are a number of Windows versions of grep, though not all have the -o option.
    – AFH
    Apr 14, 2016 at 19:03
  • Where can I download a grep version that allow me to do it? I have tryied some of them. Apr 14, 2016 at 21:38
  • I down-loaded win-bash. I don't use bash.exe itself very much, but in a 5.6MB zip file, as well as bash, there are over 100 of the common Unix utilities, including sed, wget, find, xargs and of course grep, and this version supports -o. There is no installation: extract to a directory, add it to your PATH and then run as required. Or you can add selected utilities to an existing directory already in your path. They are all 32-bit implementations, which means they will run on any Windows system since 98. Sadly there is no man.
    – AFH
    Apr 14, 2016 at 23:49
  • @CarlosAlbertoLizanaHidalgo Added answer with PowerShell and Windows batch files solutions. Do you need more help? If this answer was helpful to you and answered your question, please don't forget to accept that answer
    – DavidPostill
    Apr 16, 2016 at 12:58
  • Get-Content is a powershell command, not cmd
    – phuclv
    Mar 21, 2020 at 11:42

1 Answer 1

0

How can I strip Alpha characters from an input file, leaving only numbers?

Below are two solutions:

  • PowerShell
  • Windows batch file

PowerShell solution

Use the following command:

Get-Content input.txt | ForEach-Object {$_ -Replace "[^0-9]", ""} > output.txt

Notes:

  • input.txt contains the text to be filtered.

  • output.txt contains the filtered text

  • All non numeric characters (not just alpha characters) will be removed.

Example:

PS F:\test> type .\input.txt
cd123aaq54
zyx456abc321
PS F:\test> Get-Content input.txt | ForEach-Object {$_ -Replace "[^0-9]", ""} > output.txt
PS F:\test> type .\output.txt
12354
456321
PS F:\test>

Windows batch file solution

Use the following batch file (RemoveAlpha.cmd):

@echo off
setlocal enabledelayedexpansion
set _alpha=abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ
rem read input file line by line
for /f "usebackq tokens=*" %%i in (`type %1`) do (
  rem strip alpha characters by using them as delimeters
  for /f "tokens=1,2 delims=%_alpha%" %%j in ("%%i") do (
    rem write stripped text to output file
    echo %%j%%k>>%2
    )  
  )
endlocal

Notes:

  • The above assumes there are no special characters (!@#$%^&*() ...) in the input file (they won't be removed).

  • The 'trick' is to use the string of Alpha characters as delimeters for the second for command.

Usage:

RemoveAlpha input output
  • input: Pathname for the input file containing the text to be filtered.

  • output: Pathname for the output file to contain the filter text copied.

Example:

F:\test>RemoveAlpha input.txt output.txt

F:\test>type input.txt
cd123aaq54
zyx456abc321

F:\test>type output.txt
12354
456321

Further Reading

2
  • How do you remove special characters?
    – Cestarian
    Dec 21, 2020 at 11:36
  • @Cestarian Please ask a new question regarding this
    – DavidPostill
    Dec 21, 2020 at 12:02

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .