1

I'm new to batch scripting and have a question. I was wondering if it's possible to search a .txt file by requirements and take data specified and copy into a new .txt file?

Like if I have 50 lines with 9 digit numbers and a bunch of other crap I don't need after them can I say, "For any line beginning with a 1,2,3,4,5,6,7,8,or 9...take the first 9 digits and copy them into a new file, for all lines in the file???"

I thought this would be easier than trying to delete all the other stuff. Let me know if you know anything about how to do this! Thanks.

Here's an example of what one line looks like:
123456789@example
and I just need to extract the 9 digit numbers from about 50 lines of this.

Patrick M
  • 10,547
  • 9
  • 68
  • 101
  • What OS are you working on? – alex Jul 18 '14 at 11:36
  • windows, and I'm using textpad – BillytheKid Jul 18 '14 at 11:41
  • I don't know the Windows command line environment in detail. But UNIX-like systems offer powerful command line text manipulation tools out of the box. Either switch to Linux or Mac or just use an easy to learn scripting language. [Python](http://www.python.org) would be a good start. It is multi-platform as well. – alex Jul 18 '14 at 11:45
  • Give real examples if you need real sample code. Otherwise `repl.bat` is a sed-like tool that uses native Windows scripting and has help by typing `repl /?` (by dbenham) - download from: https://www.dropbox.com/s/qidqwztmetbvklt/repl.bat – foxidrive Jul 18 '14 at 12:04
  • If you're new to batch scripting, why not use a language which you're familiar with? – Thomas Weller Jul 18 '14 at 12:34
  • @dg99 I disagree that the [tag:batch-file] tag is irrelevant. Alec clearly specifies 'batch scripting' in the question, doesn't mention any other Windows scripting language, and `.bat` file syntax is covered by this tag. – Patrick M Jul 18 '14 at 17:14
  • @PatrickM I didn't see in OP's question a reference to putting these commands into a file and running them, which is why I thought `windows-scripting` seemed more appropriate than `batch-file`. – dg99 Jul 18 '14 at 18:03
  • @dg99 You may be right, ultimately. `windows-scripting` doesn't have wiki, but I imagined it would include e.g. powershell in addition to `.bat` files. – Patrick M Jul 18 '14 at 18:56

3 Answers3

1
@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
(
FOR /f "delims=" %%a IN (q24824079.txt) DO (
 SET "line=%%a"
 REM set DIGITS to the first 9 characters of LINE
 SET "digits=9!line:~0,9!"
 FOR /L %%z IN (0,1,9) DO SET "digits=!digits:%%z=!"
 IF NOT DEFINED digits ECHO(!line:~0,9!
)
)>newfile.txt

GOTO :EOF

I used a file named q24824079.txt containing data for my testing. Produces newfile.txt

You did not specfy what to do if the line was all-digits but has fewer than 9 characters. I chose to report that line.

Magoo
  • 77,302
  • 8
  • 62
  • 84
1

You can use FINDSTR to filter out all lines that do not start with 9 digits. Then FOR /F can read the result, line by line. A variable is set, and a substring operation preserves just the first 9 digits.

@echo off
setlocal enableDelayedExpansion
(
  for /f %%A in (
    'findstr "^[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]" yourFile.txt'
  ) do (
    set "ln=%%A"
    echo !ln:~0,9!
  )
)>newFile.txt
dbenham
  • 127,446
  • 28
  • 251
  • 390
0

Hopefully this helps getting the job done:

@echo off
setlocal enabledelayedexpansion
for /f %%e in (emails.txt) do (
    echo Email: %%e
    for /f "delims=@ tokens=1" %%b in ("%%e") do (
        set BEGIN=%%b
        echo Name: !BEGIN!
        set FIRST=!BEGIN:~0,1!
        echo First char: !FIRST!
        set /a NUMERIC=!FIRST!+0
        echo Converted to number: !NUMERIC!
        if !FIRST!==!NUMERIC! echo Yippieh!
        echo.
    )
)

Instead of echo Yippieh! append the email (%%e) to a file, e.g. like

echo %%e >> output.txt
Thomas Weller
  • 55,411
  • 20
  • 125
  • 222