FILTER Commands

     FILTER Commands

  • 1. Simple Filters: more,less,head,tail,cut,sort,paste,tr,wc,comm,unique and join
  • 2. Advanced Filters: pipe,grep,sed and awk
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------

FMT


Desc ------>> fmt command is useful to display in single line multiple words into individual records separated by space.


[haritaraka12078@cxln4 ~]$ cat>site.txt
sunday monday tuesday wednesday thursday friday saturday


[haritaraka12078@cxln4 ~]$ fmt -w 1 site.txt
sunday
monday
tuesday
wednesday
thursday
friday
saturday

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------

                                                    CAT


[haritaraka12078@cxln4 ~]$ cat>marks.txt
prabhu-50
hario-40
taraka-60
apple is red
mango is yellow
your dress colour is red
bloack colur suties you perfectly

--------------------------------------------------------------------------------------
Using Hyphen (-) As Delimiter ----------> cut -d- -f(columnNumber) <fileName> 
--------------------------------------------------------------------------------------   
[haritaraka12078@cxln4 ~]$ cut -d- -f2 marks.txt
50
40
60

[haritaraka12078@cxln4 ~]$ cut -d- -f1 marks.txt
prabhu
hario
taraka


--------------------------------------------------------------------------------------
Using Space As Delimiter ----------> cut -d ' ' -f(columnNumber) <fileName>    
--------------------------------------------------------------------------------------
[haritaraka12078@cxln4 ~]$ cut -d ' ' -f2 marks.txt
prabhu-50
hario-40
taraka-60
is
is
dress
colur

[haritaraka12078@cxln4 ~]$ cut -d ' ' -f5 marks.txt
prabhu-50
hario-40
taraka-60


red
perfectly


--------------------------------------------------------------------------------------
Cut by Byte ------>> cut -b <byte number> <file name>  
---------------------------------------------------------------------------------------
[haritaraka12078@cxln4 ~]$ cut -b 2 marks.txt
r
a
a
p
a
o
l



--------------------------------------------------------------------------------------
Cut by charecter ------>>  cut -c < characters> <file name> 
---------------------------------------------------------------------------------------
[haritaraka12078@cxln4 ~]$ cut -c 1,6 marks.txt
pu
h-
ta
a
m
yd
bk


[haritaraka12078@cxln4 ~]$ cut -c 1-6 marks.txt
prabhu
hario-
taraka
apple
mango
your d
bloack


--------------------------------------------------------------------------------------
Cut by Complement Pattern--->>  cut --complement < complement pattern> <file name>  
---------------------------------------------------------------------------------------

[haritaraka12078@cxln4 ~]$ cut --complement -c 1 marks.txt
rabhu-50
ario-40
araka-60
pple is red
ango is yellow
our dress colour is red
loack colur suties you perfectly

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------

COMM 


COMM ----> 'comm' will always display three columns. 
  • First column indicates non-matching items of first file, 
  • second column indicates non-matching items of second file, 
  • third column indicates matching items of both the files. 
Both the files has to be in sorted order for 'comm' command to be executed.



[haritaraka12078@cxln4 ~]$ cat>file1.txt
dhoni
dravid
sachin
sehwag
yuvi


[haritaraka12078@cxln4 ~]$ cat>file2.txt
dhoni
dravid
sachin
zadeja


[haritaraka12078@cxln4 ~]$ comm file1.txt file2.txt
                dhoni
                dravid
                sachin
sehwag
yuvi
        zadeja



-------------------------------------------------
To Display Single Column
-------------------------------------------------
If you want to output a single column, you have to specify number of the columns which are not to be displayed.

Syntax:

  • comm -23 (To display first column)
  • comm -13 (To display second column)
  • comm -12 (To display third column)

[haritaraka12078@cxln4 ~]$ comm -23 file1.txt file2.txt
sehwag
yuvi


[haritaraka12078@cxln4 ~]$ comm -13 file1.txt file2.txt
zadeja


[haritaraka12078@cxln4 ~]$ comm -12 file1.txt file2.txt
dhoni
dravid
sachin

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------
                                                                    GREP

  • GREP - "global regular expression print".generally used with pipe (|).
  • command | grep <searchWord>  
  • Grep with Pipe ->> command | grep <searchWord>  

------------------------------------------------------------------------
Grep with PIPE
------------------------------------------------------------------------
[haritaraka12078@cxln4 ~]$ cat marks.txt | grep 6
taraka-60
sunny-60


[haritaraka12078@cxln4 ~]$ cat marks.txt
prabhu-50
hario-40
taraka-60
apple is red
mango is yellow
your dress colour is red
bloack colur suties you perfectly
sunny-60
mhtp-50


------------------------------------------------------------------------
grep without pipe   ->   grep <searchWord> <file name>  
------------------------------------------------------------------------
[haritaraka12078@cxln4 ~]$ grep 6 marks.txt
taraka-60
sunny-60

[haritaraka12078@cxln4 ~]$ grep -v 6 marks.txt
prabhu-50
hario-40
apple is red
mango is yellow
your dress colour is red
bloack colur suties you perfectly
mhtp-50


[haritaraka12078@cxln4 ~]$ grep -i red marks.txt
apple is red
your dress colour is red


GREP Options
  • grep -A command is used to display the line after the result.
  • grep -B command is used to display the line before the result.
  • grep -C command is used to display the line after and line before the result.


[haritaraka12078@cxln4 ~]$ grep -A1 yellow marks.txt
mango is yellow
your dress colour is red


[haritaraka12078@cxln4 ~]$ grep -B1 yellow marks.txt
apple is red
mango is yellow


[haritaraka12078@cxln4 ~]$ grep -C1 yellow marks.txt
apple is red
mango is yellow
your dress colour is red


DESC:
command "grep -A1 yellow marks.txt" displays searched line with next succeeding line, 
command "grep -B1 yellow marks.txt" displays searched line with one preceding line and 
command "grep -C1 yellow marks.txt" displays searched line with one preceding and succeeding line.


---------------------------------------------------------------------------------------------------------------------------------------------------------------------------

SED

SED ---->
  • Linux 'sed' command stands for stream editor. 
  • It is used to edit streams (files) using regular expressions. 
  • But this editing is not permanent. 
  • It remains only in display, but in actual, file content remains the same.

sed [OPTION]... {script-only-if-no-other-script} [input-file]...


SED options------>

-n: ignore duplicates
-e: search for multiple expressions and print
p -print
s- substitute
d- delete


-----------------------------------------------------------------------------------------------
Applying to the STDIN directory
-----------------------------------------------------------------------------------------------

[haritaraka12078@cxln4 ~]$ echo class7 | sed 's/class/jtp/'
jtp7


[haritaraka12078@cxln4 ~]$ echo class7 | sed 's/7/10/'
class10


[haritaraka12078@cxln4 ~]$ cat marks.txt | sed 's/prabhu/haritarakaprabhu/'
haritarakaprabhu-50


-----------------------------------------------------------------------------------------------
Global Replacement ------> command | sed 's/<oldWord>/<newWord>/g'    
-----------------------------------------------------------------------------------------------

[haritaraka12078@cxln4 ~]$ echo class7 class9 | sed 's/class/jtp/g'
jtp7 jtp9



[haritaraka12078@cxln4 ~]$ cat marks.txt | sed 's/red/redddddddddddd/g'
taraka-60
apple is redddddddddddd
mango is yellow
your dress colour is redddddddddddd


-----------------------------------------------------------------------------------------------
Removing a Line--> cat <fileName> | sed '/<Word>/d'  
-----------------------------------------------------------------------------------------------
[haritaraka12078@cxln4 ~]$ cat file1.txt
dhoni
dravid
sachin
sehwag
yuvi



[haritaraka12078@cxln4 ~]$ cat file1.txt | sed '/dhoni/d'
dravid
sachin
sehwag
yuvi


------------------------------------------------------------------------------------------------------------
Using the Multiple sed Command--------->>sed -e '<script 1> ; <script 2>' <file name> 
------------------------------------------------------------------------------------------------------------

[haritaraka12078@cxln4 ~]$ cat marks.txt
prabhu-50
hario-40
taraka-60
apple is red
mango is yellow
your dress colour is red
bloack colur suties you perfectly
sunny-60
mhtp-50


[haritaraka12078@cxln4 ~]$ sed -e 's/red/blue/; s/yellow/black/' marks.txt
prabhu-50
hario-40
taraka-60
apple is blue
mango is black
your dress colour is blue
bloack colur suties you perfectly
sunny-60
mhtp-50

--------------------------------------------------------------------------------------------------
Reading Commands From a File--------->>sed -f <sed file> <file name> 
-----------------------------------------------------------------------------------------------

[haritaraka12078@cxln4 ~]$ cat>exm.txt
apple is red
mango is yellow
your dress color is red
black color suits you all



[haritaraka12078@cxln4 ~]$ cat>sedcommand
s/red/blue/;
s/yellow/black/;



[haritaraka12078@cxln4 ~]$ sed -f sedcommand exm.txt
apple is blue
mango is black
your dress color is blue
black color suits you all



[haritaraka12078@cxln4 ~]$ sed -n '1p' exm.txt
apple is red


[haritaraka12078@cxln4 ~]$ sed -n '1,3p' exm.txt
apple is red
mango is yellow
your dress color is red



[haritaraka12078@cxln4 ~]$ sed -ne '3p' -ne '4p'  exm.txt
your dress color is red
black color suits you all



-----------------------------------------------------------------------------------------------
Limiting the sed->>
  • A range of lines.
  • A pattern that matches a specific line.
-----------------------------------------------------------------------------------------------

[haritaraka12078@cxln4 ~]$ sed '3s/red/Blue/' exm.txt   
apple is red
mango is yellow
your dress color is Blue
black color suits you all

Desc: --------------->. only the line three is modified.



[haritaraka12078@cxln4 ~]$ sed '1,3s/red/Blue/' exm.txt   
apple is Blue
mango is yellow
your dress color is Blue
black color suits you all

Desc: ------------>>will update the specified text in lines 1 and 3. 



-----------------------------------------------------------------------------------------------
Inserting and Appending Text---------------------------->>>>
-----------------------------------------------------------------------------------------------
[haritaraka12078@cxln4 ~]$ echo "Another Demo" | sed 'i\First Demo'
First Demo
Another Demo



[haritaraka12078@cxln4 ~]$ echo "Another Demo" | sed 'a\First Demo'
Another Demo
First Demo


-----------------------------------------------------------------------------------------------
Modifying Lines-------------->>
-----------------------------------------------------------------------------------------------

[haritaraka12078@cxln4 ~]$ cat exm.txt
apple is red
mango is yellow
your dress color is red
black color suits you all


[haritaraka12078@cxln4 ~]$ sed '3c\This is a modified line.' exm.txt
apple is red
mango is yellow
This is a modified line.
black color suits you all



[haritaraka12078@cxln4 ~]$ sed '/apple is /c Line updated.' exm.txt
Line updated.
mango is yellow
your dress color is red
black color suits you all



-----------------------------------------------------------------------------------------------
Transformation of Characters----------->>>
-----------------------------------------------------------------------------------------------


[haritaraka12078@cxln4 ~]$ sed 'y/abc/def/' exm.txt
dpple is red
mdngo is yellow
your dress folor is red
eldfk folor suits you dll



-----------------------------------------------------------------------------------------------
Printing the Line Numbers--------->>
-----------------------------------------------------------------------------------------------

[haritaraka12078@cxln4 ~]$ sed '=' exm.txt
1
apple is red
2
mango is yellow
3
your dress color is red
4
black color suits you all



[haritaraka12078@cxln4 ~]$ sed -n '/mango/=' exm.txt    -
2

Desc: ------------->> display the line number that contains the word 'mango'



  • sed -n '1p' sedfile( To get only first record)
  • sed -n '1,3p' sedfile(To get first to third record)
  • sed -ne '3p' -ne '5p' sample.txt(To get first, third record)

 substitute
--------------
  • sed 's/abc/hello/p' sedfile
  • sed -ne s/abc/hello/p sedfile
  • sed -ne s/abc/hello/p -e s/venkat/lohith/p  sedfile
  • sed '1wfile_name' sedfile(To write the first record into the file "file_name")
  • sed '1,4w file_name' sedile
  • (To write the first 4 records into the file_name file)
  • sed '3i 500:dfdfdfd:10000' sample.txt (To insert the record after 2nd and before 3rd record)
  • sed '3a 500:dfdfdfd:10000' sample.txt(To insert the record after 3nd and before 4rd record)

  • ls -ltr $path |awk -F " " '{print $9}' |sed '/^$/d'>scriptfile.txt ( To delete any blank records)
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------

 TEE Command

  • Linux tee command is quite similar to the 'cat' command, with only one difference. It puts stdin on stdout and also put them into a file. 
Options:
  • -a, --append: It is used to append the data to the given files, it does not overwrite data.
  • -i, --ignore-interrupts: It is used to ignore the interrupt signals.
  • -p: It is used to diagnose errors writing to non-pipes.
  • --output-error[=MODE]: It is used to set behavior on write error mode.
  • --help: It is used to display the help documentation.
  • --version: it is used to display the version information.

Syntax:
  • tee <options> <file name>  

[haritaraka12078@cxln4 ~]$ cat>weeks.txt
sunday
monday
tuesday
wednesday
thursday
friday
saturday


[haritaraka12078@cxln4 ~]$ cat weeks.txt | tee file1.txt
sunday
monday
tuesday
wednesday
thursday
friday
saturday


-----------------------------------------------------------------------------------------------------------
Write a file and append output
-----------------------------------------------------------------------------------------------------------
The '-a' option is used with the tee command to append the output and write it to a file. Consider the below command:


[haritaraka12078@cxln4 ~]$ echo 'Saturday and Sunday are week offs' | tee -a file1.txt
Saturday and Sunday are week offs


[haritaraka12078@cxln4 ~]$ cat file1.txt
sunday
monday
tuesday
wednesday
thursday
friday
saturday
Saturday and Sunday are week offs


-----------------------------------------------------------------------------------------------------------
Write the State of Data to a File
-----------------------------------------------------------------------------------------------------------
  • Writing the state of data is very useful for taking backup or creating a snap of the data for the debugging purpose. 
  • It can be easily done by using the tee comman.
ls ~/ | tee pipe1.txt | grep ^b | tee pipe2.txt | sort -r  


[haritaraka12078@cxln4 ~]$ ls ~/ | tee file1.txt | grep ^b | tee weeks.txt | sort -r
[haritaraka12078@cxln4 ~]$ cat file1.txt
abc3
cloudxlab_jupyter_notebooks
dir1
erro-log
exm.txt
file1.txt
file2.txt
hari_dir
hari_training
marks.txt
sedcommand
site.txt
tee4
users-info
weeks.txt



-----------------------------------------------------------------------------------------------------------
Write to multiple files
-----------------------------------------------------------------------------------------------------------
  • To write to the multiple files, specify the names of the files after the tee command as follows:
  • echo " add text" | tee file1.txt file2.txt file3.txt  

[haritaraka12078@cxln4 ~]$ echo " file created for TEE command" | tee teefile1.txt teefile2.txt teefile3.txt
 file created for TEE command


[haritaraka12078@cxln4 ~]$ cat teefile1.txt teefile2.txt teefile3.txt
 file created for TEE command
 file created for TEE command
 file created for TEE command


-----------------------------------------------------------------------------------------------------------
Write to a privileged file
-----------------------------------------------------------------------------------------------------------
  • The tee command allows us to write to a file having sudo privilege.
  •  If we try to write a file owned by the root user will through the permission error. 
 But, we can elevate the sudo permission by executing the tee command as follows:

[haritaraka12078@cxln4 ~]$ echo "some text for privileged file" | sudo tee -a teefile6.txt



-----------------------------------------------------------------------------------------------------------
Ignoring Interrupts
-----------------------------------------------------------------------------------------------------------
The '-i' option is used to ignore the interrupts. 
  • command | tee -i teefile3.txt  
Hide the output
To restrict the tee command not to write to the standard output, redirect it to "/dev/null".

[haritaraka12078@cxln4 ~]$ echo "Text" | tee teefile7.txt >/dev/null

[haritaraka12078@cxln4 ~]$ cat teefile7.txt
Text


---------------------------------------------------------------------------------------------------------------------------------------------------------------------------

 'TR'  Command


The command 'tr' stands for 'translate'. It is used to translate, like from lowercase to uppercase and vice versa or new lines into spaces.

  1. command | tr <'old'> <'new'>  


[haritaraka12078@cxln4 ~]$ cat exm.txt | tr 'prcu' 'PRCU'
aPPle is Red
mango is yellow
yoUR dRess ColoR is Red
blaCk ColoR sUits yoU all

Note: all p,r,c,u are converted into upprecase P,R,C,U.



-----------------------------------------------------------------------------------------------------------
Remove New Lines
-----------------------------------------------------------------------------------------------------------
To write all the lines into a single line we have to translate all new lines into spaces.
  1. command | tr <'\n'> <' '>  


[haritaraka12078@cxln4 ~]$ cat>jtp.txt
hellllllllllllllllllooooooooooooo
allllllllllllllll

[haritaraka12078@cxln4 ~]$ cat jtp.txt | tr -s 'l'
helooooooooooooo
al


[haritaraka12078@cxln4 ~]$ cat>spaces.tst
a   b    c   d
e      f       g     h

[haritaraka12078@cxln4 ~]$ cat spaces.tst | tr -s ' '
a b c d
e f g h



-----------------------------------------------------------------------------------------------------------
tr Options
----------------------------------------------------------------------------------------------------------


tr -s : The 'tr -s' command squeezes the occurence of multiple characters into one.

    Syntax:  command | tr -s <'letter'>  


tr rot13: This command encrypts the text. It is case-sensitive

[haritaraka12078@cxln4 ~]$ cat exm.txt | tr 'a-z' 'nopqrstuvwxyzabcdefghijklm'
nccyr vf erq
znatb vf lryybj
lbhe qerff pbybe vf erq
oynpx pbybe fhvgf lbh nyy


[haritaraka12078@cxln4 ~]$ cat exm.txt | tr 'a-z' 'n-za-m'
nccyr vf erq
znatb vf lryybj
lbhe qerff pbybe vf erq
oynpx pbybe fhvgf lbh nyy
[haritaraka12078@cxln4 ~]$



tr -d: The 'tr -d' command is used to delete characters.


[haritaraka12078@cxln4 ~]$ cat exm.txt | tr -d o
apple is red
mang is yellw
yur dress clr is red
black clr suits yu all


---------------------------------------------------------------------------------------------------------------------------------------------------------------------------

 'UNIQ'  Command


Linux uniq command is used to remove all the repeated lines from a file. Also, it can be used to display a count of any word, only repeated lines, ignore characters, and compare specific fields. It is one of the most frequently used commands 

in the Linux system. It is often used with the sort command 

Syntax:

  1. uniq [OPTION]... [INPUT [OUTPUT]]  

Options:

  • -c, --count: it prefixes the lines by the number of occurrences.
  • -d, --repeated: it is used to print duplicate lines, one for each group.
  • -D: It is used to print all the duplicate lines.
  • --all-repeated[=METHOD]: It is quite similar to the '-D' option, the difference between both the options is that it allows separation of groups with an empty line.
  • -f, --skip-fields=N: It is used to avoid comparison of the first N fields.
  • --group[=METHOD]: It is used to display all items and separates the groups with an empty line.
  • -i, --ignore-case: It is used to ignore the differences while comparing.
  • -s, --skip-chars=N: It is used to avoid the comparison of the first N characters.
  • -u, --unique: it is used to print unique lines.
  • -z, --zero-terminated: It is used for the line delimiter is NUL and not newline mode.
  • -w, --check-chars=N: It is used to compare not more than N characters in lines.
  • --help: It is used to display help documentation.
  • --version: It is used to display the version information.


[haritaraka12078@cxln4 ~]$ cat weeks.txt

sunday
tuesday
wednesday
saturday
sunday
tuesday
wednesday
saturday


-----------------------------------------------------------------------------------------------------------
Remove repeated lines
-----------------------------------------------------------------------------------------------------------
[haritaraka12078@cxln4 ~]$ sort weeks.txt | uniq
saturday
sunday
tuesday
wednesday


-----------------------------------------------------------------------------------------------------------
Count the number of occurrences of a word
-----------------------------------------------------------------------------------------------------------
[haritaraka12078@cxln4 ~]$ sort weeks.txt | uniq -c
      2 saturday
      2 sunday
      2 tuesday
      2 wednesday



-----------------------------------------------------------------------------------------------------------
Display the repeated lines
-----------------------------------------------------------------------------------------------------------
[haritaraka12078@cxln4 ~]$ sort weeks.txt | uniq -d
saturday
sunday
tuesday
wednesday



-----------------------------------------------------------------------------------------------------------
Display the unique lines
-----------------------------------------------------------------------------------------------------------
The '-u' option is used to display only the unique lines ( which are not repeated). It will only display the lines that occur only once and write the result to standard output. Consider the below command:

sort dupli.txt | uniq -u  



-----------------------------------------------------------------------------------------------------------
Ignore characters in comparison
-----------------------------------------------------------------------------------------------------------
The '-s' option is used to ignore the characters in comparison. It will ignore the specified number of characters 

[haritaraka12078@cxln4 ~]$ sort weeks.txt | uniq -s 2
saturday
sunday
tuesday
wednesday


-----------------------------------------------------------------------------------------------------------
Ignore fields in comparison
-----------------------------------------------------------------------------------------------------------
The '-f' option is used to ignore the field

[haritaraka12078@cxln4 ~]$ uniq -f 2 weeks.txt
sunday



---------------------------------------------------------------------------------------------------------------------------------------------------------------------------

 'WC'  Command



  • Linux wc command helps in counting the lines, words, and characters in a file.
  •  It displays the number of lines, number of characters, and the number of words in a file. 
  •  Mostly, it is used with pipes for counting operation.
 
 Options:


  • -c, --bytes: It is used to print the byte counts.
  • -m, --chars: It is used to print the character counts.
  • -l, --lines: It is used to print the newline counts.
  • --files0-from=F: It is used to read input from specified files.
  • -L, --max-line-length: It is used to print the maximum display width.
  • -w, --words: It is used to print the word counts.
  • --help: It is used to display the help manual.
  • --version: It is used to display the version information.


----------------------------------------------------------------------------
---------->>Display count information of a file
----------------------------------------------------------------------------
[haritaraka12078@cxln4 ~]$ wc exm.txt
 4 16 79 exm.txt

Note:
4-no of lines
16- no of words
79 - no of bytes



----------------------------------------------------------------------------
---------------->> Multiple files count
----------------------------------------------------------------------------
[haritaraka12078@cxln4 ~]$ wc exm.txt marks.txt
  4  16  79 exm.txt
  9  21 134 marks.txt
 13  37 213 total



----------------------------------------------------------------------------
---------->> no of lines
----------------------------------------------------------------------------
[haritaraka12078@cxln4 ~]$ wc -l exm.txt
4 exm.txt


----------------------------------------------------------------------------
----> no of charecters
----------------------------------------------------------------------------
[haritaraka12078@cxln4 ~]$ wc -m exm.txt
79 exm.txt



----------------------------------------------------------------------------
----->> no of bytes
----------------------------------------------------------------------------
[haritaraka12078@cxln4 ~]$ wc -c exm.txt
79 exm.txt



----------------------------------------------------------------------------
- no of charcters
----------------------------------------------------------------------------

[haritaraka12078@cxln4 ~]$ wc -w exm.txt
16 exm.txt



----------------------------------------------------------------------------
- no of files in directory
----------------------------------------------------------------------------
[haritaraka12078@cxln4 ~]$ ls | wc -l
17


[haritaraka12078@cxln4 ~]$ wc -L exm.txt
25 exm.txt



---------------------------------------------------------------------------------------------------------------------------------------------------------------------------

 'OD'  Command


  • The 'od' term stands for octal dump. 
  • It displays content of a file in different human-readable formats like hexadecimal, octal and ASCII characters.


Syntax: 

od -b <fileName>      (display files in octal format)  
od -t x1 <fileName>       (display files in hexadecimal bytes format)  
od -c <fileName>      (display files in ASCII (backslashed) character format)  



[haritaraka12078@cxln4 ~]$ cat format.txt
abcdefghi
123456789



[haritaraka12078@cxln4 ~]$ od -b format.txt
0000000 141 142 143 144 145 146 147 150 151 012 061 062 063 064 065 066
0000020 067 070 071 012
0000024



[haritaraka12078@cxln4 ~]$ od -t x1 format.txt
0000000 61 62 63 64 65 66 67 68 69 0a 31 32 33 34 35 36
0000020 37 38 39 0a
0000024



[haritaraka12078@cxln4 ~]$ od -c format.txt
0000000   a   b   c   d   e   f   g   h   i  \n   1   2   3   4   5   6
0000020   7   8   9  \n
0000024


---------------------------------------------------------------------------------------------------------------------------------------------------------------------------

 'SORT  Command


  • The 'sort' command sorts the file content in an alphabetical order


[haritaraka12078@cxln4 ~]$ cat name.txt
hari prabhu
manyam taraka
siva teja
sunny ayyappa
lucky pinky



[haritaraka12078@cxln4 ~]$ sort name.txt
hari prabhu
lucky pinky
manyam taraka
siva teja
sunny ayyappa


--------------------------------------------------------------------------------------
---To Sort A Column
--------------------------------------------------------------------------------------
[haritaraka12078@cxln4 ~]$ sort -k1 name.txt
hari prabhu
lucky pinky
manyam taraka
siva teja
sunny ayyappa



[haritaraka12078@cxln4 ~]$ sort -k2 name.txt
sunny ayyappa
lucky pinky
hari prabhu
manyam taraka
siva teja



--------------------------------------------------------------------------------------
-------------Numeric Sorting
--------------------------------------------------------------------------------------
[haritaraka12078@cxln4 ~]$ sort -n -k2 mark.txt
tarka 5
prabhu 10
sunny 25
hari 50





---------------------------------------------------------------------------------------------------------------------------------------------------------------------------

 'GZIP  Command


  • Gzip (GNU zip) is a compressing tool, which is used to truncate the file size. By default original file will be replaced by the compressed file ending with extension (.gz).
  • To decompress a file you can use gunzip command and your original file will be back.

Syntax:

gzip <file1> <file2> <file3>. . .   
gunzip <file1> <file2> <file3>. . .   



Example:

gzip file1.txt file2.txt  
gunzip file1.txt file2.txt  


-------------------------------------------------------
More than one file compressing :
-------------------------------------------------------
cat file1.txt file2.txt | gzip > final.gz   



-------------------------------------------------------
gzip -i -->  compression ratio or how much the original file has compressed.
-------------------------------------------------------
gzip -l final.gz jtp.txt.gz    



-------------------------------------------------------
How To Compress A Directory
-------------------------------------------------------
  • The gzip command will not be able to compress a directory because it can only compress a single file. 
  • To compress a directory you have to use 'tar' command.
  • Hyphen (-) is not mandatory in 'tar' command.

Options:
  • 'c' is to create,
  • 'v' is for verbose, to display output,
  • 'f' to mention destination of your output file,
  • 'z' for specifying compress with gzip.


Syntax:

tar cf - <directory> | gzip > <directoryName>  

  • tar cf - office | gzip > office.tar.gz