Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

What does following cat commnad in Unix mean? I am new to Unix

cat test.txt | java myfilegrammar.pcfg > test1.txt 2> test2.txt
share|improve this question

migrated from stackoverflow.com Mar 23 '10 at 4:14

This question came from our site for professional and enthusiast programmers.

    
It just makes test.txt's contents the standard-input to the java program -- but that has nothing to do with programming. –  Alex Martelli Mar 23 '10 at 3:59
add comment

7 Answers 7

up vote 3 down vote accepted

It pipes the contents of test.txt to the input of java myfile.java grammar.pcfg, and then redirects that java command's output stream to test1.txt and its error stream to test2.txt.

You might find it a little easier to understand if it is parenthesized, like this (note that this is for clarity and is not valid syntax):

(cat test.txt | java myfile.java grammar.pcfg) > test1.txt 2> test2.txt

This makes it more clear that the first text file is an input file, while the last two are output files.

(However, the java myfile.java bit doesn't really make sense to me -- java is called on .class files, not .java files.)

share|improve this answer
    
corrected the command Thanks. –  Harshit Mar 23 '10 at 4:20
add comment

Randal L. Schwartz would award your code a Useless use of cat award. For more information of how one earns this award, and how it can be avoided, see http://sial.org/howto/shell/useless-cat/. I'll just quote Randal L. Schwartz's form letter for posterity.

And of course, if you've been following along for a week or two, you know that this (BING!) is a Useless Use of Cat!

Rememeber, nearly all cases where you have:

    cat file | some_command and its args ...

you can rewrite it as:

    <file some_command and its args ...

and in some cases, such as this one, you can move the filename to the arglist as in:

    some_command and its args ... file

Just another Useless Use of Usenet,

(No, I'm no trying to be insulting; merely entertaining. If you follow the links, your question will be asnwered.)

share|improve this answer
add comment
  1. Dump the contents of test.txt into the standard input for a process launched using java as the executable with the arguments "myfile.java grammar.pcfg"
  2. Map the standard output of the java execution into test1.txt
  3. Map the error stream into test2.txt
share|improve this answer
add comment

Your java program reads the input from stdin and the input that you want to supply are in the file test.txt. The way to supply these inputs is to put the contents of the text.txt file to stdout and pipe it to the stdin of the java program.

The same can be done without using cat as:

java myfile.java grammar.pcfg > test1.txt 2> test2.txt < test.txt 
share|improve this answer
add comment

Each process in Linux starts with three file descriptors by default:

  • 0 -> Stdandard Input
  • 1 -> Standard Output
  • 2 -> Standard Error

If you see a redirection, i.e. fd_number > some_file.txt , it indicates that whatever would normally be printed to the file that holds file descriptor fd_number will now be redirected to whatever comes after it.

The command that you gave could also be written as:

cat test.txt | java myfile.java grammar.pcfg 1> test1.txt 2> test2.txt

Or:

java myfile.java grammar.pcfg 0< test.txt 1> test1.txt 2> test2.txt

... depending on how myfile.java handles standard input.

share|improve this answer
add comment

send the contents of test.txt to "myfile.java grammar.pcfg" via standard input. send the standard output of myfile.java grammar.pcfg to test1.txt send the standard error output of myfile.java grammar.pcfg to test2.txt

share|improve this answer
add comment

it is the same as

java myfile.java grammar.pcfg > test1.txt 2> test2.txt < test.txt

but with additional process (cat) that reads the data from test.txt and prints it to standard output (which is being redirected by the pipe | to the standard input of java)

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.