NEED Help with org/420. My week 3 paper that you must go off of is in attachment. IF YOU CAN NOT COMPLETE DO NOT RESPOND!
Introduction to Unix - POS420
Unix Lab Exercise Week 3 A
Topics : Input/Output redirection
cut, paste and touch commands
Input/Output redirection :
1. When the shell executes any command, shell also opens three files called file descriptors for every execution.
They are
stdin 0 Standard input to the program
stdout 1 Standard output from the program
stderr 2 Standard error output from the program
A. Standard Input :
Here, wc -l command takes input from standard input, keyboard
$ wc -l
This is the text that
is typed on the
standard input device.
CTRL-d
3
$
B. Standard Output :
Here, the output of echo and date commands go to the terminal.
$ echo "hello"
hello
$ date
Mon Aug 11 17:57:38 EDT 2014
C. Standard Error :
$ ls myfile.txt
myfile.txt not found
$
ls command does not find myfile.txt in the current directory it sends the error message to the terminal.
Let us type a command wrongly
$ ecoh hello
ksh: ecoh: not found. --- Standard Error is diverted to terminal
2. We know that > represents to redirect output
< to redirect input
>> to append to a file
Let us redirect the output of this command to a file.
$ who > users
$
To see the contents
$ cat users
$
$ echo "This is line 1" > myfile
$ cat myfile
This is line 1
$ echo "This is line 2" >> myfile
$ cat myfile
This is line 1
This is line 2
2. Let us create a file called "datafile” in the east directory under sales.
(We have already created sales and purchases directories in our first lab). Enter the data through vi.
$ vi datafile
Enter the following data in vi insert mode :
Tom Smith 7.00 15 105.00 Rob Sheryl 8.00 20 160.00 Ken Bradman 7.00 13 91.00 Peter Smith 6.00 15 90.00 Dennis Smith 8.00 13 104.00 Tom Dave 9.00 12 108.00
3. We will append one more entry into this file
$ echo " John Lee 7.50 10 75.0" >> datafile (If you use > , it will overwrite, but we are appending)
4. Create a file “memo1” with the Sales data in east directory
$ vi memo1 (Enter the following data)
Sales of the Year 2008 Grocery $2000.00 Spare Parts $3000.00 Automobile $2678.00 Misc $1200.00
Create another file memo2 and enter the data
$ vi memo2
Sales of the Year 2007 Grocery $2100.00 Spare Parts $5000.00 Automobile $2999.00 Misc $1000.00
Now we will concatanate both files into Sales_0708.dat
$ cat memo1 memo2 Sales of the Year 2008 Grocery $2000.00 Spare Parts $3000.00 Automobile $2678.00 Misc $1200.00 Sales of the Year 2007 Grocery $2000.00 Spare Parts $3000.00 Automobile $2678.00 Misc $1200.00
This is not what we want, we want to redirect to a file.
$ cat memo1 memo2 > Sales_0708.dat
Now let us see the file
$ cat Sales_0708.dat
Sales of the Year 2007 Grocery $2000.00 Spare Parts $3000.00 Automobile $2678.00 Misc $1200.00 Sales of the Year 2008 Grocery $2000.00 Spare Parts $3000.00 Automobile $2678.00 Misc $1200.00
My manager is unhappy that there is no newline between those two year's data. Let us delete the file and recreate it
$ rm Sales_0708.dat
$ echo "" >> memo1 $ cat memo1 memo2 > Sales_0708.dat
Now let us see the file
$ cat Sales_0708.dat
Sales of the Year 2007 Grocery $2000.00 Spare Parts $3000.00 Automobile $2678.00 Misc $1200.00
Sales of the Year 2008 Grocery $2000.00 Spare Parts $3000.00 Automobile $2678.00 Misc $1200.005.
$ echo "This is line 1" > file1
$ cat file1
This is line 1
$ echo "This is line 2" >> file2
$ cat file2
This is a line in file2.
$ cat file1 >> file2
$ cat file2
This is line in file2.
This is line in file1.
5. Another example :
$ cat file1
This is line in file1.
$ cat file2
This is line in file2.
$ cat file1 file2 > file3
$ cat file3
This is line in file1.
This is a line in file2.
6. Let us see input redirection
$ wc -l users
7 users
$
Now let us redirect the input of file to wc command.
$ wc -l < users
7
7. The sort command which we are going tp learn in detail later, sorts a file alphabetically or numerically.
Type the names of some cities and enter [Return] after each one and once you are done enter CTRL-d.
$ sort
Newyork
Chicago
Los Angeles
Detroit
CTRL-d
The output will be
Chicago
Detroit
Los Angeles
Newyork
Now we can redirect the input to come from a file (say the file citylist has a list of cities) rather than the keyboard.
To sort the list of cities, type
Enter the cities in a file 'citylist' with vi
$ vi citylist
and type the following cities
Newyork
Chicago
Los Angeles
Detroit
Now let us sort
$ sort < citylist
Chicago
Detroit
Los Angeles
Newyork
The sorted list will be the output to the screen.
Now let us redirect the ouput to to a file.
$ sort < citylist > citysorted
$
See the contents of the file citysorted
$ cat citysorted
Chicago
Detroit
Los Angeles
Newyork
Let us see some examples :
1. To find out how many users are logged on, type
$ who | wc -l
2. To see top ten files of /etc directory.
$ ls -l /etc | head
3. We want to redirect the man page of ksh to a file.
$ man ksh > ksh_manpage
4. To see lines from 50 to 60 in a file.
$ head -60 filename | tail
5. To look for a particular user
$ who | grep mary
6. To count number of files in the /etc directory
$ ls -l /etc | wc -l
7. Sort the users currently logged into the system
$ who | sort
8. Create a file “ phonebook” in your home directory.
Tom Smith 732-330-1111
Kay George 732-300-0000
Penny Smith 973-560-1234
David Korn 908-444-0987
Mary Lisa 732-666-6789
Ashwin Patel 732-786-4567
Tom Kerry 732-456-1456
9. Create another file "intro" in your home directory and type the following :(enter 4 lines as below)
The UNIX operating system was pioneered by Ken Thomson and Dennis Ritchie at
Bell Laboratories in the late 1960s.
One of the primary goals in the design of the UNIX system was to create an environment that (UNIX)promoted efficient program development.
cut command :
10. Display the current users logged in:
$ who
11. Extract the first 8 characters :
$ who | cut -c1-8
12. Sort the above users :
$ who | cut -c1-8 | sort
13. Extract the tty number fields from the above who command :
$ who | cut -c10-16
14. Display the user name and login name :
$ who | cut -c1-8,18-
15. Display the file /etc/passwd on the screen.
$ cat /etc/passwd
16. Extract the first field of the above file (This runs long on many machines like schools, free internet sites- Use CNTRL C to stop)
$ cut -d: -f1 /etc/passwd
17. Extract the username and home directory ( 6th field ) :
$ cut -d: -f1,6 /etc/passwd
paste command:
If you do not have the files users1, ssn create the files and enter the following contents.
$ vi users1
Tsmith Thomson Smith 9/17/02 Betsy Betsy Williams 9/16/02 Sandia Michael Sandiago 9/17/02 Vivian Vivian Richards 9/17/02 James James Blake 8/10/02 John Dev Jhonson 9/10/02 Cramer Ron Cramer 9/17/02 Nichols Ben Nicholos 8/01/02 David David Newton 9/16/02
$ vi ssn
Thomson Smith 111-22-0001 Betsy Williams 111-22-0002 Michael Sandiago 111-22-0004 Vivian Richards 111-22-0005 James Blake 111-22-0006 Dev Jhonson 111-22-0007 Ron Cramer 111-22-0008 Ben Nicholos 111-22-0009 David Newton 111-22-0010
18. First, extract the ssn into a temporary file, tempssn: $ cut -d" " -f3 ssn > tempssn
19. Now paste the ssn to the end of each line in users1 and store in a new file users_listing
$ paste users1 tempssn > users_listing
20. Now see the contents of the new file. $ cat users_listing
Another way is :
$ cut -d" " -f2 ssn | paste users1 > users_listing
touch command :
$ touch filename -- If filename does not exit it creates with zero size,otherwise it updates the access time.
21. Let us create a new file, before that we make sure that it does not exist.
$ ls filename
ls: The file filename does not exist.
22. Now create a file with touch command
$ touch filename
$ ls -l filename -rw-r--r-- 1 stangira faculty 0 Jul 18 14:14 filename
See the size of the file creatd by touch command
23. Let us update the access time of another file.
$ ls -l users -rw-r--r-- 1 stangira faculty 348 Jul 17 12:23 users
$ touch users
$ ls -lt users -rw-r--r-- 1 stangira faculty 348 Oct 31 23:15 users
Syam Tangirala University of Phoenix Online Faculty [email protected] 732-397-4997(CEL)