FREE OPEN SOURCE LAB

EXP-1:

Session-1
  1. Log into the system
  2. Use ‘VI” editor to create a file called myfile.txt which contains some text.
  3. Correct typing errors  during creation
  4. Save the file
  5. Logout of the system

  1. Log into the system
When we return on the system one screen will appear. In this we have to type 100.0.0.9 then we enter into editor. It asks our details such as
         Login : krishnasai
         password:
Then we get log into the commands.

  1. Use vi editor to create a file called myfile.txt which contains some text
Syntax:-vi file name
Description: 
Ø  Vi Command is used to create and editing a file;
Ø  If we type "vi filename".
ü  If the file named filename exists, then the first page (or screen) of the file will be                  displayed;
ü  If the file does not exist, then an empty file and screen are created into which you may enter text.
Example: vi myfile.text.

  1. Correct typing errors  during creation
In creating a file through vi editor an error an error will occur we  modify the file by  opening the file again. An error will occur when we don’t give space between filename and command name
Example: vikrishnasai

Save the File.
To save the file created in vi editor we press esc “:WQ” .It means that saves the file and  Quits editing mode .then we come out from the vi editor.
:wq:- quit vi, writing out modified file to file named in original invocation
:q!:- quit vi even though latest changes have not been saved for this vi call
:x:- quit vi, writing out modified file to file named in original invocation

Logout the system.
Syntax: exit
Description: In our current directory press exit to log out from the commands


Session-2
  1. Log into the system
  2. Open the file created in session-1.
  3. Add some text
  4. Change some text
  5. Delete some text.
  6. Save the file
  7. Logout of the system

a.      Log into the system
When we return on the system one screen will appear. In this we have to type 100.0.0.9 then we enter into editor. It asks our details such as
         Login : krishnasai
         password:
Then we get log into the commands.


b.      Open the file created in session -1
Syntax: vi filename
In the above session we create a file in vi editor To open the  file created in  Session 1 we have to type as follows  “vi filename”


c.       Add Some text:
To add some text to the file which is already created first we have to open that file then add text.
vi filename. Text
The following commands allow you to insert and add text. Each of these commands puts the vi editor into insert mode; thus, the <Esc> key must be pressed to terminate the entry of text and to put the vi editor back into command mode.

Ø  i:- insert text before cursor, until <Esc> hit
Ø  I:- insert text at beginning of current line, until <Esc> hit
Ø  a:- append text after cursor, until <Esc> hit
Ø  A:- append text to end of current line, until <Esc> hit
Ø  o:- open and put text in a new line below current line, until <Esc> hit
Ø  O:- open and put text in a new line above current line, until <Esc> hit



d.      Changing Text
To change the some text in myfile. text move the cursor where we want to change the text After that replace the text under cursor with other text first open the file as follows.
vi myfile.text
The following commands allow you to modify text.
Ø  r:- replace single character under cursor (no <Esc> needed)
Ø  R:- replace characters, starting with current cursor position, until <Esc> hit
Ø  cw:- change the current word with new text, starting with the character under cursor, until <Esc> hit
Ø  cNw:-change N words beginning with character under cursor, until <Esc> hit;
Ø  e.g., c5w changes 5 words
Ø  C:- change (replace) the characters in the current line, until <Esc> hit
Ø  cc:- change (replace) the entire current line, stopping when <Esc> is hit
Ø  Ncc or cNc:-change (replace) the next N lines, starting with the current line, stopping when <Esc> is hit

e.       Delete some text
To delete text in my file text we first move the cursor to end of that line and then press delete then the line is erased to do this first we open the file as
 vi myfile.text
The following commands allow you to delete text.
Ø  x:- delete single character under cursor
Ø  Nx:- delete N characters, starting with character under cursor
Ø  dw:- delete the single word beginning with character under cursor
Ø  dNw:-delete N words beginning with character under cursor;
Ø  e.g., d5w deletes 5 words
Ø  D:- delete the remainder of the line, starting with current cursor position
Ø  dd:- delete entire current line
Ø  Ndd or dNd:-delete N lines, beginning with the current line; e.g., 5dd deletes 5 lines

f.       Save the changes:
To save the changes made in file myfile.text we press
esc:WQ
It means that saves the file and quits editing mode then we log out from the vi  Mode.

Logout the system.
Syntax: exit
Description: In our current directory press exit to log out from the commands




*********************************************************************************
EXP-2:

  1. Log into the system
  2. Use the ‘cat’ command to create a file containing the following data. Call it mytable use tabs to separate the fields.
1425    ravi      15.65
4320    ramu    26.27
6830    sita       36.15
1450    raju      21.86
  1. Use the cat command to display the file, mytable.
  2. Use the ‘vi’ command to correct any errors in the file, mytable.
  3. Use the ‘sort’ command to sort the file my table according to the first field. Call the sorted file my table (same name)
  4. Print the file mytable.
  5. Use the cut and paste commands to swap fields 2 and 3 of mytable. Call it my table (Same name)
  6. Print the new, my table.
  7. Logout of the system






  1. Log into the system
When we return on the system one screen will appear. In this we have to type 100.0.0.9 then we enter into editor. It asks our details such as
         Login : krishnasai
         password:
Then we get log into the commands.

  1. Use the cat command to create a file containing the following data. Call it mytable use tabs to separate the fields.
1425   Ravi     15.65
4320   Ramu   26.27
6830   Sita      36.15
1450   Raju     21.86
cat > mytable
1425   Ravi     15.65
4320   Ramu   26.27
6830   Sita      36.15
            1450   Raju     21.86
To save the file press Ctrl + D or Ctrl+Z
Description:-
Cat Command can view the file data and create a new file and also concatenate file
ü  Creating file
Cat>file1.txt
ü  Creating one file from several files
cat file1 file2 file3 > file4
This command combines the contents of the first three files into file4. With this command, file4 is created if it didn't already exist. (Or, it will overwrite file4 if file4 already existed.)
ü  Showing line numbers
cat -n myfile.txt
cat myfile -b
This prints the line number before each line that is output
ü  appending one file to another file
cat file1>>file2
Append file1 at the end of the file2


  1. Use the cat command to display the file, mytable.
$cat mytable
1425  Ravi  15.65
4320  Ramu  26.27
6830  Sita  36.15
1450  Raju  21.86



  1. Use the vi command to correct any errors in the file ,mytable
By using vi editor we have to correct errors occurred in creating file .
Open the file in vi editor as
Syntax: Vi mytable
Use vi command to correct errors


  1. 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






  1. Print the file mytable
cat mytable
1425  Ravi   15.65
1450  Raju   21.86
4320  Ramu   26.27
6830  Sita       36.15

  1. Use the cut and paste commands to swap fields 2 and 3 of mytable. Call it my table (same name)
$cut -f1 > mytab1
$ cut –f 2 > mytab 2
$cut –f 3 > my tab3
$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

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.

6. Merge a 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




  1. Print the new file, mytable
cat mytable
1425   15.65   Ravi
1450   21.86   Raju
4320   26.27   Ramu
6830   36.15   Sita

  1. Logout the system.
Syntax: exit
Description: In our current directory press exit to log out from the commands

*********************************************************************************

EXP-3:
1      
a)      Login to the system
b)      Use the appropriate command to determine your login shell
c)      Use the /etc/passwd file to verify the result of step b.
d)     Use the ‘who’ command and redirect the result to a file called myfile1. Use the more command to see the contents of myfile1.
e)      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.

2           
a)      Write a “sed” command that deletes the first character in each line in a file.
b)      Write a “sed” command that deletes the character before the last character in each line in a file.
c)      Write a “sed” command that swaps the first and second words in each line in a file.


  1. Log into the system
When we return on the system one screen will appear. In this we have to type 100.0.0.9 then we enter into editor. It asks our details such as
         Login : krishnasai
         password:
Then we get log into the commands.

  1. 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:
Shell Name
Developed by
Where
Remark
BASH
( Bourne-Again SHell )
Brian Fox and
Chet Ramey
Free Software Foundation
Most common shell in Linux. It's Freeware shell.
CSH
(C SHell)
Bill Joy
University of California
 (For BSD)
The C shell's syntax and usage are very similar to
the C programming language.
KSH
(Korn SHell)
David Korn
AT & T Bell Labs
--
TCSH
See the man page.
Type $ man tcsh
--
TCSH is an enhanced but completely compatible version of the Berkeley UNIX C shell(CSH).

Ø  To find all available shells in your system type following command:
$ cat /etc/shells

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


  1. 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.

  1. 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)

2

A)    Write a sed command that deletes the first character in each line in a file.
$ cat  mytable
                        1425    Ravi      15.65
                        4320    Ramu    26.27
                        6830    Sita       36.15
                        1450    Raju      21.86
                        7820    Anil      23.34
                        9000    Allam   35.56     

                        $ sed  's/^./ / ' mytable
                            425    Ravi        15.65
                        320    Ramu      26.27
                        830    Sita         36.15
                        450    Raju        21.86
                        820    Anil        23.34
                        000   Allam     35.56

B)    Write a sed command that deletes the character before the last character in each line in a file.
# Delete the last character in each line in a file.

                        $ sed  's/.$/  /'  mytable
                        1425    Ravi      15.6
                        4320    Ramu    26.2
                        6830    Sita       36.1
                        1450    Raju      21.8
                        7820    Anil      23.3
                        9000    Allam   35.5

C)    Write a sed command that swaps the first and second words in a file.
# Swaps the first and second words in each line in a file.

                        $ sed   ‘s/ \([^ ]*\) *\([^ ]*\)/  \2  \1 /g’ mytable
                        Ravi                 1425    15.65   
                        Ramu               4320    26.27   
                        Sita                  6830    36.15   
                        Raju                 1450    21.86   
                        Anil                 7820    23.34   
                        Allam                              9000            35.56


descripiton

Sed is a Stream Editor used for modifying the files in unix (or linux). Whenever you want to make changes to the file automatically, sed comes in handy to do this.
>cat file.txt
unix is great os. unix is opensource. unix is free os.
learn operating system.
unixlinux which one you choose.

 

SED COMMAND EXAMPLES


1. Replacing or substituting string
Sed command is mostly used to replace the text in a file. The below simple sed command replaces the word "unix" with "linux" in the file.
>sed 's/unix/linux/' file.txt
linux is great os. unix is opensource. unix is free os.
learn operating system.
linuxlinux which one you choose.

Here the "s" specifies the substitution operation. The "/" are delimiters. The "unix" is the search pattern and the "linux" is the replacement string.

By default, the sed command replaces the first occurrence of the pattern in each line and it won't replace the second, third...occurrence in the line.

2. Replacing the nth occurrence of a pattern in a line.
Use the /1, /2 etc flags to replace the first, second occurrence of a pattern in a line. The below command replaces the second occurrence of the word "unix" with "linux" in a line.
>sed 's/unix/linux/2' file.txt
unix is great os. linux is opensource. unix is free os.
learn operating system.
unixlinux which one you choose.

3. Replacing all the occurrence of the pattern in a line.

The substitute flag /g (global replacement) specifies the sed command to replace all the occurrences of the string in the line.
>sed 's/unix/linux/g' file.txt
linux is great os. linux is opensource. linux is free os.
learn operating system.
linuxlinux which one you choose.

4. Replacing from nth occurrence to all occurrences in a line.
Use the combination of /1, /2 etc and /g to replace all the patterns from the nth occurrence of a pattern in a line. The following sed command replaces the third, fourth, fifth... "unix" word with "linux" word in a line.
>sed 's/unix/linux/3g' file.txt
unix is great os. unix is opensource. linux is free os.
learn operating system.
unixlinux which one you choose.

5. Changing the slash (/) delimiter
You can use any delimiter other than the slash. As an example if you want to change the web url to another url as
>sed 's/http:\/\//www/' file.txt

In this case the url consists the delimiter character which we used. In that case you have to escape the slash with backslash character, otherwise the substitution won't work.
Using too many backslashes makes the sed command look awkward. In this case we can change the delimiter to another character as shown in the below example.


>sed 's_http://_www_' file.txt
>sed 's|http://|www|' file.txt

6. Using & as the matched string

There might be cases where you want to search for the pattern and replace that pattern by adding some extra characters to it. In such cases & comes in handy. The & represents the matched string.

>sed 's/unix/{&}/' file.txt
{unix} is great os. unix is opensource. unix is free os.
learn operating system.
{unix}linux which one you choose.
 
>sed 's/unix/{&&}/' file.txt
{unixunix} is great os. unix is opensource. unix is free os.
learn operating system.
{unixunix}linux which one you choose.

7. Using \1,\2 and so on to \9
The first pair of parenthesis specified in the pattern represents the \1, the second represents the \2 and so on. The \1,\2 can be used in the replacement string to make changes to the source string. As an example, if you want to replace the word "unix" in a line with twice as the word like "unixunix" use the sed command as below.
>sed 's/\(unix\)/\1\1/' file.txt
unixunix is great os. unix is opensource. unix is free os.
learn operating system.
unixunixlinux which one you choose.
The parenthesis needs to be escaped with the backslash character. Another example is if you want to switch the words "unixlinux" as "linuxunix", the sed command is
>sed 's/\(unix\)\(linux\)/\2\1/' file.txt
unix is great os. unix is opensource. unix is free os.
learn operating system.
linuxunix which one you choose.
Another example is switching the first three characters in a line
>sed 's/^\(.\)\(.\)\(.\)/\3\2\1/' file.txt
inux is great os. unix is opensource. unix is free os.
aelrn operating system.
inuxlinux which one you choose.

8. Duplicating the replaced line with /p flag
The /p print flag prints the replaced line twice on the terminal. If a line does not have the search pattern and is not replaced, then the /p prints that line only once.
>sed 's/unix/linux/p' file.txt
linux is great os. unix is opensource. unix is free os.
linux is great os. unix is opensource. unix is free os.
learn operating system.
linuxlinux which one you choose.
linuxlinux which one you choose.


9. Printing only the replaced lines
Use the -n option along with the /p print flag to display only the replaced lines. Here the -n option suppresses the duplicate rows generated by the /p flag and prints the replaced lines only one time.
>sed -n 's/unix/linux/p' file.txt
linux is great os. unix is opensource. unix is free os.
linuxlinux which one you choose.

If you use -n alone without /p, then the sed does not print anything.

10. Running multiple sed commands.
You can run multiple sed commands by piping the output of one sed command as input to another sed command.
>sed 's/unix/linux/' file.txt| sed 's/os/system/'
linux is great system. unix is opensource. unix is free os.
learn operating system.
linuxlinux which one you chosysteme.
Sed provides -e option to run multiple sed commands in a single sed command. The above output can be achieved in a single sed command as shown below.

>sed -e 's/unix/linux/' -e 's/os/system/' file.txt
linux is great system. unix is opensource. unix is free os.
learn operating system.
linuxlinux which one you chosysteme.

11. Replacing string on a specific line number.
You can restrict the sed command to replace the string on a specific line number. An example is
>sed '3 s/unix/linux/' file.txt
unix is great os. unix is opensource. unix is free os.
learn operating system.
linuxlinux which one you choose.

The above sed command replaces the string only on the third line.

12. Replacing string on a range of lines.
You can specify a range of line numbers to the sed command for replacing a string.
>sed '1,3 s/unix/linux/' file.txt
linux is great os. unix is opensource. unix is free os.
learn operating system.
linuxlinux which one you choose.

Here the sed command replaces the lines with range from 1 to 3. Another example is
>sed '2,$ s/unix/linux/' file.txt
linux is great os. unix is opensource. unix is free os.
learn operating system.
linuxlinux which one you choose.

Here $ indicates the last line in the file. So the sed command replaces the text from second line to last line in the file.


13. Replace on a lines which matches a pattern.
You can specify a pattern to the sed command to match in a line. If the pattern match occurs, then only the sed command looks for the string to be replaced and if it finds, then the sed command replaces the string.
>sed '/linux/ s/unix/centos/' file.txt
unix is great os. unix is opensource. unix is free os.
learn operating system.
centoslinux which one you choose.
Here the sed command first looks for the lines which has the pattern "linux" and then replaces the word "unix" with "centos".

14. Deleting lines.
You can delete the lines a file by specifying the line number or a range or numbers.
>sed '2 d' file.txt
>sed '5,$ d' file.txt

15. Duplicating lines
You can make the sed command to print each line of a file two times.
>sed 'p' file.txt

16. Sed as grep command
You can make sed command to work as similar to grep command.
>grep 'unix' file.txt
>sed -n '/unix/ p' file.txt

Here the sed command looks for the pattern "unix" in each line of a file and prints those lines that has the pattern.
You can also make the sed command to work as grep -v, just by using the reversing the sed with NOT (!).
>grep -v 'unix' file.txt
>sed -n '/unix/ !p' file.txt

The ! here inverts the pattern match.

17. Add a line after a match.
The sed command can add a new line after a pattern match is found. The "a" command to sed tells it to add a new line after a match is found.
>sed '/unix/ a "Add a new line"' file.txt
unix is great os. unix is opensource. unix is free os.
"Add a new line"
learn operating system.
unixlinux which one you choose.
"Add a new line"

18. Add a line before a match
The sed command can add a new line before a pattern match is found. The "i" command to sed tells it to add a new line before a match is found.
>sed '/unix/ i "Add a new line"' file.txt
"Add a new line"
unix is great os. unix is opensource. unix is free os.
learn operating system.
"Add a new line"
unixlinux which one you choose.

19. Change a line
The sed command can be used to replace an entire line with a new line. The "c" command to sed tells it to change the line.
>sed '/unix/ c "Change line"' file.txt
"Change line"
learn operating system.
"Change line"

20. Transform like tr command
The sed command can be used to convert the lower case letters to upper case letters by using the transform "y" option.
>sed 'y/ul/UL/' file.txt
Unix is great os. Unix is opensoUrce. Unix is free os.
Learn operating system.
UnixLinUx which one yoU choose.


Here the sed command transforms the alphabets "ul" into their uppercase format "UL"

0 comments: