You are on page 1of 7

Week-4:

a) Use the sort command to sort the file mytable according to the first field. Call the
sorted file mytable (same name)
b) Print the file mytable
c) Use the cut and paste commands to swap fields 2 and 3 of mytable. Call it my table
(same name)
d) Print the new file, mytable
e) Logout of the system.
Mytable
121 sai 89 87 77 66 55
021 ram 90 76 54 67 89
221 king 91 67 45 87 55
002 ramana 87 76 56 54 47
322 sekhar 99 33 55 73 27

a. Use the sort command to sort the file mytable according to the first field. Call the
sorted file my table
$sort +1 mytable > mytable
or
$ sort -k 1 mytable > sortfile;cp sortfile mytable

Description:-
Sort:- is a simple and very useful command which will rearrange the lines in a text file so
that they are sorted, numerically and alphabetically.

Syntax: sort [OPTION]... [FILE]...

Options
⮚ -b, --ignore-leading-blanks Ignore leading blanks.
⮚ -d, --dictionary-order Consider only blanks and alphanumeric characters.
-f, --ignore-case Fold lower case to upper case characters.
⮚ -g, --general-numeric-sort Compare according to general numerical value.
⮚ -i, --ignore-nonprinting Consider only printable characters.
⮚ -M, --month-sort Compare (unknown) < `JAN' < ... < `DEC'.
⮚ -h, --human-numeric-sort Compare human readable numbers (e.g., "2K", "1G").
⮚ -n, --numeric-sort Compare according to string numerical value.
⮚ -R, --random-sort Sort by random hash of keys.
⮚ --random-source=FILE Get random bytes from FILE.
⮚ -r, --reverse Reverse the result of comparisons.
⮚ --sort=WORD Sort according to WORD: general-numeric -g, human-numeric -h,
month -M, numeric -n, random -R, version -V.
⮚ -V, --version-sort Natural sort of (version) numbers within text.
Note that this command does not actually change the input file, data.txt. If you want to write
the
output to a new file, output.txt, redirect the output like this:

sort data.txt > output.txt

You can perform a reverse-order sort using the -r flag. For example, the following command:
sort -r data.txt
b. print the file mytable
cat mytable

c. Use the cut and paste commands to swap fields 2 and 3 of mytable. Call it my
table (same name)

$cut -f1 > mytab1


$ cut –f2 > mytab 2
$cut –f3 > mytab3
$paste mytab3 mytab2 > mytab4
$paste mytab1 mytab4 > mytable

Description:
Cut
Remove or "cut out" sections of each line of a file or files.
Syntax cut OPTION... [FILE]...

Options
⮚ -b, --bytes=LIST Select only the bytes from each line as specified in LIST. LIST
specifies a byte, a set of bytes, or a range of bytes; see Specifying LIST below.
⮚ -c, --characters=LIST Select only the characters from each line as specified in
LIST.
LIST specifies a character, a set of characters, or a range of characters; see Specifying
LISTbelow.
⮚ -d, --delimiter=DELIM use character DELIM instead of a tab for the field
delimiter.
⮚ -f, --fields=LIST select only these fields on each line; also print any line that
contains no delimiter character, unless the -s option is specified. LIST specifies a
field, a set of fields, or a range of fields; see Specifying LIST below.
⮚ -n This option is ignored, but is included for compatibility reasons.
⮚ --complement complement the set of selected bytes, characters or fields.
⮚ -s, --only-delimited do not print lines not containing delimiters.
⮚ --output-delimiter=STRING use STRING as the output delimiter string. The
default
is to use the input delimiter.

To "cut" only the third field of each line, use the command:

cut -f 3 data.txt

If instead you want to "cut" only the second-through-fourth field of each line, use the
command:

cut -f 2-4 data.txt

o/p:

If you want to "cut" only the first-through-second and fourth-through-fifth field of each line
(omitting the third field), use the command:

cut -f 1-2,4-5 data.txt

If you want the third field and every field after it, omitting the first two fields. In this case,
you could use the command:

cut -f 3- data.txt

Specifying a range with LIST also applies to cutting characters (-c) or bytes (-b) from a line.
For example, to output only the third-through-twelfth character of every line ofdata.txt, use
the command:

cut -c 3-12 data.txt

paste command:
The paste command displays the corresponding lines of multiple files side-by-side.
Syntax: paste [OPTION]... [FILE]...
Examples: paste file1.txt file2.txt
This command would display the contents of file1.txt and file2.txt, side-by-side, with
the
corresponding lines of each file separated by a tab.
$ cat file1
Linux
Unix
Solaris
HPUX
AIX
paste command with a single file:

1. paste command without any options is as good as the cat command when operated on
a single file.
$ paste file1
Linux
Unix
Solaris
HPUX
AIX

2. Join all lines in a file:


$ paste -s file1
Linux Unix Solaris HPUX AIX
-s option of paste joins all the lines in a file. Since no delimiter is specified, default
delimiter
tab is used to separate the columns.

3. Join all lines using the comma delimiter:


$ paste -d, -s file1
Linux,Unix,Solaris,HPUX,AIX
-d option is used to specify the delimiter. Using this -d and -s combination, all the
lines in the
file get merged into a single line.

4. Merge a file by pasting the data into 2 columns:


$ paste - - < file1
Linux Unix
Solaris HPUX
AIX

The '-' reads a line from the standard input. Two '-' reads 2 lines and pastes them side by side.
5.Merge a file by pasting the data into 2 columns using a colon separator:
$ paste -d':' - - < file1
Linux:Unix
Solaris:HPUX
AIX:
This is same as joining every 2 lines in a file.

s file by pasting the file contents into 3 columns:

$ paste - - - < file1


Linux Unix Solaris
HPUX AIX

7. Merge a file into 3 columns using 2 different delimiters:


$ paste -d ':,' - - - < file1
Linux:Unix,Solaris
HPUX:AIX,

The -d option can take multiple de-limiters. The 1st and 2nd columns is separated by ':',
whereas the 2nd and 3rd are separated by a ','.
paste command examples for multiple files handling
Let us consider a file, file2, with the following contents:
$ cat file2
Suse
Fedora
CentOS
OEL
Ubuntu

8. paste contents of 2 files side by side.


$ paste file1 file2
Linux Suse
Unix Fedora
Solaris CentOS
HPUX OEL
AIX Ubuntu
paste command is used in scenarios to merge multiple files side by side. As shown above, the
file contents are pasted side by side.

9. paste contents of 2 files side by side with a comma separator:


$ paste -d, file1 file2
Linux,Suse
Unix,Fedora
Solaris,CentOS
HPUX,OEL
AIX,Ubuntu
10. paste command can take standard input in case of multiple files too:

$ cat file2 | paste -d, file1 -


Linux,Suse
Unix,Fedora
Solaris,CentOS
HPUX,OEL
AIX,Ubuntu
Like this as well:

$ cat file1 | paste -d, - file2


Linux,Suse
Unix,Fedora
Solaris,CentOS
HPUX,OEL
AIX,Ubuntu
One more:

$ cat file1 file2 | paste -d, - -


Linux,Unix
Solaris,HPUX
AIX,Suse
Fedora,CentOS
OEL,Ubuntu

11. Read lines in both the files alternatively:


$ paste -d'\n' file1 file2
Linux
Suse
Unix
Fedora
Solaris
CentOS
HPUX
OEL
AIX
Ubuntu

d. Print the new file, mytable


cat mytable
1425 15.65 Ravi
1450 21.86 Raju
4320 26.27 Ramu
6830 36.15 Sita

e. Logout the system.


Syntax: exit
Description: In our current directory press exit to log out from the commands
Week-5:
a) Use the appropriate command to determine your login shell
b) Use the /etc/passwd file to verify the result of “step a”.
c) Use the who command and redirect the result to a file called myfile1. Use the more command
to see the contents of myfile1.
d) Use the date and who commands in sequence (in one line) such that the output of date
willdisplay on the screen and the output of who will be redirected to a file called myfile2. Use the
more command to check the contents of myfile2.

a. use the appropriate command to determine your login shell


Syntax: $ echo $SHELL
Output: $ echo $SHELL
/bin/bash
Description:-
What is "the shell"?
Shell is a program that takes your commands from the keyboard and gives them to the
operating system to perform. In the old days, it was the only user interface available
on a Unix computer. Nowadays, we have graphical user interfaces (GUIs) in addition
to command
line interfaces (CLIs) such as the shell.
On most Linux systems a program called bash (which stands for Bourne Again SHell,
an enhanced version of the original Bourne shell program, sh, written by Steve
Bourne) acts as the shell program. There are several additional shell programs
available on a typical Linux system. These include: ksh, tcsh and zsh.

To find your current shell type following command


$ echo $SHELL
⮚ Shell is a user program or it's environment provided for user interaction. Shell is an
command language interpreter that executes commands read from the standard input
device (keyboard) or from a file.
⮚ Shell is not part of system kernel, but uses the system kernel to execute programs,
create files etc.
⮚ Several shell available with Linux including:
⮚ To find all available shells in your system type following command:
$ cat /etc/shells

b. use the /etc/passwd file to verify the result of step b.

$cat /etc/passwd

c. Use the who command and redirect the result to a file called myfile1. Use the
more command to see the contents of myfile1.

Sol : $who > myfile1 | more


User1 pts/0 Apr 23 10:43
User2 pts/1 May 6 18:19
Description:
⮚ Who>myfile1🡪The result of who command is stored in myfile1
⮚ More myfile1🡪By using more command we print the myfile1 contents
"who" command:
The Linux "who" command lets you display the users that are currently logged into your
Unix computer system.
Syntax: who
On a very busy Unix/Linux system the output of the who command may scroll off your
terminal screen. To solve that, pipe the output of the who command into the Linux more
command, like this: who | more
The -a argument of the who command lists all available output for each user on your system.
who -a
more command:
is a command to view (but not modify) the contents of a text file one screen at a time.
The syntax for the more command is:
more [options] [files]

OPTIONS
Option Description
⮚ -c Page through the file by clearing the window. (not scrolling).
⮚ -d Displays "Press space to continue, 'q' to quit"
⮚ -f Count logical lines rather than screen lines (wrapping text)
⮚ -l Ignores form feed (^L) characters.
⮚ -r Display all control characters.
⮚ -s Displays multiple blank lines as one blank line.
⮚ -u Does not display underline characters and backspace (^H).
⮚ -w Waits for a user to press a key before exiting.
⮚ -n Displays n lines per window.
⮚ +num Displays the file starting at line number num.
⮚ +/pattern Displays the file starting at two lines before the pattern.

d. Use the date and who commands in sequence (in one line) such that the output of
date will display on the screen and the output of who will be redirected to a file
called myfile2. Use the more command to check the contents of myfile2.

$ date;who > myfile2


Sat Aug 23 09:38:05 IST 2008
$ more myfile2
allam tty7 2008-08-23 09:08 (:0)
allam pts/1 2008-08-23 09:09 (:0.0)

You might also like