<<

I/O - reading

 To redirect input for a program or , < file n < file n is the file CS 50011: Introduction to Systems II descriptor Lecture 2: *nix << file n << file n is the file descriptor

Prof. Jeff Turkstra  Example: [email protected] < my_document

© 2017 Dr. Jeffrey A. Turkstra 1 2

I/O redirection - writing I/O redirection - pipes

 We can redirect the output from a  Pipes enable a series of programs to work program or command too! together command_1 | command_2 | … | command_n > file and n > file Redirect output to file  Functions a lot like > except stdout from >> file and n >> file Appends output to file command_n-1 is redirected to stdin of command_n. >| file and n >| file Overrides the noclobber option, if set  Example: $ -l | -l >& number Redirects the output to file 46 descriptor number counts how many lines of text ls just output

3 4

command command

 Check out the tee command…  Used to search files for lines of information. any_command | tee save_out Many, many flags - see the . grep -flags regular_expression filename  Saves a of all output (sent to  Useful flags… save_out standard out) in the file -x Exact match of line tee save_in | any_command -i Ignore upper/lower case  Saves a copy of all input (sent to - Only count the number of lines standard in) in the filesave_in match -n Add relative line numbers -b Add block numbers - Output all lines which do not match

5 6

Simple regular Examples expressions  Regular expressions express patterns. They are  grep "^string$" file_name used to and/or extract pieces of information collects all lines which contain only string from a string.  grep " … " file_name . Matches any character ^ of line collects all lines which have any three $ End of line characters surrounded by spaces \ Escape character  grep " [0-9]\{1,3\} " file_name [list] Matches any character in the list [^list] Matches any character not in the list collects all lines containing a sequence of * Match zero or more occurrences of the one to three digits surrounded by spaces previous regular expression  grep "^x*[abc]" file_name \{min,max\} Matches least min and at max occurrences of the previous regular expression collects all lines which start with zero or more x's followed by a single a, b, or c

7 8

-v option, inverting the More examples match  Let's pretend we have a file nameddata1 …  Let's pretend we have a file nameddata1 … 12 12 12 345 12 345 567 567 3 abd 3 abd asdf asdf  And this …  And this script… #! /bin/ # begins with 1 or 2 #! /bin/bash grep "^[0-9]\{1,2\} " data1 # digits followed by grep -v "^[0-9]\{1,2\} " data1 0 # a space exit 0  We should get this output…  We should get this output… 12 345 12 3 abd 567 asdf

9 10

-c option, counting the -n option, adding line matches numbers  Let's pretend we have a file nameddata1 …  Let's pretend we have a file nameddata1 … 12 12 12 345 12 345 567 567 3 abd 3 abd asdf asdf  And this script…  And this script… #! /bin/bash #! /bin/bash grep -n "^[0-9]\{1,2\} " data1 grep -c "^[0-9]\{1,2\} " data1 exit 0 exit 0  We should get this output…  We should get this output… 2:12 345 2 4:3 abd

11 12

Using grep inside a Output script #! /bin/bash $ Id_File $ Check if (( $# != 1 )); then "Usage: $0 " sam Usage: Check exit 1 maryann $ Check jeff fi USER="$1" john Bad way: jeff in file if echo "${USER}" Id_File > /dev/null then jeff Good way: jeff in file echo "Bad way: ${USER} in file" jeffrey $ Check son else echo "Bad way: ${USER} not in file" bill Bad way: son in file fi william Good way: son not in file if grep "^${USER}$" Id_File > /dev/null then peterson echo "Good way: ${USER} in file" else echo "Good way: ${USER} not in file" fi exit 0

13 14

  tail [+/-[n] [b|c|l] [-f]] file Collects the first n lines of a file with delivers n units from the file n defaulting to 10 if unspecified +n counting from the head [-n] file -n counting from the end n defaults to -10 if unspecified  Examples… counting by head -30 yuk # top 30 lines b blocks head yuk # top 10 lines c characters head * # top 10 lines of every file l lines (default) -f means follow - infinite trailing output (use ctrl-c to stop)  Has buffer limitations - see the man page

15 16

Examples

 tail +10 yuk # all lines beyond line 10  Used to vertical cuts across a tail -30 yuk # last 30 lines file tail -30c yuk # last 30 characters cut -flags columns or field filename tail -30f yuk # last 30 lines,  Useful flags continuing outputting any added lines -c characters tail -30 * # last 30 lines of all files -d field delimiter -f fields  See man page for more information

17 18

Examples Another example

 Given a filedata2 ,  Here's another example: 12345 7890 abcd efgb #! /bin/bash This is line one DAY_OF_WEEK="$(date | cut -d' ' -f1-1)" this is no big deal MONTH="$(date | cut -d' ' -f2-2)"  …and this script, DAY="$(date | cut -d' ' -f3-3)" #! /bin/bash YEAR="$(date | cut -d' ' -f6-)" cut -c1-5,8- data2 echo "Date: $(date)" echo '------' echo "Month: ${MONTH}" cut -d' ' -f2-3 data2 echo "Day: ${Day}" exit 0 echo "Year: ${Year}" echo "Day of the week: ${DAY_OF_WEEK}"  We get this output: exit 0 12345890 abcd efgb This line one  Which outputs… this no big deal Date: Mon Jul 22 16:01:17 EST 1996 ------Month: Jul 7890 abcd Day: 22 is line Year: 1996 is no Day of the week: Mon

19 20

More examples

 Used to combine lines from two files together  paste -s [-d list] file1 file2 … paste [-dlist] file1 file2 … merges lines together serially (one file  By default concatenates corresponding lines at a ) of the files together using a tab as the  paste -s -d" \n" yuk separator pastes each pair of lines in the file yuk  Example: together paste -d" " x y z  the list specified with -d is a space followed concatenates the corresponding lines of the by a newline filesx , y, and z together using the list of separators circularly. In this case the list only  See man page for more options and contains a single space. information

21 22

wc Example

 Word count #! /bin/bash Output: wc -[c||l] file wc x.c 301 878 8382 x.c wc -l x.c 301 x.c  Used to count wc -w x.c 878 x.c -c characters wc -c x.c 8382 x.c =$(wc x.c) 301 878 8382 x.c -l lines echo ${NL} " 301 878 8382 x.c" -w words (separated by whitespace) echo "\"${NL}\"" " 301" LL=$(wc -l < x.c) 301  Default is all three echo "\"${LL}\"" echo ${LL} exit 0

23 24

A quick look at sort example

 Read the man page for more information  This example,  #! /bin/bash A few very useful flags: cat data -u unique lines only echo -txfield separatorx (default is whitespace) sort data3 exit 0 -b ignore leading blanks  Yields this output: -r reverse sort 1 a 5 0 b 3 -n numbers not characters 2 b 4 1 a 5 -k sort on fields (up to 10-k options 3 a 4 1 a 5 allowed) 1 b 4 1 b 4 0 b 3 2 b 4  Note: field numbers begin with 1 1 a 5 3 a 4

25 26

Another example -u and -k example

 This example,  This example, #! /bin/bash #! /bin/bash cat data cat data echo echo sort -u data3 sort -u -k 2 data3 exit 0 exit 0  Yields this output:  Yields this output: 1 a 5 0 b 3 1 a 5 3 a 4 2 b 4 1 a 5 2 b 4 1 a 5 3 a 4 1 b 4 3 a 4 0 b 3 1 b 4 2 b 4 1 b 4 1 b 4 0 b 3 3 a 4 0 b 3 1 a 5 1 a 5

27 28

Another -u and -k Specifying field order example  This example,  This example, #! /bin/bash #! /bin/bash cat data cat data echo echo sort -u -k 2,2 data3 sort -ur -k 2,2 -k 3,3 -k 1,1 data3 exit 0 exit 0  Yields this output:  Yields this output: 1 a 5 1 a 5 1 a 5 2 b 4 2 b 4 0 b 3 2 b 4 1 b 4 3 a 4 3 a 4 0 b 3 1 b 4 1 b 4 1 a 5 0 b 3 0 b 3 3 a 4 1 a 5 1 a 5

29 30

Beating the dead horse Questions?

 This example, #! /bin/bash cat data echo sort -k 3,3 -k 1,1 -k 2,2 data3 echo sort -k 3bn,3 -k 1bn,1 -k 2b,2 data3 exit 0  Yields this output: 1 a 5 3 a 14 11 b 4 11 b 4 11 b 4 1 a 5 12 c 40 12 c 40 3 a 14 2 a 40 2 a 40 2 a 40 21 c 51 1 a 5 12 c 40 3 a 14 21 c 51 21 c 51

31 32