Using Redirection & Pipes in WSL

Using redirection and pipes in WSL

In WSL, many commands can read their input either from a file or from a previous command in a pipeline of commands. There are four that control the flow of data between commands or files.

  • The bar character, "|", between two commands says send the output to the next command.

  • The less than character, "<" says get the input for a command from the file following the <

  • The greater than character, ">" says send your output to a file.

  • Two greater than characters, ">>" says add your output to the end of a file.


Some additional things to note:

  • If there's no ">" or ">>" the output of the last command goes to the screen.

  • Error and status messages are normally sent to the screen.

  • In documentation etc., the input, output and error/status message streams are named STDIN, STDOUT and STDERR.

  • If you have command1 | command2 then STDOUT for command1 becomes STDIN for command2.


There's an example of piping in the instructions to enumerate the markers in a file. Let's look at it in detail:

  • cat filename.db | sed -e "s/ .*$//" | sort | uniq -c > sfm-list.txt


There's 4 commands in the pipeline:

  • cat gets a filename(s) on the command line and sends the contents to STDOUT.

  • cat filename.db | sed -e "s/ .*$//"

  • is the same as:

  • sed -e "s/ .*$//" < filename.db

  • sed -e does some regular expression commands on STDIN and then passes the result to STDOUT

  • This pipe does the sed command s/from/to/

  • s/from/to/ takes text that matches from and changes it into to.

  • the to can be empty, in which case from is deleted.

  • The effect here is to delete everything following the first space, i.e., everything but the leading SFM.

  • That converts each line into just its SFM.

  • sort does what it says; it sorts STDIN and then passes the result to STDOUT

  • this puts all the same SFMs together.

  • uniq reads STDIN and ignores repeats. then passes the result to STDOUT

        • The -c option adds a count of how many times a line was repeated.

    • The > sfm-list.txt at the end store the sorted, counted list of SFM marker into sfm-list.txt