{"id":3634,"date":"2014-10-09T10:18:22","date_gmt":"2014-10-09T02:18:22","guid":{"rendered":"http:\/\/rmohan.com\/?p=3634"},"modified":"2014-10-09T11:05:08","modified_gmt":"2014-10-09T03:05:08","slug":"log-rotation-in-catalina-out","status":"publish","type":"post","link":"https:\/\/mohan.sg\/?p=3634","title":{"rendered":"Log Rotation in Catalina.out"},"content":{"rendered":"<p><strong>1. Remove all logs of manager&amp; admin<\/strong><\/p>\n<ol start=\"2\">\n<li><strong> Remove 90 days old logs\u00a0<\/strong><\/li>\n<li><strong> Backup logs greater that 35MB<\/strong><\/li>\n<\/ol>\n<p>#!\/bin\/bash<\/p>\n<p>rm -rf \/app\/tomcat-6.0.18\/logs\/*manager* \/app\/tomcat-6.0.18\/logs\/*admin*<\/p>\n<p>find \/cda1-logs-backups -name &#8216;*.gz&#8217; -mtime +90 -exec rm {} \\;<\/p>\n<p>echo &#8220;Initiating logrotate `date +%F-%A`&#8221; &gt;&gt; \/app\/tomcat-6.0.18\/logs\/diskutil.log<\/p>\n<p>cd \/app\/tomcat-6.0.18\/logs\/<\/p>\n<p>siz_catalina=`\/bin\/ls -l catalina.out | \/bin\/awk &#8216;{ print $5 }&#8217;`<\/p>\n<p>if [ $siz_catalina -ge &#8220;3600000&#8221; ];then<\/p>\n<p>echo &#8220;catalina.out greater that 35MB, tarring the catalina.out file as catalina.out.`date +%F-%I-%M-%p` and nullifieng the file&#8221; &gt;&gt; diskutil.log<\/p>\n<p>tar -cvzf \/cda1-logs-backups\/catalina.out.`date +%F-%I-%M-%p`.tar.gz catalina.out<\/p>\n<p>if [ -s \u00a0\/cda1-logs-backups\/catalina.out.`date +%F-%I-%M-%p`.tar.gz ]; then<\/p>\n<p>`:&gt;catalina.out`<\/p>\n<p>fi<\/p>\n<p>else<\/p>\n<p>echo &#8220;Catalina Log file less than 35 MB at `date +%F-%I-%M-%p`&#8221; &gt;&gt; diskutil.log<\/p>\n<p>fi<\/p>\n<ol start=\"4\">\n<li>Backup one day old logs<\/li>\n<\/ol>\n<p>if [ -s catalina.`date +%F -d &#8220;-1 day&#8221;`*.log ]<\/p>\n<p>then<\/p>\n<p>tar -zcvf \/cda1-logs-backups\/catalina.`date +%F -d &#8220;-1 day&#8221;`*.log.tar.gz catalina.`date +%F -d &#8220;-1 day&#8221;`*.log<\/p>\n<p>rm -f catalina.`date +%F -d &#8220;-1 day&#8221;`.log<\/p>\n<p>echo &#8221; Backup of catalina.`date +%F -d &#8220;-1 day&#8221;`*.log taken and removed &#8221; &gt;&gt; logrotate<\/p>\n<p>else<\/p>\n<p>echo &#8220;catalina.`date +%F -d &#8220;-1 day&#8221;`*.log doesnt exist &#8221; &gt;&gt; logrotate<\/p>\n<p>fi<\/p>\n<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;<\/p>\n<p><strong>#&#8211; Removing blank localhost_access files<\/strong><\/p>\n<p>30 1 * * * find \/usr\/local\/apache-tomcat-6.0.18\/logs -name &#8220;localhost_access*&#8221; -type f -size 0c | xargs rm<\/p>\n<p>30 1 * * * find \/usr\/local\/apache-tomcat-6.0.18\/logs -name &#8220;manager*&#8221;\u00a0 -type f -size 0c | xargs rm<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p><strong>#&#8211; Removing blank localhost_access files<\/strong><\/p>\n<p>30 1 * * * find \/usr\/local\/apache-tomcat-6.0.18\/logs -name &#8220;localhost_access*&#8221; -type f -size 0c | xargs rm<\/p>\n<p>30 1 * * * find \/usr\/local\/apache-tomcat-6.0.18\/logs -name &#8220;manager*&#8221;\u00a0 -type f -size 0c | xargs rm<\/p>\n<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;<\/p>\n<p><strong>Count Apache &amp; tomcat connections<\/strong><\/p>\n<p># crontab \u00a0-e<\/p>\n<p>*\/1 * * * * \u00a0 \u00a0 \/home\/rack\/countdbconnections.sh &gt;&gt; \/home\/rack\/countdbconnection<\/p>\n<p># vi countdbconnections.sh<\/p>\n<p>#!\/bin\/bash<\/p>\n<p>apache_con=`ps -ef | grep apache | wc -l`<\/p>\n<p>tomcat_total_con=`netstat -an | grep :7001 | wc -l`<\/p>\n<p>tomcat_est=`netstat -an | grep :7001 | grep ESTABLISHED | wc -l`<\/p>\n<p>tomcat_wait=`netstat -an | grep :7001 | grep TIME_WAIT | wc -l`<\/p>\n<p>echo &#8220;$(date): apache_conn = $apache_con, tomcat_established_conn = $tomcat_est, tomcat_time_wait = $tomcat_wait&#8221;<\/p>\n<p># less countdbconnection<\/p>\n<p>Wed Jul 18 12:18:01 BST 2012: apache_conn = 33, tomcat_established_conn = 11, tomcat_time_wait = 809<\/p>\n<p>Wed Jul 18 12:19:02 BST 2012: apache_conn = 38, tomcat_established_conn = 22, tomcat_time_wait = 690<\/p>\n<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;<\/p>\n<p><strong>Monitor Java CPU and System load avg<\/strong><\/p>\n<p># crontab -e<\/p>\n<p>*\/2 * * * * \u00a0 \u00a0 \/root\/scripts\/monitor_pcpu_cda_per.sh &gt;&gt; \/root\/scripts\/monitor_pcpu_cda_per.out<\/p>\n<p># vi monitor_pcpu_cda_per.sh<\/p>\n<p>java_cpu=&#8221;$(date): Java CPU: $(top -b -n1 \u00a0| grep java | head -1 | awk {&#8216;print $9&#8217;})&#8221;<\/p>\n<p>server_load=&#8221;Load: $(w | head -1 | awk {&#8216;print $10&#8217;})&#8221;<\/p>\n<p>echo $java_cpu $server_load<\/p>\n<p># less monitor_pcpu_cda_per.out<\/p>\n<p>Fri Jul 20 06:58:02 BST 2012: Java CPU: 5.9 Load: 0.11,<\/p>\n<p>Fri Jul 20 07:00:02 BST 2012: Java CPU: 96.0 Load: 0.17,<\/p>\n<p>Fri Jul 20 07:02:01 BST 2012: Java CPU: 0.0 Load: 0.36,<\/p>\n<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;<\/p>\n<p>Restart tomcat if it is down and count open files<\/p>\n<p># vi \/root\/scripts\/tomcatrestart.sh<\/p>\n<p>ps -ef | grep &#8220;\/rss\/tomcat-6.0.18\/&#8221; | grep -v grep<\/p>\n<p>if [ $? -eq 0 ]; then<\/p>\n<p>Openfiles=`\/usr\/sbin\/lsof | wc -l`<\/p>\n<p>echo &#8220;checked at time `date` and rss is up, Openfiles=$Openfiles&#8221; &gt;&gt; \/root\/scripts\/tomcatcheck.out<\/p>\n<p>else<\/p>\n<p>\/bin\/sh \/rss\/tomcat-6.0.18\/bin\/startup.sh<\/p>\n<p>echo &#8220;&lt;&lt;&lt;&lt;&lt;&lt; RSS is down, restarted it at `date` &gt;&gt;&gt;&gt;&gt;&gt;&gt;&#8221; &gt;&gt; \/root\/scripts\/tomcatcheck.out<\/p>\n<p>mail -s &#8220;Restarted RSS @ web2-prod&#8221; harora4@sapient.com,vsrivastav4@sapient.com<\/p>\n<p>fi<\/p>\n<p>if [ $Openfiles -ge 10000]; then<\/p>\n<p>echo &#8220;OpenFiles @ `hostname` reached its limit of $Openfiles&#8221; | mail -s &#8220;`hostname`: OpenFiles=$Openfiles&#8221; harora4@sapient.com,vsrivastav4@sapient.com<\/p>\n<p>else<\/p>\n<p>cat \/dev\/null<\/p>\n<p>fi<\/p>\n<p># less tomcatcheck.out<\/p>\n<p>checked at time Wed Mar 14 15:44:03 GMT 2012 and rss is up, Openfiles=24779<\/p>\n<p>checked at time Wed Mar 14 15:45:03 GMT 2012 and rss is up, Openfiles=21161<\/p>\n<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;<\/p>\n<p># Script to be used for Removal of Cached Memory<\/p>\n<p>#!\/bin\/bash<\/p>\n<p>CACHE_MEM=`free -m |awk &#8216;NR==2&#8217; |awk &#8216;{ print$7 }&#8217;`<\/p>\n<p>LIMIT=2000<\/p>\n<p>if [ &#8220;$CACHE_MEM&#8221; -gt &#8220;$LIMIT&#8221; ]<\/p>\n<p>then<\/p>\n<p>`sync; echo 3 &gt; \/proc\/sys\/vm\/drop_caches`<\/p>\n<p>mail -s &#8220;CACHE_MEM is now cleared and the value was ##### $CACHE_MEM #####&#8221; vsrivastav4@sapient.com<\/p>\n<p>else<\/p>\n<p>echo exiting &gt; \/dev\/null<\/p>\n<p>fi<\/p>\n<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;<\/p>\n<p>Restart Tomcat if connections goes high<\/p>\n<p>curl -v http:\/\/262670-web1-prod.visitbritain.com:7001\/cs\/Satellite\/en_EN\/ &gt; \/root\/scripts\/connstatus.out<\/p>\n<p>count=$(cat \/root\/scripts\/connstatus.out | wc -l)<\/p>\n<p>if [ $count -gt 200 ]; then<\/p>\n<p>echo &#8220;checked at time `date` and rss is up and responding, Openfiles=`\/usr\/sbin\/lsof | wc -l`&#8221; &gt;&gt; \/root\/scripts\/check_8Sep<\/p>\n<p>else<\/p>\n<p>netstat -alpn &gt; \/root\/scripts\/connectionstat_`date +%F-%I-%M-%p`<\/p>\n<p># taking thread dump two times<\/p>\n<p>pgrep -f \/rss\/tomcat-6.0.18\/temp | xargs kill -3<\/p>\n<p>pgrep -f \/rss\/tomcat-6.0.18\/temp | xargs kill -3<\/p>\n<p># kill tomcat process<\/p>\n<p>pgrep -f \/rss\/tomcat-6.0.18\/temp | xargs kill -9<\/p>\n<p>\/bin\/sh \/rss\/tomcat-6.0.18\/bin\/startup.sh<\/p>\n<p>fi<\/p>\n<p>rm -f \/root\/scripts\/connstatus.out<\/p>\n<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;<\/p>\n<p>Checking Disk Space and send alert<\/p>\n<p>#!\/bin\/sh<\/p>\n<p>limit=80<\/p>\n<p>df -H \u00a0| grep &#8221; \/&#8221; | grep -vE &#8216;^Filesystem|tmpfs|cdrom|boot&#8217; | awk &#8216;{ print $4 &#8221; &#8221; $5 }&#8217;| while read i;<\/p>\n<p>do<\/p>\n<p>perc=$(echo $i | awk &#8216;{ print $1}&#8217; | cut -d&#8217;%&#8217; -f1)<\/p>\n<p>part=$(echo $i | awk &#8216;{ print $2 }&#8217; )<\/p>\n<p>if [ $perc -ge $limit ]; then<\/p>\n<p>echo &#8220;Running out of space \\&#8221;$part ($perc%)\\&#8221; on $(hostname) as on $(date)&#8221; | mail -s &#8220;Alert: Almost out of disk space $usep&#8221; vsrivastav4@sapient.com<\/p>\n<p>fi<\/p>\n<p>done<\/p>\n<p>2nd Method<\/p>\n<p>df -h | grep -e [8-9][0-9]%<\/p>\n<p>if [ `echo $?` -ne 0 ]; then<\/p>\n<p>exit<\/p>\n<p>else<\/p>\n<p>df -h | grep -e [8-9][0-9]% -e Use% | mail -s &#8220;Disk space more than 80%&#8221; \u00a0arora4@sapient.com\u00a0-c vsrivastav4@sapient.com<\/p>\n<p>fi<\/p>\n<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;<\/p>\n<p>Script if 15 minutes load goes &gt;= 6.0.<\/p>\n<p>#!\/bin\/bash<\/p>\n<p>NOTIFY=&#8221;6.0&#8243;<\/p>\n<p>FTEXT=&#8217;load average:&#8217;<\/p>\n<p># 15 min<\/p>\n<p>F15M=&#8221;$(uptime | awk -F &#8220;$FTEXT&#8221; &#8216;{ print $2 }&#8217; | cut -d, -f3)&#8221;<\/p>\n<p>RESULT=$(echo &#8220;$F15M &gt; $NOTIFY&#8221; | bc)<\/p>\n<p># if load &gt;= 6.0 create a file \/tmp\/file.txt<\/p>\n<p>if [ &#8220;$RESULT&#8221; == &#8220;1&#8221; ]; then<\/p>\n<p>echo &#8216;LOAD ISSUE&#8217;&gt;\/tmp\/file.txt<\/p>\n<p>fi<\/p>\n<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;<\/p>\n<p>Rename files .txt to .new in a directory<\/p>\n<p>#!\/bin\/bash<\/p>\n<p>echo &#8220;Enter Directory&#8221;<\/p>\n<p>read dir<\/p>\n<p>for i in `ls $dir\/*`<\/p>\n<p>do<\/p>\n<p>mv $i $i.new<\/p>\n<p>done<\/p>\n<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;<\/p>\n<p>Cut the huge log file in half<\/p>\n<p>#!\/bin\/bash<\/p>\n<p>size=`cat $file|wc -l`<\/p>\n<p>half=$(echo &#8220;$size\/2&#8243;|bc)<\/p>\n<p>tail -$half $file &gt; $loc\/file1.txt \u00a0 \u00a0\/\/ for lower half<\/p>\n<p>tail +$half $file &gt; $loc\/file2.txt \u00a0 \u00a0\/\/ for upper half<\/p>\n<p><strong>If Condition :<\/strong>\u00a0<a href=\"http:\/\/tldp.org\/LDP\/Bash-Beginners-Guide\/html\/sect_07_01.html\">http:\/\/tldp.org\/LDP\/Bash-Beginners-Guide\/html\/sect_07_01.html<\/a><\/p>\n<p><strong>AWK<\/strong><\/p>\n<p>Que:- Why awk?<\/p>\n<p>Ans:-\u00a0\u00a0 The Awk text-processing programming language and is a useful tool for manipulating text.<\/p>\n<p>Awk recognizes the concepts of &#8220;file&#8221;, &#8220;record&#8221;, and &#8220;field&#8221;.<\/p>\n<p>A file consists of records, which by default are the lines of the file. One line becomes one record.<\/p>\n<p>Awk operates on one record at a time.<\/p>\n<p>A record consists of fields, which by default are separated by any number of spaces or tabs.<\/p>\n<p>Field number 1 is accessed with $1, field 2 with $2, and so forth. $0 refers to the whole record.<\/p>\n<ol>\n<li><strong> To print 2nd line of a file<\/strong><\/li>\n<\/ol>\n<p># cat file |awk NR==2&#8242;{print $0}&#8217;<\/p>\n<ol start=\"2\">\n<li><strong> Print section of file between two regular expressions (inclusive)<\/strong><\/li>\n<\/ol>\n<p># cat file |awk &#8216;\/Iowa\/,\/Montana\/&#8217; \u00a0 \u00a0\u00a0\u00a0 \u00a0 \u00a0 \u00a0 # case sensitive<\/p>\n<ol start=\"3\">\n<li><strong> Print section of file based on line numbers (lines 8-12, inclusive)<\/strong><\/li>\n<\/ol>\n<p># cat file |awk NR==8,NR==12&#8242;{print $0}&#8217;<\/p>\n<ol start=\"4\">\n<li><strong> Remove duplicate, consecutive lines (emulates &#8220;uniq&#8221;)<\/strong><\/li>\n<\/ol>\n<p># cat file |awk &#8216;a !~ $0; {a=$0}&#8217;<\/p>\n<ol start=\"5\">\n<li><strong> Print only lines of less than 65 characters<\/strong><\/li>\n<\/ol>\n<p># awk &#8216;length &lt; 64&#8217;<\/p>\n<ol start=\"6\">\n<li><strong> To print no. of blanks lines in a file<\/strong><\/li>\n<\/ol>\n<p># cat file |awk \/^$\/{++x}END'{print x}&#8217;<\/p>\n<ol start=\"7\">\n<li><strong> Average five grades<\/strong><\/li>\n<\/ol>\n<p>john 85 92 78 94 88<\/p>\n<p>andrea 89 90 75 90 86<\/p>\n<p>jasper 84 88 80 92 84<\/p>\n<p># cat file |awk{total = $2 + $3 + $4 + $5 + $6; avg = total \/ 5; print $1, avg }<\/p>\n<ol start=\"8\">\n<li><strong> Print and sort the login names of all users<\/strong><\/li>\n<\/ol>\n<p># awk -F &#8220;:&#8221; &#8216;{ print $1 | &#8220;sort&#8221; }&#8217; \/etc\/passwd<\/p>\n<ol start=\"9\">\n<li><strong> Print the first 2 fields, in opposite order, of every line<\/strong><\/li>\n<\/ol>\n<p># awk &#8216;{print $2, $1}&#8217; file<\/p>\n<p><strong>10.Switch the first 2 fields of every line<\/strong><\/p>\n<p># awk &#8216;{temp = $1; $1 = $2; $2 = temp}&#8217; file<\/p>\n<p><strong>11.Print every line, deleting the second field of that line<\/strong><\/p>\n<p># awk &#8216;{ $2 = &#8220;&#8221;; print }&#8217;<\/p>\n<p><strong>12.Print in reverse order the fields of every line<\/strong><\/p>\n<p># awk &#8216;{for (i=NF; i&gt;0; i&#8211;) printf(&#8220;%s &#8220;,i);printf (&#8220;\\n&#8221;)}&#8217; file <strong>13.Print the sums of the fields of every line<\/strong> \u00a0# awk &#8216;{s=0; for (i=1; i&lt;=NF; i++) s=s+$i; print s}&#8217;<\/p>\n<ol start=\"4\">\n<li><strong> Add all fields in all lines and print the sum<\/strong><\/li>\n<\/ol>\n<p>awk &#8216;{for (i=1; i&lt;=NF; i++) s=s+$i}; END{print s}&#8217;<\/p>\n<ol start=\"5\">\n<li><strong> Print every line after replacing each field with its absolute value<\/strong><\/li>\n<\/ol>\n<p># awk &#8216;{for (i=1; i&lt;=NF; i++) if ($i &lt; 0) $i = -$i; print }&#8217;<\/p>\n<p># awk &#8216;{for (i=1; i&lt;=NF; i++) $i = ($i &lt; 0) ? -$i : $i; print }&#8217;<\/p>\n<ol start=\"6\">\n<li><strong> Print the total number of fields (&#8220;words&#8221;) in all lines<\/strong><\/li>\n<\/ol>\n<p># awk &#8216;{ total = total + NF }; END {print total}&#8217; file<\/p>\n<p><strong>\u00a017.Print the total number of lines that contain &#8220;Beth&#8221;<\/strong><\/p>\n<p># awk &#8216;\/Beth\/{n++}; END {print n+0}&#8217; file<\/p>\n<p><strong>18.Print the largest first field and the line that contains it<\/strong><\/p>\n<p># awk &#8216;$1 &gt; max {max=$1; maxline=$0}; END{ print max, maxline}&#8217;<\/p>\n<ol start=\"9\">\n<li><strong> Print the number of fields in each line, followed by the line<\/strong><\/li>\n<\/ol>\n<p># awk &#8216;{ print NF &#8220;:&#8221; $0 } &#8216;<\/p>\n<ol>\n<li><strong> Print the last field of each line<\/strong><\/li>\n<\/ol>\n<p># awk &#8216;{ print $NF }&#8217;<\/p>\n<ol>\n<li><strong> Print the last field of the last line<\/strong><\/li>\n<\/ol>\n<p># awk &#8216;{ field = $NF }; END{ print field }&#8217;<\/p>\n<ol start=\"2\">\n<li><strong> Print every line with more than 4 fields<\/strong><\/li>\n<\/ol>\n<p># awk &#8216;NF &gt; 4&#8217;<\/p>\n<ol start=\"3\">\n<li><strong> Count the total number of fields in a file.<\/strong><\/li>\n<\/ol>\n<p>awk -F &#8216;:&#8217; &#8216;{ total += NF }; END { print total }&#8217; \/etc\/passwd<\/p>\n<ol start=\"4\">\n<li><strong> Count number of users who is using \/bin\/sh shell<\/strong><\/li>\n<\/ol>\n<p>awk -F &#8216;:&#8217; &#8216;$NF ~ \/\\\/bin\\\/sh\/ { n++ }; END { print n }&#8217; \/etc\/passwd<\/p>\n<ol start=\"5\">\n<li><strong> Find the user details who is having the highest USER ID<\/strong><\/li>\n<\/ol>\n<p>awk -F &#8216;:&#8217;\u00a0 &#8216;$3 &gt; maxuid { maxuid=$3; maxline=$0 }; END { print maxuid, maxline }&#8217; \/etc\/passwd<\/p>\n<ol start=\"6\">\n<li><strong> Print the even-numbered lines<\/strong><\/li>\n<\/ol>\n<p>awk &#8216;NR % 2 == 0&#8217; \/etc\/passwd<\/p>\n<ol start=\"7\">\n<li><strong> Print every line which has the same USER ID and GROUP ID<\/strong><\/li>\n<\/ol>\n<p>awk -F &#8216;:&#8217; &#8216;$3==$4&#8217; passwd.txt<\/p>\n<ol start=\"8\">\n<li><strong> Print user details who has USER ID greater than or equal to 100 and who has to use \/bin\/sh<\/strong><\/li>\n<\/ol>\n<p>awk -F &#8216;:&#8217; &#8216;$3&gt;=100 &amp;&amp; $NF ~ \/\\\/bin\\\/sh\/&#8217; passwd.txt<\/p>\n<ol start=\"9\">\n<li><strong> Print user details who doesn\u2019t have the comments in \/etc\/passwd file<\/strong><\/li>\n<\/ol>\n<p>awk -F &#8216;:&#8217; &#8216;$5 == &#8220;&#8221; &#8216; passwd.txt<\/p>\n<p><strong>How to print line above\/below of a string pattern in a file.<\/strong><\/p>\n<p># cat Portin.log<\/p>\n<p>&lt;PORTIN_ACK&gt;<\/p>\n<p>&lt; Receiver&gt;220&lt;\/Receiver&gt;<\/p>\n<p>&lt; TimeStamp&gt;2011-10-19T10:53:07.225+05:30&lt;\/TimeStamp&gt;<\/p>\n<p>&lt; PortAccepted&gt;N&lt;\/PortAccepted&gt;<\/p>\n<p>&lt; SubscriberSequence&gt;<\/p>\n<p>&lt; MSISDN&gt;8985001696&lt;\/MSISDN&gt;<\/p>\n<p>&lt; ResultCode&gt;RC00006&lt;\/ResultCode&gt;<\/p>\n<p>&lt; \/SubscriberSequence&gt;<\/p>\n<p>&lt; \/PORTIN_ACK&gt;<\/p>\n<p># awk &#8216;{arr[NR]=$0; if($0 ~ \/RC00006\/)s=NR} END{print arr[s-1]}&#8217; Porting.log<\/p>\n<p>O\/P:-&lt; MSISDN&gt;8985001696&lt;\/MSISDN&gt;<\/p>\n<p># awk &#8216;{arr[NR]=$0; if($0 ~ \/RC00006\/)s=NR} END{print arr[s-2]}&#8217; Porting.log<\/p>\n<p>O\/P:- &lt;SubscriberSequence&gt;<\/p>\n<p><strong>GREP:<\/strong><\/p>\n<p><strong>How to count no of blank lines in a file? <\/strong><\/p>\n<p># grep \u2013c \u201c^$\u201d &lt;filename&gt;<\/p>\n<p><strong>How to remove Empty lines form a given file?<\/strong><\/p>\n<p># grep \u2013v \u201c^$\u201d\u00a0 filename &gt; temfilename<\/p>\n<p><strong>What is pattern to search 4 digit word in a file?<\/strong><\/p>\n<p># grep \u201c\\&lt;[0-9] [0-9] [0-9] [0-9]\\&gt;\u201d filename<\/p>\n<p><strong>What is pattern to search the line having only three characters?<\/strong><\/p>\n<p># grep \u201c^\u2026$\u201d filename<\/p>\n<p><strong>What is pattern to display lines ending with \u201c$\u201d character in a given file?<\/strong><\/p>\n<p># grep \u201c\\$$\u201d filename<\/p>\n<p><strong>CUT<\/strong><\/p>\n<p><strong>Getting a List of User Accounts on the System<\/strong><\/p>\n<p># cut -d: -f1 \/etc\/passwd | sort<\/p>\n<p><strong>How to display 2 and 4 th fileds from a given file if the delimetr is \u201c:\u201d? <\/strong><\/p>\n<p># Cut \u2013d\u201d:\u201d \u2013f 2,4 filename<\/p>\n<p>O2Scripts :\u00a0<a href=\"https:\/\/drive.google.com\/?tab=wo&amp;authuser=0#folders\/0BzYdExDgx78PVHYzSTRBQUpfSGM\">https:\/\/drive.google.com\/?tab=wo&amp;authuser=0#folders\/0BzYdExDgx78PVHYzSTRBQUpfSGM<\/a><br \/>\n<strong>IF Conditions<\/strong><\/p>\n<table>\n<tbody>\n<tr>\n<td width=\"638\"><strong>File test operators, <\/strong><strong>Returns true if&#8230;<\/strong><strong>-e:<\/strong>\u00a0 file exists<\/p>\n<p><strong>-a:<\/strong>\u00a0 file exists<\/p>\n<p><strong>-f:<\/strong>\u00a0 file is a <em>regular<\/em> file (not a directory or <a href=\"http:\/\/tldp.org\/LDP\/abs\/html\/devref1.html#DEVFILEREF\">device file<\/a>)<\/p>\n<p><strong>-s:<\/strong>\u00a0 file is not zero size<\/p>\n<p><strong>-d:<\/strong>\u00a0 file is a directory<\/p>\n<p><strong>-b:<\/strong>\u00a0 file is a <a href=\"http:\/\/tldp.org\/LDP\/abs\/html\/devref1.html#BLOCKDEVREF\">block device<\/a><\/p>\n<p><strong>-c:<\/strong>\u00a0 file is a <a href=\"http:\/\/tldp.org\/LDP\/abs\/html\/devref1.html#CHARDEVREF\">character device<\/a><\/p>\n<p>device0=&#8221;\/dev\/sda2&#8243;\u00a0\u00a0\u00a0 # \/\u00a0\u00a0 (root directory)<\/p>\n<p>if [ -b &#8220;$device0&#8221; ]<\/p>\n<p>then<\/p>\n<p>echo &#8220;$device0 is a block device.&#8221;<\/p>\n<p>fi<\/p>\n<p>Output # \/dev\/sda2 is a block device.<\/p>\n<p>device1=&#8221;\/dev\/ttyS1&#8243;\u00a0\u00a0 # PCMCIA modem card.<\/p>\n<p>if [ -c &#8220;$device1&#8221; ]<\/p>\n<p>then<\/p>\n<p>echo &#8220;$device1 is a character device.&#8221;<\/p>\n<p>fi<\/p>\n<p><strong>-p:<\/strong>\u00a0 file is a pipe<\/p>\n<p>function show_input_type()<\/p>\n<p>{<\/p>\n<p>[ -p \/dev\/fd\/0 ] &amp;&amp; echo PIPE || echo STDIN<\/p>\n<p>}<\/p>\n<p>show_input_type &#8220;Input&#8221;\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0# STDIN<\/p>\n<p>echo &#8220;Input&#8221; | show_input_type\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 # PIPE<\/p>\n<p><strong>-h:<\/strong>\u00a0 file is a symbolic link<\/p>\n<p><strong>-L:<\/strong>\u00a0 file is a symbolic link<\/p>\n<p><strong>-S:<\/strong>\u00a0 file is a <a href=\"http:\/\/tldp.org\/LDP\/abs\/html\/devref1.html#SOCKETREF\">socket<\/a><\/p>\n<p><strong>-t:<\/strong>\u00a0 file (<a href=\"http:\/\/tldp.org\/LDP\/abs\/html\/io-redirection.html#FDREF\">descriptor<\/a>) is associated with a terminal device<\/p>\n<p>This test option <a href=\"http:\/\/tldp.org\/LDP\/abs\/html\/intandnonint.html#II2TEST\">used to check<\/a> whether stdin [ -t 0 ] or stdout [ -t 1 ] in a given terminal.<\/p>\n<p><strong>-r:<\/strong>\u00a0 file has read permission (<em>for the user running the test<\/em>)<\/p>\n<p>SLAPD_CONF = \u201d\/usr\/local\/etc\/openldap\/slapd.conf&#8221;<\/p>\n<p>for i in &#8220;$SLAPD_CONF&#8221;<\/p>\n<p>do<\/p>\n<p>if [ ! -r $i ]<\/p>\n<p>then<\/p>\n<p>message &#8220;alert&#8221; &#8220;[ALERT] can&#8217;t read $i&#8221;<\/p>\n<p>exit 1<\/p>\n<p>fi<\/p>\n<p>done<\/p>\n<p><strong>-w:<\/strong> file has write permission (for the user running the test)<\/p>\n<p><strong>-x:<\/strong> file has execute permission (for the user running the test)<\/p>\n<p>SLAPD_BIN=&#8221;$SLAPD_PATH\/libexec\/slapd&#8221;<\/p>\n<p>SLAPCAT_BIN=&#8221;$SLAPD_PATH\/sbin\/slapcat&#8221;<\/p>\n<p>for i in &#8220;$SLAPD_BIN&#8221; &#8220;$SLAPCAT_BIN&#8221; &#8220;$SLAPINDEX_BIN&#8221; &#8220;$SLAPTEST_BIN&#8221; &#8220;$SLURPD_BIN&#8221; &#8220;$DB_ARCHIVE_BIN&#8221; &#8220;$DB_RECOVER_BIN&#8221;<\/p>\n<p>do<\/p>\n<p>if [ ! -x $i ]<\/p>\n<p>then<\/p>\n<p>message &#8220;alert&#8221; &#8220;[ALERT] can&#8217;t execute $i&#8221;<\/p>\n<p>exit 1<\/p>\n<p>fi<\/p>\n<p>done<\/p>\n<p><strong>-g:<\/strong>\u00a0 set-group-id (sgid) flag set on file or directory<\/p>\n<p><strong>-u:<\/strong>\u00a0 set-user-id (suid) flag set on file<\/p>\n<p><strong>-k:<\/strong>\u00a0 <em>sticky bit<\/em> set<\/p>\n<p><strong>-O:<\/strong>\u00a0 you are owner of file<\/p>\n<p><strong>-G:<\/strong>\u00a0 group-id of file same as yours<\/p>\n<p><strong>-N:<\/strong>\u00a0 file modified since it was last read<\/p>\n<p><strong>f1 -nt f2:<\/strong>\u00a0 file <em>f1<\/em> is newer than <em>f2<\/em><\/p>\n<p><strong>f1 -ot f2:<\/strong>\u00a0 file <em>f1<\/em> is older than <em>f2<\/em><\/p>\n<p><strong>f1 -ef f2:<\/strong>\u00a0 files <em>f1<\/em> and <em>f2<\/em> are hard links to the same file<\/p>\n<p><strong>!: <\/strong>\u00a0&#8220;not&#8221; &#8212; reverses the sense of the tests above (returns true if condition absent).<\/p>\n<p><strong>Link:<\/strong>\u00a0<a href=\"http:\/\/tldp.org\/LDP\/Bash-Beginners-Guide\/html\/sect_07_01.html\">http:\/\/tldp.org\/LDP\/Bash-Beginners-Guide\/html\/sect_07_01.html<\/a><\/p>\n<p><strong>Are you root (for port &lt; 1024)?<\/strong><\/p>\n<p>if [ $PORT -lt 1024 -a `id -u` -ne 0 ]<\/p>\n<p>then<\/p>\n<p>message &#8220;alert&#8221; &#8220;[ALERT] only root can launch OpenLDAP on port $PORT&#8221;<\/p>\n<p>exit 1<\/p>\n<p>fi<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n","protected":false},"excerpt":{"rendered":"<p>1. Remove all logs of manager&amp; admin<\/p>\n<p> Remove 90 days old logs Backup logs greater that 35MB <\/p>\n<p>#!\/bin\/bash<\/p>\n<p>rm -rf \/app\/tomcat-6.0.18\/logs\/*manager* \/app\/tomcat-6.0.18\/logs\/*admin*<\/p>\n<p>find \/cda1-logs-backups -name &#8216;*.gz&#8217; -mtime +90 -exec rm {} \\;<\/p>\n<p>echo &#8220;Initiating logrotate `date +%F-%A`&#8221; &gt;&gt; \/app\/tomcat-6.0.18\/logs\/diskutil.log<\/p>\n<p>cd \/app\/tomcat-6.0.18\/logs\/<\/p>\n<p>siz_catalina=`\/bin\/ls -l catalina.out | \/bin\/awk &#8216;{ print $5 }&#8217;`<\/p>\n<p>if [ $siz_catalina -ge &#8220;3600000&#8221; [&#8230;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[21],"tags":[],"_links":{"self":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/3634"}],"collection":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3634"}],"version-history":[{"count":3,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/3634\/revisions"}],"predecessor-version":[{"id":3637,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/3634\/revisions\/3637"}],"wp:attachment":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3634"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3634"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3634"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}