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?
- ? The preceding item is optional and matched at most once.
- * matched zero or more times.
- + matched one or more times.
- {n} matched exactly n times.
- {n,} matched n or more times.
- {,m} matched at most m times.
- {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?
- nice/renice: Runs a program with modified scheduling priority.
- netstat: Prints network connections, routing tables and interface statistics.
- time: give resource usage
- uptime: This is System Load Average.
- ps: Current process list
- vmstat: Virtual Memory stats
- 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 lpr.info /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]