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.

3
  • 1
    Can you please give a real example what exactly you want to do? For example cat file1 is just type file1; cat file1 file2 >> file3 is just type file1 file2 >> file3... Dec 17, 2014 at 8:23
  • ... oh yes, cat >> file in windows is copy con >> file Dec 17, 2014 at 8:31
  • Would a third party app be an acceptable option?
    – Journeyman Geek
    Dec 17, 2014 at 9:15

4 Answers 4

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.

5
  • 1
    x* "zero or more x" that works. thanks. findstr "^" seems to work too. but either way findstr will hang if the last line does not end with CRLF. I suspect this is an undocumented_feature of findstr
    – user313114
    Dec 17, 2014 at 20:26
  • 1
    find seems better behaved than findstr. (the unnofficial errata for findstr is truly impressive) except it's find /v "" that seems to do what I want. that's got to be a bug too. but atleast it diesn't hang. more > con works too - again undocumented, but probably not a bug.
    – user313114
    Dec 17, 2014 at 20:31
  • @user313114 You're right find/v "" is an even cleverer way I missed. Before posting I did test findstr with unterminated last line and it worked on (my) Vista -- but now you got me to retest on (my) Win7 and that hangs, so this may be not just an undocumented feature but an undocumented enhancement! Dec 19, 2014 at 4:36
  • Very clever; update as of Windows 10: findstr "^" no longer hangs without a trailing line break, and apparently even the latest Win7 version has been patched; find /v "" still works, but is certainly counter-intuitive.
    – mklement0
    Jul 29, 2016 at 22:00
  • Be careful both seem to have a hidden limit of columns. On Windows 8.1 the pipe may end (no error) if a line is more than 300 or 400 chars.
    – AqD
    Jul 5, 2018 at 13:03
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
3
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.

2
  • 3
    echo test | type - no doesn't seem to work if my question was unclear please improve it.
    – user313114
    Dec 17, 2014 at 7:40
  • 3
    Yes, type overlaps with cat in functionality, but unlike cat it cannot read from stdin, which is what the OP is after.
    – mklement0
    Jul 29, 2016 at 22:02
-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

2
  • no it;s nothing likr that at all, standard input not the xommand-line. I want tp pipe some stuff to it.
    – Jasen
    Dec 17, 2014 at 5:59
  • cat reads a file or streams's contents into the standard (like, a terminal window). echo just prints text into the standard output. The example you gave is using the command-line interpreter to send a program's output into a file.
    – int_541
    Dec 17, 2014 at 6:19

You must log in to answer this question.

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