Translate

Friday 31 January 2014

INPUTS, OUTPUTS AND WILDCARDS



Unix commands generally get their information from the screen, and output to it. There are three main 'streams' which unix uses to get/place it's information on. These streams are called:
  • stdin (Standard Input) - normally, what you type into the screen
  • stdout (Standard Output) - normally, what is output to the screen
  • stderr (Standard Error) - normally, error messages which go to the screen

any of these may be redirected by the following symbols:
  • < <filename> take input from <filename> rather than the screen. e.g.
    $ ksh < x # will read all commands from the file x and execute them using the Korn shell.
  • > <filename> take output from the command and place it in <filename>. e.g.
    $ ls > x will place the output of the command 'ls' in the file x
  • >> <filename> take output from the command and append it to <filename>. e.g.
    $ ls /tmp >> x will place the output of the command 'ls' and append it to the file x
  • 2> <filename> take any error messages from the command and put it in <filename>. e.g.
    $ ls /tmp 2>/dev/null would throw away any error messages that are produced by ls (sorry, /dev/null is a file that, if written to, the information disappears never to be seen again).
  • command1 | command2 Pipe - Takes the standard output of the first command, and turns it into the standard input of the second command. The output of the second command will then be put on the standard output (which, again, may be a pipe) e.g.
    $ ls | more will send the output of 'ls' into the command 'more', thus producing a directory listing which stops after every page. This method is called piping.

command1 & - the ampersand (&) forces command1 to run in the background. so that you may continue to type other commands in the shell, while command1 executes. It is not advisable to run a command in the background if it outputs to the screen, or takes it's input from the screen

See also tee which allows splitting of the input stream and output to several different places at once.

Wildcards

B Bib Baby Fox Fib

There are various wildcards which you may use. One is '*' which means 0 or more characters. e.g. 'B*' will match 'B,Bib and Baby' from the list above, another wildcard is '?' which matches 1 character, e.g. '?ib' will match 'Bib and Fib'. Wildcards differ depending on the program in use: awk derivatives (awk,sed,grep,ex,vi,expr and others) have the following special characters:
  • ^ beginning of the line
  • $ end of the line
  • . any character
  • * one or more of the preceding character
  • .* any number of characters
  • \n Carriage return
  • \t Tab character
  • \<char> Treat <char> as is (so, \$ would try to match a '$')
Given the following four lines:

Chargeable calls in bundle: $47.50
Chargeable calls out of bundle: $20.50
Other bundle charges: $0.00
Total Charge: $20.50

$ grep "^Charg.*bundle.*\$.*"
would match the first two lines.
In english - match all lines which start with 'Charg', then have any number of characters and then the word 'bundle', then have any number of characters, and then a dollar symbol, and then have any number of characters following to the end of the line

No comments:

Post a Comment