20

Under cmd is there a windows equivalent to the posix command cat ?

cat all by itself no filenames, no switches. I just want something that copies stdin to stdout until it hits EOF.

it's not a hard program to write, but is one provided by windows?

the answer is not necessarily a single word and is definately not type.

echo test | type

just gets me an error message

echo test | type con 

hangs wainting for me to type some stuff

echo test | more

is close, but "more"` halts after one screenfull.

The reason I want this is for running a program that behaves differently when stdout is into a pipe. so I can see the output.

4 Answers4

14

someprog | findstr x* or any other single character followed by asterisk copies all lines from the pipe stdin to stdout. Specifically, it copies each line if it either does contain an x or doesn't, which you can easily see is always true. Windows findstr is roughly equivalent to Unix grep but with annoying minor differences in the syntax.

findstr is intended for text and I'm not sure (and didn't test) if it works for binary data with few or no [CR]LFs and therefore very long apparent lines. Unix cat with no args does work for binary, but your stated use case is programs that alter their output for pipes and in my experience that only happens on text output -- and usually is not pipes as such but rather NON-tty/NON-console/etc and therefore I can test equally well with someprog >temp; cat temp on Unix or & type on Windows unless the program is interactive and I need to see one output before entering the next input.

4

TLDR: findstr "^" > STDIN.txt as the first line in your .bat will neatly capture all piped input for later use.

I discovered that the first line/command/program in a batch file gets STDIN so I took the following approach:

  1. Capture STDIN to a file STDIN.txt
  2. Code your .bat logic
  3. Use TYPE STDIN.txt to access or pipe your STDIN on to further programs

Example:

fawk.bat:

@ECHO OFF
:: Capture STDIN initially as the first line of our .bat
FINDSTR "^" > STDIN.txt

:: Do some other stuff here

:: Pipe STDIN on to our program here (e.g. find all .txt files)
TYPE STDIN.txt | AWK /\.txt$/

:: Cleanup
DEL /Q STDIN.txt

Usage:

C:>DIR /b | fawk.bat
a.txt
file.txt
README.txt
Marc
  • 413
2

The closest Windows equivalent to *nix's cat is the TYPE command. Note that on *nix systems, cat is actually a separate program whereas Windows' TYPE is actually part of the command-line interpreter.

int_541
  • 320
-2

ECHO Should work similar to CAT.

Echo text into a FILE

The general syntax is

Echo This is some Text > FileName.txt

or if you want to avoid extra spaces: Echo Some more text>FileName.txt