Linux Commands



grep -  it is a command line utility for searching plain text data sets for lines matching  a regular expression. Grep was developed by  Ken Thompson.

syntax:           grep [options] pattern [files]

Options
Option
Description
-b
Display the block number at the beginning of each line.
-c
Display the number of matched lines.
-h
Display the matched lines, but do not display the filenames.
-i
Ignore case sensitivity.
-l
Display the filenames, but do not display the matched lines.
-n
Display the matched lines and their line numbers.
-s
Silent mode.
-v
Display all lines that do NOT match.
-w
Match whole word.

         e.g     grep imocha student.txt
                  above code will search all the line which contain 'imocha' in the file student.txt
here the grep is case sensitive. if you want to ignore then use -i
       e.g.      grep -i imocha student.txt
               the above code will find the word imocha and IMOCHA in the file student.txt

Options in grep



egrep -  it is an acronym for " Extended Global Regular Expression Print". it is used to scan the file line by line and it returns the pattern matching a given expression.
  syntax: egrep <flag> '< regular expression>' <filename>

flag:  -c    ---- counting the number of successful matches and not printing the actual matches.
        -i     ---- make the search case insensitive.
        -n    ---- to print the number before each match printout.
       -v    ---- returns the line which don't match.

ls  - it is use to list the files or directory

rm - it is used to remove file or directory
       to remove a file
       rm filename 
       to remove a directory
       rm -r directoryname 


sed - it is an stream editor, but it is unlike others because it is not a screen oriented,centos
 it is not interactive.
This means we have to insert the commands to be executed on the data at the command     line or in  a script to pe processed.
        sed accept series of commands and execute them on a file ( or set of files).
   
How sed works
       The sed utility works by sequentially reading a file, line by line, into the memory. It then performs all actions specified for the line and places the line back in memory to dump to the terminal with the requested changes made. After all actions have taken place to this one line, it reads the next line of the file and repeats the process until it is finished with the file.

         syntax: sed [option] '[command]' [filename]

example:  sed  's/mail/email/' filename
             here all the 'mail' word will be replace by 'email' word in the file.
        
 sort - it is used for sorting
          ls -l | sort -k1
   it will will sort the list of files based on first column (-k1)

tee -  it is used to read the standard input and write it to standard output as well as into one or more files.
         ls | tee filename

wc -  it is use to count the number of lines,word or characters in a file.
       Suppose if we execute the command as
          wc filename
      5 10 85 filename
      5 ---- number of lines in the file
      10 ---- number of word in the file
      85 ---- number of characters in the file.

   if you want only the line or only the character or only the words then
     wc -c filename ---- for characters
     wc -l filename ----- for line number
     wc -w filename ---- for words

1. How to find the occurrence  of a word in a file?

     grep -c "search_word" filename

2. List the lines in file.txt containing the word 'hello'
 
     egrep -i 'hello' file.txt

3. How to display only the directories in linux?
  
     ls -l | egrep '^d'
     
     ^d -- it is used to find the character d at the beginning of each line.
     if you execute ls -l it display like 


as we see first characters of each line is - and d.
so if we execute the  ls -l | egrep '^d'  it will display only the lines which having 'd' as a first character in each line.


4. How to list only the files?
   ls -l | egrep -v '^d'
or 
   ls -al | grep '^-'
  this will list all the files including the hidden files   

5.  How to rename a directory?
     mv oldfilename newfilename

6.  How to display the 5th line of a file?
    head -5 filename | tail -1

7. How to delete a line from a file?
    sed -i '3d' filename
8. How to remove the footer from a file?
sed -i '$ d' filename

9. How to get the 5th word of a line in Unix?
awk '{print $5}' filename

10 Print the 2nd and 5th line from a file.
sed -n '2p;5p' filename
or
awk 'NR==2|| NR==5' filename

11 Print alternate lines of a file
awk 'NR%2==0' filename
or
cat filename | awk 'NR%2==0'

12. Display the last column form a file

cat filename | rev | awk '{print $1}'| rev

or
cat filename | rev | cut -f1 -d ' '| rev

13. Replace the nth line by a new line

 sed -i '' '4d' filename
sed -i'' ' 4i new_line ' filename
where 4. is the line number
d means delete
i means insert

14. Convert all the text in a file into uppercase letter.

    awk '{ print toupper ($0) }' filename > new_filename
or
dd if=filename of=new_filename conv=ucase
or
tr '[:lower:]' '[:upper:]' <filename> new_filename

15. Convert all the text in a file into lowercase letter.

    awk '{ print tolower($0) }' filename > new_filename
or
dd if=filename of=new_filename conv=lcase
or
tr  '[:upper:]' '[:lower:]' <filename> new_filename
or
y=$(<filename)
echo "${y^^)"


15. Find the newest file in the directory.
 ls -ltr | tail -1

15.1. Find the newest directory
ls -ltr | grep ^d | tail -1

16. What is difference bewteen $#, $@ and $?  
$# it is used to returned the number of argument (parameter)
$@ it is used to get the parameters passed
$? it is used to find the status of the previous executed command. if the command was success full then it will return otherwise it will return other number.  

17. Print the content of a file with the line number
cat -n filename
 or
grep -n '^' filename
or
awk -F':' '{ print NR, $1} filename

18. Print a listing of files in directory.
ls | grep -v "files">files.txt

19. How to find the difference between two files?
diff file1 file2
or
sdiff file1 file2

20. Count the number of files in a directory.
ls | wc -l

21. List the files which 5 days old.

find . -name "*" -mtime -5 -print

22. List all the .csv files in a directory
find . -name "*.csv"

23. How to insert a new line after 4th line in a file

sed '4 a\Your line' filename

24. Find the file based on the month
 
ls -l | grep monthname
 
here filename should be Jan, Feb or first character or the month like J,F,M etc. 
 
25. Convert tab delimited file into comma delimited file

sed 's/t\/,/g' input_filename>output_filename

26. Print a lower case string into uppercase
echo "hello" | awk '{print toupper($1)}' 

27. Display a file by removing white spaces.
cat <filename> | sed 's/ //g'

  28. Create a file by removing unwanted string from an existing file.
cat <filename> | sed 's/string_to_remove//g' > <newfilename> .

29. Count he blank line in a file
grep -cPv '\S' <filename>
-c is to count
-v is to find the blank line
-P '\S' is to find the which has non-space

30. Print the file by removing blank line
grep -P '\S' <filename>

31. Print the file by removing blank line

tail -n+2 <filename> or sed -n '2,$p' emp

32. Calculate the difference of two time

startTime=$(date +%s) endTime=$(date +%s) differentTime=$(($endTime - $startTime)) printf 'Time difference %dh:%dm:%ds\n' $(($differentTime/3600)) $(($differentTime%3600/60)) $(($differentTime%60))

No comments:

Post a Comment