2

was wandering if there is a way to extract a line of text which contains certain value from a .txt file using CMD. For example using "type filepath\example.txt" will open the whole file and in my case I am working trough a software which doesn't allow CTRL+F option and .txt files are massive. Is there any way I could specify certain word in order to get that whole line of text or to open only .txt files which have that word within them. For example I have 1000 .txt files and all of them contain the same text but only one of them has a word "EXAMPLE" in it. Could I use some command to find and open that file using CMD.

Thank you.

milke 92
  • 39
  • 1
  • 5

4 Answers4

1

I have 1000 .txt files and all of them contain the same text but only one of them has a word "EXAMPLE" in it. Could I use some command to find and open that file using CMD.

a) find the file(s) that have the desired string:

findstr /m "EXAMPLE" *.txt

b) find the file (assuming, only one contains the string; else it will open all matching files) and open the file in notepad:

for /f "delims=" %a in ('findstr /m "EXAMPLE" *.txt') do "%a"

c) find the one line that has the string:

findstr "EXAMPLE" *.txt 
Stephan
  • 53,940
  • 10
  • 58
  • 91
  • Thank you, "findstr "EXAMPLE" *.txt " is what I basically need but there is a problem. Let's say all .txt files contain text like "Number one", "Number two" etc... when I use "Number two" in this command it shows me all the files which contain "Number".. why doesn't it treat the value as a whole since it is in quotes? – milke 92 Jan 23 '22 at 21:17
  • 1
    Open a Command Prompt window, type `findstr /?`, press the `[ENTER]` key, and learn how the command you are using works. This site isn't here just so that you can depend upon others to do everything for you @milke92. _You should also learn that, as your question stipulated 'the word "EXAMPLE"', the answer above does not isolate a word, (only a substring)_. – Compo Jan 23 '22 at 21:23
  • Thank you so much @Compo This helped me find everything I need. – milke 92 Jan 23 '22 at 21:39
0

The findstr.exe solution from Stephen would be less typing. But, using Select-String allows the use of a much more complete implementation of regex. All supported Windows systems have PowerShell available. This works in a batch-file run by cmd on windows and at the command prompt. Given that this would likely be put into a batch-file, the amount of typing is not very significant.

@powershell.exe -NoLogo -NoProfile -Command ^
    Get-ChildItem -Path '.\*.txt' ^| ^
        ForEach-Object { Select-String -Pattern 'example' -Path $_ } ^| ^
        Select-Object -Property Filename,Line
lit
  • 14,456
  • 10
  • 65
  • 119
0

Not the most elegant alternative, I agree, and I am certain someone will tell me how it could be bettered. The key is it uses basic find a string (you can set as I have to be case insensitive) in a file then reports filename and line number, Line [9] in this case.

enter image description here

@forfiles /m *.txt /C "cmd /c (find /n /i \"For example\" "@file"1>nul) &&if %errorlevel%==0 (find /n /i \"For example\" "@file"2>nul)"

When used in a batch file it could look something like this, but see caveats below

Finder$.cmd

@echo off & Title Find wally String in a file
set "string=where's Wally"
if not "%1"=="" set "string=%~*"

forfiles /m *.txt /C "cmd /c (find /n /i \"%string%\" "@file"1>nul) &&if %errorlevel%==0 (find /n /i \"%string%\" "@file")"

echo/ & pause & exit /b

enter image description here

It is not perfect but note its not case sensitive (using /i), it can readily fail if *.txt files are not plain text and as written will only accept a short unquoted string of up to 9 words ( avoid " or other punctuation). It works in local directory with *.txt, but you could alter those as require to first say cd /d f:\mylogs and search *.log files.

Finally you asked to open the file thus we can simplify for that task to call an editor like notepad or with some fetteling one that accepts line numbers (but that is another question)

forfiles /m *.txt /C "cmd /c (find /n /i \"%string%\" "@file"1>nul) &&if %errorlevel%==0 (notepad.exe "@file")"

enter image description here

K J
  • 8,045
  • 3
  • 14
  • 36
-1

Everything below is related to unix bash terminal, so please install Linux Subsystems if you are using Windows. Or even Linux itself :)

  1. You can open specific file in a text editor like Vim or Nano in terminal. They offer the "CTRL+F" function.

Below you can see me searching for the "gameId" keyword in the file game_stats.js, which I opened via $ vim game_stats.js. enter image description here

P.S. To quite vim you need to type :q :)

  1. Use grep command as advised in this answer https://stackoverflow.com/a/16957078/13212398

Example for the command that recursively searches for any .js or .txt files that contains "let" in the current directory (.).

$ grep  --include=\*.{js,txt} -rnw -e "let" .

./this_test.js:18:let bob = new Person('Bob');
./this_test.js:19:let bill = new Person('Bill', bob);
./TUD-2Q-WDB/checkers-in-delft/public/javascripts/game_state.js:99:        let pieces = []

AlexFreik
  • 64
  • 1
  • 3