Friday 1 September 2017

Unix Shell Scripting Tutorial - page 6

Unix Shell Scripting Tutorial - page 6

Question: How can we search in file using Regular expression?
We use grep command.
grep "search string" filename.txt

Question: How to search a string in file?
grep "search string" filename.txt

search string is text searched in filename.txt. By Default its case sensitive.

Question: How to search a case-insensitive string in file?
use -i to search case insensitive.
grep -i "search string" filename.txt

Question: How to search a string in mutiple file?
separate the filename by space.
grep -i "search string" filename.txt filename2.txt filename3.txt 

Question: How to search a string in file using regular expression? Use regex character for example .* used to search all
grep -i "textname .*" filename.txt 

It will return all after the textname.

Question: What are the basic Regex characters?
  1. ? The preceding item is optional and matched at most once.
  2. * matched zero or more times.
  3. + matched one or more times.
  4. {n} matched exactly n times.
  5. {n,} matched n or more times.
  6. {,m} matched at most m times.
  7. {n,m} matched at least n times, but not more than m times.

Question: How to search a words in file?
Use -w to search words.
grep -iw "textname" filename.txt 

It will not search textnameA.

Question: How to search and return next line?
grep -A 3 -i "textname" filename.txt

It will search and return next 3 lines after search.
You can use -B to return Before 3 lines.

Question: How to search recursively in folder? Use -r to search recursive.
grep -r "textname" 

It will search in current folder and sub-folder.

Question: How to find number of matches ? Use -c to return number of matches.
grep -c "textname" fiename.txt

It will return number of matches in number.

Question: What are the Performance Tools in unix?
  1. nice/renice: Runs a program with modified scheduling priority.
  2. netstat: Prints network connections, routing tables and interface statistics.
  3. time: give resource usage
  4. uptime: This is System Load Average.
  5. ps: Current process list
  6. vmstat: Virtual Memory stats
  7. top: Top Task List

Question: What is syslog?
Syslog is a way for network devices to send event messages to a server. Also known as Syslog server.
SysLog is stored in /etc/syslogd OR /etc/syslog.
syslog.conf have following configuration.
*.err;kern.debug;auth.notice /dev/console
daemon,auth.notice           /var/log/messages                     /var/log/lpr.log
mail.*                       /var/log/mail.log
ftp.*                        /var/log/ftp.log
mark.*                       /dev/console

Question: What is command for logging?
logger [-i] [-f file] [-p priority] [-t tag] [message]