Happy Diwali
happy diwali

Saturday, 7 June 2014

Bsic Unix commands



Cat: - it is used to create new files (or) to Open existing files (Or) Append data to the existing files
Syntax: - Cat > File Name
                Ctrld [save & close]
How to open a file
Cat < filename (or) cat filename
Appending data to the file
Cat>> filename
          Ctrl D
It append data at the end of the file
How to open multiple files
Cat file1 file2------file N
Ex: - cat emp1 emp2 emp3
Touch: - it is used to create empty file [0 byte file]
Syntax: - Touch filename
Creating multiple files
Syntax: - Touch file1 file2 file3
By using cat>> we can enter data into the files which were created by touch
How to delete files
Syntax: - rm filename
Rm –I file name [it deletes files with conformation message] delete file Y/N
Rm –f filename [it delete file F-forcibly]
Deleting multiple files
Syntax: rm file1 file2 file3 ----- file N
Working with directories
Syntax:- Mkdir dirname    [it is used to create a new directory
PWD [present working directories]
CD: - to change the directory
Syntax:- cd dirname
Cd abc
    Pwd
Cd..
To come out from current directory
Cut: - it is used to retrieve specific fields and specific characters in a given file
Cut –f 2, 4 filename [it displays 2nd field to 5th field file details
Cut –d “,” –f 2, 4 filename [here ‘,’ is the delimiter, it display 2 and 4 fields from file]
Cut –c 1-5 filename [c means character to character, it displays every line first character to fifth character]
Paste:-It is used to join two (or) more files horizontally by using delimiter
Syntax: - Paste filename1 filename2
Grep :- it is used to search a string (or) regular expression in a file
Syntax: - Grep techno file    [ grep pattern filename(s)]
Grep sachin file1 file2 file3
Grep sachin* [it searches in current directory all files]
Note: - if string contains more than one word it must enclosed with in double quotes, otherwise second word becomes file name
Ex: - “sachin sehwag” filename
Grep command options:-
1) Grep –i Sachin filename [to ignore case sensitive]
2) Grep –c Filename
3) Grep –n Sachin filename [it prints lines with line numbers containing the given pattern]
4) Grep-l Sachin* [it gives only filename containing given pattern
5) Grep –V “Sachin Sehwag” filename [it prints the line do not containing the given pattern
6) Grep –O Sachin filename [print only the pattern]
Any String contains wildcard character known as regular expression (or) pattern
The Patterns are classified into 3 types
1. Character pattern
The default pattern is character pattern
1)       Grep “Sachin*” filename
2)      Grep “b[aeiou]ll” sample
Output: - Ball, bell, bill, boll, bull,[this word are matched]  bfll [it is wrong]
3)      Grep “b..d” filename
Output: - band, bold, b$#d, bad
Note: - “.” Is a wild card character
2. Word Pattern
4)      Grep “\<Samsung\> filename [starting of the word]
(or)
            Grep-w “samsung” filename
5)      Grep “Samsung\> filename [ending of the word]

3. Line Pattern
^ => Start of the line
$ => end of the line
1). Grep “^d” filename                                 3). Grep “[bkt]” filename
Output: - do, dog, doing                               Output: - b - -, k- -, t- -
2). Grep “^the” filename             4). Grep “^ [^bkt]” filename [if ^ is inside then it means as not]
Output: - the, then, there            Output: - a--, c--, u--
5). Grep “t$” filename [it shows lines ending with ‘T’]
6). Grep “[0-9] $” filename [it shows lines ending with “digit”]
7). Grep “^…$” filename [line having 3 characters]
8) Grep “\$$” filename [displays line ending with $]
9) Grep “^$” filename [it prints no of empty lines
How to delete empty lines from file
10) Grep –v “^$” filename >temp
                Mv temp filename
F Grep [faster Grep]:-
*It is used to search multiple strings but it doesn’t used to search regular expression
*it searches the strings faster than the grep command.
11) Fgrep “unix
                     Perl
                   Oracle Filename
*Every string must separated by enter key
E Grep [extended Grep]:-
It is a combination of grep and f grep plus additional some regular expressions.
Additional Regular Expressions:-
*[;] :- it matches any one string in the given list
E Grep “(unix:oracle:perl)” filename
*{M} :- it matches exact occurrence of it’s proceeding character
Ex: - ab{3}c
Output: - abbbc
Sed [Stream editor]
It is used to replace a string and it is used for multipurpose filter command
1)      Sed “s/oldstring/newstring/g” filename
2)      Sed “s|^$|I like unix|” filename [it will replace all empty lines with “I like unix”
3)      Sed “s|unix||gi” filename [ it delete “unix” word from file]
4)      Sed –n ‘3p’ filename [How to print 3rd row/record/line from file]
5)      Sed –n ‘2,5p’ filename [it prints 2nd record to 5th records from file]
6)      Sed –n ‘$p’ filename [it prints last record from file]
7)      Sed –n ‘1p
$p’ filename [it prints first and last record from file]
8)      Sed ‘3d’ filename [it deletes 3rd record from file]
9)      Sed ‘2,5w file1’ file2 [ it copies 2-5 records from file2 to file1
How to add a header line say “Employee, EmpId” to this file using sed?
Sed -i ‘1a or (1i) Employee, EmpId’ empFile {‘1a’ means after it’ll insert ‘1i’ it’ll insert 1st record, like we can add any record any were}
Print lines which contain the character 'u' or 'x' 
sed -n '/[ux]/p' file
Print every alternate line:
Sed ‘n;d' file  N command prints the current line, and immediately reads the next line into pattern space. D command deletes the line present in pattern space. In this way, alternate lines get printed
To add something to the beginning of a every line in a file, say to add a word Fruit:
sed 's/^/Fruit:/’filename
Similarly, to add something to the end of the file
sed 's/$/Fruit:/’filename
To replace or substitute a particular character, say to replace 'a' with 'A'.
sed 's/a/A/' FileName
To replace only the 2nd occurrence of a character
sed 's/a/A/2g’filename
To replace 'a' only in a specific line say 3rd line, not in the entire file:
sed '3s/a/A/g’filename
To replace or substitute 'a' on a range of lines, say from 1st to 3rd line:
sed '1,3s/a/A/g’filename
Using sed, we can also do multiple substitutions. For example, say to replace all 'a' to 'A', and 'p' to 'P':
sed 's/a/A/g; s/p/P/g’ filename  (or) sed 's/a/A/g’ -e ‘s/p/P/g’ filename
Find sum of all columns / numbers in a line
1, Sed ‘s/ /+/g’ FileName | bc example:- 10 20 30 OutPut:- 60
2, Sed ‘s/,/+/g’ FileName | bc  example:- 10,20,30 OutPut:- 60

Sort:-
*It is used to sort the file contents
*By default it sorts the file contents based on ASCII values
*The default order is ascending order
1) Sort filename
2) sort –r filename (reverse ex:- descending)
3) sort –u filename [it displays unique lines in the file
Unique:-
It displays unique lines in the file but the file contents must be in sorted order
1)       Uniq filename
2)       Uniq –u filename [ it displays non duplicated lines
3)       Uniq –d filename [it displays only duplicated lines
4)       Uniq –c filename [it counts how many times each line repeated in the file]
5)       Uniq –u filename>temp
Mv temp >filename [how to delete duplicated rows fromfile]
Tee:-
It is used to write data to the file as well as to the screen
1)      Cat emp !tee file

No comments:

Post a Comment

happy diwali
happy diwali