<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss xmlns:atom="http://www.w3.org/2005/Atom" xmlns:openSearch="http://a9.com/-/spec/opensearch/1.1/" xmlns:georss="http://www.georss.org/georss" version="2.0"><channel><atom:id>tag:blogger.com,1999:blog-8796493175195266816</atom:id><lastBuildDate>Mon, 09 Nov 2009 01:35:54 +0000</lastBuildDate><title>UNIX BASH scripting</title><description>Dedicated to all BASH newbies and Linux one liner lovers.
Useful AWK,SED,BASH one liners.</description><link>http://unstableme.blogspot.com/</link><managingEditor>noreply@blogger.com (Jadu Saikia)</managingEditor><generator>Blogger</generator><openSearch:totalResults>349</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openSearch:itemsPerPage><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" href="http://feeds.feedburner.com/UnixBashScripting" type="application/rss+xml" /><feedburner:emailServiceId xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0">UnixBashScripting</feedburner:emailServiceId><feedburner:feedburnerHostname xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0">http://feedburner.google.com</feedburner:feedburnerHostname><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com" /><item><guid isPermaLink="false">tag:blogger.com,1999:blog-8796493175195266816.post-6919212866957345458</guid><pubDate>Sat, 07 Nov 2009 09:36:00 +0000</pubDate><atom:updated>2009-11-07T15:42:15.813+05:30</atom:updated><category domain="http://www.blogger.com/atom/ns#">bash seq</category><category domain="http://www.blogger.com/atom/ns#">awk if else</category><category domain="http://www.blogger.com/atom/ns#">Awk</category><category domain="http://www.blogger.com/atom/ns#">awk newbie</category><category domain="http://www.blogger.com/atom/ns#">bash shell newbie</category><category domain="http://www.blogger.com/atom/ns#">Bash</category><title>Construct range from numbers - awk</title><description>&lt;span style="font-weight: bold;"&gt;Required:&lt;/span&gt;&lt;br /&gt;With the numbers between 100 and 139 whose last digit is in between 0-3, construct the following output:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;100-103&lt;br /&gt;110-113&lt;br /&gt;120-123&lt;br /&gt;130-133&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;Step by step solution:&lt;/span&gt;&lt;br /&gt;1) Numbers between 100 and 139 whose last digit is between 0-3&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;$&lt;/span&gt; seq 100 139 | grep '[0-3]$'&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Output:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;100&lt;br /&gt;101&lt;br /&gt;102&lt;br /&gt;103&lt;br /&gt;110&lt;br /&gt;111&lt;br /&gt;112&lt;br /&gt;113&lt;br /&gt;120&lt;br /&gt;121&lt;br /&gt;122&lt;br /&gt;123&lt;br /&gt;130&lt;br /&gt;131&lt;br /&gt;132&lt;br /&gt;133&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;2) Make them a single line with comma separated&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;$&lt;/span&gt; seq 100 139 | grep '[0-3]$' | paste -sd,&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Output:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;100,101,102,103,110,111,112,113,120,121,122,123,130,131,132,133&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;3) Split the above into multiple sub-lines with each line containing 4 numbers&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;$&lt;/span&gt; seq 100 139 | grep '[0-3]$' | paste -sd, | awk -F, '&lt;br /&gt;{ for(i=1;i&lt;=NF;i++)&lt;br /&gt;{printf("%s%s",$i,i%4?",":"\n")}&lt;br /&gt;}'&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Output:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;100,101,102,103&lt;br /&gt;110,111,112,113&lt;br /&gt;120,121,122,123&lt;br /&gt;130,131,132,133&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;4) Print the first and last field&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;$&lt;/span&gt; seq 100 139 | grep '[0-3]$' | paste -sd, | awk -F, '&lt;br /&gt;{ for(i=1;i&lt;=NF;i++)&lt;br /&gt;{printf("%s%s",$i,i%4?",":"\n")}&lt;br /&gt;}' | awk -F, '{print $1"-"$NF}'&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Output:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;100-103&lt;br /&gt;110-113&lt;br /&gt;120-123&lt;br /&gt;130-133&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;I am sure there must be better ways to achieve this, please comment.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;Related post:&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;- &lt;a href="http://unstableme.blogspot.com/2008/07/break-line-into-multiple-lines-awk-sed.html"&gt;Break a line into multiple lines using awk and sed&lt;/a&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8796493175195266816-6919212866957345458?l=unstableme.blogspot.com'/&gt;&lt;/div&gt;</description><link>http://unstableme.blogspot.com/2009/11/construct-range-from-numbers-awk.html</link><author>noreply@blogger.com (Jadu Saikia)</author><thr:total xmlns:thr="http://purl.org/syndication/thread/1.0">2</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-8796493175195266816.post-7961944300324231674</guid><pubDate>Mon, 02 Nov 2009 17:56:00 +0000</pubDate><atom:updated>2009-11-02T23:35:32.935+05:30</atom:updated><category domain="http://www.blogger.com/atom/ns#">Awk</category><category domain="http://www.blogger.com/atom/ns#">sed replacement</category><category domain="http://www.blogger.com/atom/ns#">Sed</category><category domain="http://www.blogger.com/atom/ns#">bash scripts</category><category domain="http://www.blogger.com/atom/ns#">awk newbie</category><category domain="http://www.blogger.com/atom/ns#">bash shell newbie</category><category domain="http://www.blogger.com/atom/ns#">Bash</category><title>Bash - numbering lines in file using awk</title><description>Input file 'file.txt' contains names of few students.&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;$&lt;/span&gt; cat file.txt&lt;br /&gt;Sam G&lt;br /&gt;Ashok Niak&lt;br /&gt;Rosy M&lt;br /&gt;Peter K&lt;br /&gt;Sid Thom&lt;br /&gt;Rasi Yad&lt;br /&gt;Papu S&lt;br /&gt;Niaraj J&lt;br /&gt;Aloh N K&lt;br /&gt;Nipu H&lt;br /&gt;Quam L&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;Required output:&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;For the entries of the above file,&lt;br /&gt;- add a serial number to each line&lt;br /&gt;- Also add 'House' number such that all the students are group into total 4 houses in the following fashion:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;Sl No,Name,House&lt;br /&gt;1,Sam G,House1&lt;br /&gt;2,Ashok Niak,House2&lt;br /&gt;3,Rosy M,House3&lt;br /&gt;4,Peter K,House4&lt;br /&gt;5,Sid Thom,House1&lt;br /&gt;6,Rasi Yad,House2&lt;br /&gt;7,Papu S,House3&lt;br /&gt;8,Niaraj J,House4&lt;br /&gt;9,Aloh N K,House1&lt;br /&gt;10,Nipu H,House2&lt;br /&gt;11,Quam L,House3&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;The awk solution using awk NR variable:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;$&lt;/span&gt; awk '&lt;br /&gt;BEGIN {OFS=","; print "Sl No,Name,House"}&lt;br /&gt;{print NR,$0,"House"((NR-1)%4)+1}&lt;br /&gt;' file.txt&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Lets format the output for a better look:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;$&lt;/span&gt; awk '&lt;br /&gt;BEGIN {&lt;br /&gt;    FORMAT="%-8s%-18s%s\n" ;&lt;br /&gt;    {printf FORMAT,"Sl No","Name","House"}&lt;br /&gt;}&lt;br /&gt;{printf FORMAT,NR,$0,"House"((NR-1)%4)+1}&lt;br /&gt;' file.txt&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;Output:&lt;/span&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;Sl No   Name              House&lt;br /&gt;1       Sam G             House1&lt;br /&gt;2       Ashok Niak        House2&lt;br /&gt;3       Rosy M            House3&lt;br /&gt;4       Peter K           House4&lt;br /&gt;5       Sid Thom          House1&lt;br /&gt;6       Rasi Yad          House2&lt;br /&gt;7       Papu S            House3&lt;br /&gt;8       Niaraj J          House4&lt;br /&gt;9       Aloh N K          House1&lt;br /&gt;10      Nipu H            House2&lt;br /&gt;11      Quam L            House3&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Read about text alignment using awk printf function &lt;a href="http://unstableme.blogspot.com/2009/07/text-alignment-with-awk-printf-function.html"&gt;here&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;A Bash script for the same will be something like this:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;#!/bin/sh&lt;br /&gt;i=0&lt;br /&gt;while read&lt;br /&gt;   do&lt;br /&gt;       echo "$((i+1)),$REPLY,House$((i++ % 4 + 1))"&lt;br /&gt;done &amp;lt; file.txt&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;Output:&lt;/span&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;$&lt;/span&gt; sh numbering.sh&lt;br /&gt;1,Sam G,House1&lt;br /&gt;2,Ashok Niak,House2&lt;br /&gt;3,Rosy M,House3&lt;br /&gt;4,Peter K,House4&lt;br /&gt;5,Sid Thom,House1&lt;br /&gt;6,Rasi Yad,House2&lt;br /&gt;7,Papu S,House3&lt;br /&gt;8,Niaraj J,House4&lt;br /&gt;9,Aloh N K,House1&lt;br /&gt;10,Nipu H,House2&lt;br /&gt;11,Quam L,House3&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Now a question:&lt;br /&gt;What is that '$REPLY' in the above script ?&lt;br /&gt;&lt;br /&gt;Answer: '$REPLY' is the default value when a variable is not supplied to read.&lt;br /&gt;&lt;br /&gt;So the above script is same as:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;#!/bin/sh&lt;br /&gt;i=0&lt;br /&gt;while read line&lt;br /&gt;   do&lt;br /&gt;       echo "$((i+1)),$line,House$((i++ % 4 + 1))"&lt;br /&gt;done &amp;lt; file.txt&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;In general, numbering of the lines of a file can be done in several ways viz&lt;br /&gt;&lt;br /&gt;Using UNIX/Linux nl(1) command - number lines of files&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;$&lt;/span&gt; nl file.txt&lt;br /&gt;     1  Sam G&lt;br /&gt;     2  Ashok Niak&lt;br /&gt;     3  Rosy M&lt;br /&gt;     4  Peter K&lt;br /&gt;     5  Sid Thom&lt;br /&gt;     6  Rasi Yad&lt;br /&gt;     7  Papu S&lt;br /&gt;     8  Niaraj J&lt;br /&gt;     9  Aloh N K&lt;br /&gt;    10  Nipu H&lt;br /&gt;    11  Quam L&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Using awk NR:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;$&lt;/span&gt; awk '{print "\t"NR"\t"$0}' file.txt&lt;br /&gt;        1       Sam G&lt;br /&gt;        2       Ashok Niak&lt;br /&gt;        3       Rosy M&lt;br /&gt;        4       Peter K&lt;br /&gt;        5       Sid Thom&lt;br /&gt;        6       Rasi Yad&lt;br /&gt;        7       Papu S&lt;br /&gt;        8       Niaraj J&lt;br /&gt;        9       Aloh N K&lt;br /&gt;        10      Nipu H&lt;br /&gt;        11      Quam L&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Using sed syntax:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;$&lt;/span&gt; sed = file.txt | sed 'N;s/\n/\t/'&lt;br /&gt;1       Sam G&lt;br /&gt;2       Ashok Niak&lt;br /&gt;3       Rosy M&lt;br /&gt;4       Peter K&lt;br /&gt;5       Sid Thom&lt;br /&gt;6       Rasi Yad&lt;br /&gt;7       Papu S&lt;br /&gt;8       Niaraj J&lt;br /&gt;9       Aloh N K&lt;br /&gt;10      Nipu H&lt;br /&gt;11      Quam L&lt;br /&gt;&lt;/pre&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8796493175195266816-7961944300324231674?l=unstableme.blogspot.com'/&gt;&lt;/div&gt;</description><link>http://unstableme.blogspot.com/2009/11/bash-numbering-lines-in-file-using-awk.html</link><author>noreply@blogger.com (Jadu Saikia)</author><thr:total xmlns:thr="http://purl.org/syndication/thread/1.0">0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-8796493175195266816.post-5694241412759675466</guid><pubDate>Sat, 31 Oct 2009 17:57:00 +0000</pubDate><atom:updated>2009-10-31T23:45:43.626+05:30</atom:updated><category domain="http://www.blogger.com/atom/ns#">vi editor tips</category><category domain="http://www.blogger.com/atom/ns#">Perl One liner</category><category domain="http://www.blogger.com/atom/ns#">Awk</category><category domain="http://www.blogger.com/atom/ns#">python newbie</category><category domain="http://www.blogger.com/atom/ns#">Sed</category><category domain="http://www.blogger.com/atom/ns#">awk newbie</category><category domain="http://www.blogger.com/atom/ns#">bash shell newbie</category><category domain="http://www.blogger.com/atom/ns#">VIM</category><category domain="http://www.blogger.com/atom/ns#">Bash</category><title>Extract range of lines using sed awk bash</title><description>Below are few different ways to print or extract a section of a file based on line numbers.&lt;br /&gt;&lt;br /&gt;Lets try to   extract lines between line number 27 and line number 99 of input file 'file.txt'&lt;br /&gt;&lt;br /&gt;Using sed editor:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;$&lt;/span&gt; sed -n '27,99 p' file.txt &amp;gt; /tmp/file1&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Which is same as:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;$&lt;/span&gt; sed '27,99 !d' file.txt &amp;gt; /tmp/file2&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Awk alternative : you can make use of awk &lt;a href="http://unstableme.blogspot.com/2009/01/difference-between-awk-nr-and-fnr.html"&gt;NR&lt;/a&gt; variable&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;$&lt;/span&gt; awk 'NR &amp;gt;= 27 &amp;amp;&amp;amp; NR &amp;lt;= 99' file.txt &amp;gt; /tmp/file3&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Using Linux/UNIX 'head' and 'tail' command:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;$&lt;/span&gt; head -99 file.txt | tail -73 &amp;gt; /tmp/file4&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Which is basically:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;$&lt;/span&gt; head -99 file.txt | tail -$(((99-27)+1)) &amp;gt; /tmp/file5&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;In vi editor, we can use the following command in ex mode (open the main file 'file.txt' in vi):&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;:27,99 w! /tmp/file6&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;i.e. Write lines between line number 27 and line number 99 of main file 'file.txt' to file '/tmp/file6'&lt;br /&gt;&lt;br /&gt;Perl alternative would be:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;$&lt;/span&gt; perl -ne 'print if 27..99' file.txt &amp;gt; /tmp/file7&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;And the solution using python:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;$&lt;/span&gt; python&lt;br /&gt;Python 2.5.2 (r252:60911, Jul 22 2009, 15:35:03)&lt;br /&gt;[GCC 4.2.4 (Ubuntu 4.2.4-1ubuntu3)] on linux2&lt;br /&gt;&lt;br /&gt;&gt;&gt;&gt; fp = open("/tmp/file8","w")&lt;br /&gt;&gt;&gt;&gt; for i,line in enumerate(open("file.txt")):&lt;br /&gt;...     if i &gt;= 26 and i &lt; 99 :&lt;br /&gt;...             fp.write(line)&lt;br /&gt;...&lt;br /&gt;&gt;&gt;&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;So the contents of all the output files produced (i.e /tmp/file[1-8]) will be the same (i.e. line number 27 to line number 99 of 'file.txt')&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8796493175195266816-5694241412759675466?l=unstableme.blogspot.com'/&gt;&lt;/div&gt;</description><link>http://unstableme.blogspot.com/2009/10/extract-range-of-lines-using-sed-awk.html</link><author>noreply@blogger.com (Jadu Saikia)</author><thr:total xmlns:thr="http://purl.org/syndication/thread/1.0">2</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-8796493175195266816.post-5736099568827007912</guid><pubDate>Fri, 30 Oct 2009 03:58:00 +0000</pubDate><atom:updated>2009-10-30T09:35:53.114+05:30</atom:updated><category domain="http://www.blogger.com/atom/ns#">xargs</category><category domain="http://www.blogger.com/atom/ns#">bash for</category><category domain="http://www.blogger.com/atom/ns#">bash loop</category><category domain="http://www.blogger.com/atom/ns#">linux redirection</category><category domain="http://www.blogger.com/atom/ns#">awk newbie</category><category domain="http://www.blogger.com/atom/ns#">bash shell newbie</category><category domain="http://www.blogger.com/atom/ns#">bash while</category><category domain="http://www.blogger.com/atom/ns#">bash read</category><category domain="http://www.blogger.com/atom/ns#">Bash</category><title>Bash while loop sum issue explained</title><description>On one of my directory I had a lot of log files and I had to find the count of the total number of lines which starts with 's' (i.e. ^s).&lt;br /&gt;My first approach was:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;$&lt;/span&gt; ls | xargs -i grep -c ^s {} | awk '{sum+=$0} END {print sum}'&lt;br /&gt;190978&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;And I got my result. Then I thought of performing the same using bash scripting for and while loop and this is what I tried.&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;#!/bin/sh&lt;br /&gt;&lt;br /&gt;sum=0&lt;br /&gt;DIR=~/original&lt;br /&gt;for file in $(ls $DIR)&lt;br /&gt;  do&lt;br /&gt;      Slines=$(grep -c ^s $DIR/$file)&lt;br /&gt;      ((sum+=Slines))&lt;br /&gt;      #You can also use&lt;br /&gt;      #sum=$(expr $sum + $Slines)&lt;br /&gt;      #sum=`expr $sum + $Slines`&lt;br /&gt;done&lt;br /&gt;echo $sum&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Executing it:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;$&lt;/span&gt; ./usingfor.sh&lt;br /&gt;190978&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Cool, correct result.&lt;br /&gt;&lt;br /&gt;And then I modified the above script for bash while loop:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;#!/bin/sh&lt;br /&gt;sum=0&lt;br /&gt;DIR=~/original&lt;br /&gt;ls $DIR | while read file&lt;br /&gt;  do&lt;br /&gt;      Slines=$(grep -c ^s $DIR/$file)&lt;br /&gt;      ((sum+=Slines))&lt;br /&gt;done&lt;br /&gt;echo $sum&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Executing it:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;$&lt;/span&gt; ./usingwhile.sh&lt;br /&gt;0&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;Oops!!! what went wrong ?&lt;br /&gt;&lt;br /&gt;In Bash shell, piping directly to bash while loop causes the bash shell to function in a sub shell.&lt;br /&gt;So in the above example the scope of the 'sum' variable is limited to the sub-shell of the while loop and so the modified value of 'sum' is not reflected when we exit the loop. Value of sum is still 0 (local value) as we initialized it to 0 at the beginning of the script.&lt;br /&gt;&lt;br /&gt;The solution of this variable scoping problem with while and direct piping will be:&lt;br /&gt;&lt;br /&gt;Remove the direct pipe and feed the list of file names under '~/original' directory as stdin to the while loop as shown below (Basically create a temp file with the file names of the directory '~/original')&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;#!/bin/sh&lt;br /&gt;sum=0&lt;br /&gt;DIR=~/original&lt;br /&gt;ls $DIR &amp;gt; /tmp/filelist&lt;br /&gt;&lt;br /&gt;while read file&lt;br /&gt;  do&lt;br /&gt;      Slines=$(grep -c ^s $DIR/$file)&lt;br /&gt;      ((sum+=Slines))&lt;br /&gt;done &amp;lt; /tmp/filelist&lt;br /&gt;echo $sum&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Executing it:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;$&lt;/span&gt; ./usingwhile_1.sh&lt;br /&gt;190978&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;And the result is correct.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8796493175195266816-5736099568827007912?l=unstableme.blogspot.com'/&gt;&lt;/div&gt;</description><link>http://unstableme.blogspot.com/2009/10/bash-while-loop-sum-issue-explained.html</link><author>noreply@blogger.com (Jadu Saikia)</author><thr:total xmlns:thr="http://purl.org/syndication/thread/1.0">3</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-8796493175195266816.post-6521840874627545483</guid><pubDate>Wed, 28 Oct 2009 04:16:00 +0000</pubDate><atom:updated>2009-10-28T10:00:37.568+05:30</atom:updated><category domain="http://www.blogger.com/atom/ns#">Linux Utilities</category><category domain="http://www.blogger.com/atom/ns#">Linux Commands</category><category domain="http://www.blogger.com/atom/ns#">bash shell newbie</category><category domain="http://www.blogger.com/atom/ns#">ubuntu tips</category><category domain="http://www.blogger.com/atom/ns#">linux grep</category><category domain="http://www.blogger.com/atom/ns#">Bash</category><title>Grep and print control characters in file - unix</title><description>One of my input file had some control characters (^B i.e. hex \x02)&lt;br /&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://3.bp.blogspot.com/_yqqceaP4wog/SufF-uGjT_I/AAAAAAAACNk/HGmTyY5LMzo/s1600-h/print-control-characters-unix-bash.bmp"&gt;&lt;img style="cursor: pointer; width: 141px; height: 156px;" src="http://3.bp.blogspot.com/_yqqceaP4wog/SufF-uGjT_I/AAAAAAAACNk/HGmTyY5LMzo/s400/print-control-characters-unix-bash.bmp" alt="" id="BLOGGER_PHOTO_ID_5397500359744901106" border="0" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;On my Ubuntu 8.04.3 and GNU grep version of&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;$&lt;/span&gt; grep --version&lt;br /&gt;GNU grep 2.5.3&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;I can grep for any control characters like this:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;$&lt;/span&gt; grep '[[:cntrl:]]' /tmp/file.txt&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;$&lt;/span&gt; grep '[[:cntrl:]]' /tmp/file.txt | less&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://4.bp.blogspot.com/_yqqceaP4wog/SufF_XSdi5I/AAAAAAAACN0/uggmGrswv-A/s1600-h/grep-control-characters-unix-bash.JPG"&gt;&lt;img style="cursor: pointer; width: 115px; height: 75px;" src="http://4.bp.blogspot.com/_yqqceaP4wog/SufF_XSdi5I/AAAAAAAACN0/uggmGrswv-A/s400/grep-control-characters-unix-bash.JPG" alt="" id="BLOGGER_PHOTO_ID_5397500370800708498" border="0" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;Also if you know what to grep for, say in above example the control character is ^B (hex \x02); then you can directly grep for it like this&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;$&lt;/span&gt; grep ^B /tmp/file.txt&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;* ^B to be typed as ctrl V and ctrl B&lt;br /&gt;&lt;br /&gt;And to match any non printable characters, here is another way using grep&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;$&lt;/span&gt; grep '[^[:print:]]' /tmp/file.txt&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;To display non printable characters, here is a way using GNU cat command (My cat version : cat GNU coreutils 6.10)&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;$&lt;/span&gt; cat -v -e -t /tmp/s&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Output:&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://1.bp.blogspot.com/_yqqceaP4wog/SufF-23UhfI/AAAAAAAACNs/IMtTESkr2xo/s1600-h/print-control-characters-unix-bash1.JPG"&gt;&lt;img style="cursor: pointer; width: 115px; height: 78px;" src="http://1.bp.blogspot.com/_yqqceaP4wog/SufF-23UhfI/AAAAAAAACNs/IMtTESkr2xo/s400/print-control-characters-unix-bash1.JPG" alt="" id="BLOGGER_PHOTO_ID_5397500362096936434" border="0" /&gt;&lt;/a&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8796493175195266816-6521840874627545483?l=unstableme.blogspot.com'/&gt;&lt;/div&gt;</description><link>http://unstableme.blogspot.com/2009/10/grep-and-print-control-characters-in.html</link><author>noreply@blogger.com (Jadu Saikia)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://3.bp.blogspot.com/_yqqceaP4wog/SufF-uGjT_I/AAAAAAAACNk/HGmTyY5LMzo/s72-c/print-control-characters-unix-bash.bmp" height="72" width="72" /><thr:total xmlns:thr="http://purl.org/syndication/thread/1.0">1</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-8796493175195266816.post-3229608499178688639</guid><pubDate>Mon, 26 Oct 2009 17:47:00 +0000</pubDate><atom:updated>2009-10-26T23:20:39.078+05:30</atom:updated><category domain="http://www.blogger.com/atom/ns#">vi editor tips</category><category domain="http://www.blogger.com/atom/ns#">vi handy commands</category><category domain="http://www.blogger.com/atom/ns#">bash shell newbie</category><category domain="http://www.blogger.com/atom/ns#">VIM</category><category domain="http://www.blogger.com/atom/ns#">Bash</category><title>Find n-th occurrence of pattern - vim tip</title><description>&lt;span style="font-weight: bold;"&gt;Question:&lt;/span&gt; In vi editor, how can I  find or locate the nth occurrence of a particular search pattern ?&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;Answer:&lt;/span&gt; With new vim editor, once you search a pattern say /queryname , type 4n in command mode which will leap to the 4th occurrence of the word 'queryname' from where you are.&lt;br /&gt;&lt;br /&gt;So to find or locate the 10th occurrence of a particular pattern, go to the top of the file (:1), search for the pattern (/pattern) and then in command mode type 10n.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8796493175195266816-3229608499178688639?l=unstableme.blogspot.com'/&gt;&lt;/div&gt;</description><link>http://unstableme.blogspot.com/2009/10/find-n-th-occurrence-of-pattern-vim-tip.html</link><author>noreply@blogger.com (Jadu Saikia)</author><thr:total xmlns:thr="http://purl.org/syndication/thread/1.0">0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-8796493175195266816.post-5401223367543623105</guid><pubDate>Sat, 24 Oct 2009 16:19:00 +0000</pubDate><atom:updated>2009-10-24T22:16:47.321+05:30</atom:updated><category domain="http://www.blogger.com/atom/ns#">bash seq</category><category domain="http://www.blogger.com/atom/ns#">Linux Utilities</category><category domain="http://www.blogger.com/atom/ns#">Linux Commands</category><category domain="http://www.blogger.com/atom/ns#">bash shell newbie</category><category domain="http://www.blogger.com/atom/ns#">Bash</category><title>Linux shuf command - generate random permutations</title><description>shuf - generate random permutations&lt;br /&gt;&lt;br /&gt;Lets discuss the command line options available with Linux/UNIX 'shuf' command&lt;br /&gt;&lt;br /&gt;From SHUF(1) man page:&lt;br /&gt;1) -e, --echo&lt;br /&gt;treat each ARG as an input line&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;$&lt;/span&gt; shuf -e 3 5 6 7&lt;br /&gt;7&lt;br /&gt;6&lt;br /&gt;5&lt;br /&gt;3&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;$&lt;/span&gt; shuf -e 3 5 6 7&lt;br /&gt;7&lt;br /&gt;5&lt;br /&gt;6&lt;br /&gt;3&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;$&lt;/span&gt; shuf -e 3 5 6 7&lt;br /&gt;3&lt;br /&gt;6&lt;br /&gt;5&lt;br /&gt;7&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;2) -i, --input-range=LO-HI&lt;br /&gt;treat each number LO through HI as an input line&lt;br /&gt;&lt;br /&gt;To shuffle the numbers between 100 and 200&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;$&lt;/span&gt; shuf -i 100-200&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Also, 'shuf' command can be used along with UNIX/Linux '&lt;a href="http://unstableme.blogspot.com/2007/02/generating-loop-arguments-seq-command.html"&gt;seq&lt;/a&gt;' or '&lt;a href="http://unstableme.blogspot.com/2007/12/jot-print-sequential-or-random-data.html"&gt;jot&lt;/a&gt;' command to perform the same as shuf "-i" option.&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;$&lt;/span&gt; shuf -e $(seq 100 200)&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;$&lt;/span&gt; shuf -e $(jot 100 100)&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;3) -n, --head-lines=LINES&lt;br /&gt;output at most LINES lines&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;$&lt;/span&gt; shuf -i 100-200 -n 3&lt;br /&gt;118&lt;br /&gt;133&lt;br /&gt;117&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;$&lt;/span&gt; shuf -i 100-200 -n 3&lt;br /&gt;193&lt;br /&gt;188&lt;br /&gt;145&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;To print a random word in Linux/UNIX&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;$&lt;/span&gt; shuf -n 1 /usr/share/dict/words&lt;br /&gt;disrupted&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;$&lt;/span&gt; shuf -n 1 /usr/share/dict/words&lt;br /&gt;festered&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Note: /usr/share/dict/words is a standard file on UNIX like operating system and is a newline delimited list of dictionary words.&lt;br /&gt;&lt;br /&gt;4) -o, --output=FILE&lt;br /&gt;write result to FILE instead of standard output&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;$&lt;/span&gt; shuf -n 3 /usr/share/dict/words -o /tmp/dict.txt&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;$&lt;/span&gt; cat /tmp/dict.txt&lt;br /&gt;heartlands&lt;br /&gt;temple&lt;br /&gt;unsatisfied&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Also you can use UNIX/Linux redirection for the same&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;$&lt;/span&gt; shuf -n 3 /usr/share/dict/words &amp;gt; /tmp/dict.txt&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;You can shuffle the lines of file and print the output to standard output like this&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;$&lt;/span&gt; shuf &amp;lt; /tmp/file.txt&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;Related post:&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;- &lt;a href="http://unstableme.blogspot.com/2008/05/generate-random-words-in-linux.html"&gt;Generate random words in Linux in bash&lt;/a&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8796493175195266816-5401223367543623105?l=unstableme.blogspot.com'/&gt;&lt;/div&gt;</description><link>http://unstableme.blogspot.com/2009/10/linux-shuf-command-generate-random.html</link><author>noreply@blogger.com (Jadu Saikia)</author><thr:total xmlns:thr="http://purl.org/syndication/thread/1.0">0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-8796493175195266816.post-4646170236478312371</guid><pubDate>Sun, 18 Oct 2009 18:40:00 +0000</pubDate><atom:updated>2009-10-19T00:19:02.472+05:30</atom:updated><category domain="http://www.blogger.com/atom/ns#">bash shell</category><category domain="http://www.blogger.com/atom/ns#">Awk</category><category domain="http://www.blogger.com/atom/ns#">awk newbie</category><category domain="http://www.blogger.com/atom/ns#">bash shell newbie</category><category domain="http://www.blogger.com/atom/ns#">Bash</category><title>Exponential value is awk sum output</title><description>In one of my Debian box with mawk 1.3.3 (mawk is an interpreter for the AWK Programming Language), if I try to add the 2nd fields of the following file using awk:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;$&lt;/span&gt; cat data.txt&lt;br /&gt;a:99540232&lt;br /&gt;b:89795683&lt;br /&gt;a:08160808&lt;br /&gt;c:0971544&lt;br /&gt;d:99500728&lt;br /&gt;a:12212539898&lt;br /&gt;d:98065599&lt;br /&gt;e:92640031&lt;br /&gt;a:3129013&lt;br /&gt;c:4085555&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;The output:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;$&lt;/span&gt; awk -F ":" '{sum+=$NF} END {print sum}' data.txt&lt;br /&gt;1.27084e+10&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;So, awk is giving sum output as exponential format as seen above.&lt;br /&gt;&lt;br /&gt;To get the above sum output in integer, here is a way:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;$&lt;/span&gt; awk -F ":" '{sum+=$NF} END { printf ("%0.0f\n", sum)} ' data.txt&lt;br /&gt;12708429091&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;But on my Ubuntu 8.04.3 with awk version:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;$&lt;/span&gt; awk --version | head -1&lt;br /&gt;GNU Awk 3.1.6&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;$&lt;/span&gt; awk -F ":" '{sum+=$NF} END {print sum}' data.txt&lt;br /&gt;12708429091&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;$&lt;/span&gt; awk -F ":" '{sum+=$NF} END { printf ("%d\n", sum)} ' data.txt&lt;br /&gt;12708429091&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;$&lt;/span&gt; awk -F ":" '{sum+=$NF} END { printf ("%0.0f\n", sum)} ' data.txt&lt;br /&gt;12708429091&lt;br /&gt;&lt;/pre&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8796493175195266816-4646170236478312371?l=unstableme.blogspot.com'/&gt;&lt;/div&gt;</description><link>http://unstableme.blogspot.com/2009/10/exponential-value-is-awk-sum-output.html</link><author>noreply@blogger.com (Jadu Saikia)</author><thr:total xmlns:thr="http://purl.org/syndication/thread/1.0">1</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-8796493175195266816.post-2930095935081275724</guid><pubDate>Thu, 15 Oct 2009 18:02:00 +0000</pubDate><atom:updated>2009-10-15T23:43:03.662+05:30</atom:updated><category domain="http://www.blogger.com/atom/ns#">linux cut</category><category domain="http://www.blogger.com/atom/ns#">awk for loop</category><category domain="http://www.blogger.com/atom/ns#">awk if else</category><category domain="http://www.blogger.com/atom/ns#">Awk</category><category domain="http://www.blogger.com/atom/ns#">Linux Commands</category><category domain="http://www.blogger.com/atom/ns#">awk newbie</category><category domain="http://www.blogger.com/atom/ns#">Bash</category><title>Awk - split file vertically on columns</title><description>I have already put a &lt;a href="http://unstableme.blogspot.com/2009/09/split-file-using-awk-few-examples.html"&gt;post &lt;/a&gt;on - how we can split a file into multiple sub-files based on different conditions (that was basically a horizontal splitting of file); lets see how we can split a file vertically.&lt;br /&gt;&lt;br /&gt;Input file 'file.txt' is a csv file:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;$&lt;/span&gt; cat file.txt&lt;br /&gt;A,B,C,D,E,F,G,H,I&lt;br /&gt;1,2,3,4,5,6,7,8,9&lt;br /&gt;I,II,III,IV,V,VII,VIII,IX&lt;br /&gt;a,b,c,d,e,f,g,h,i&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Required:&lt;br /&gt;&lt;br /&gt;Split the above file into two sub-files such that 1st 3 columns are written to sub-file1 and rest of the columns to sub-file2.&lt;br /&gt;i.e.&lt;br /&gt;&lt;br /&gt;sub-file1 content will be&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;A,B,C&lt;br /&gt;1,2,3&lt;br /&gt;I,II,III&lt;br /&gt;a,b,c&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;And sub-file2 content will be&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;D,E,F,G,H,I&lt;br /&gt;4,5,6,7,8,9&lt;br /&gt;IV,V,VII,VIII,IX&lt;br /&gt;d,e,f,g,h,i&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Well, this is a pretty simple task using Linux/UNIX cut command&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;#Printing first 3 columns of 'file.txt'&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;$&lt;/span&gt; cut -d"," -f1-3 file.txt&lt;br /&gt;or&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;$&lt;/span&gt; cut -d"," -f-3 file.txt&lt;br /&gt;&lt;br /&gt;and&lt;br /&gt;&lt;br /&gt;#Printing from 4th column till end&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;$&lt;/span&gt; cut -d"," -f4-9 file.txt&lt;br /&gt;or&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;$&lt;/span&gt; cut -d"," -f4- file.txt&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Awk solution:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;$&lt;/span&gt; awk -F "," '&lt;br /&gt;{&lt;br /&gt;  for(i=1;i&lt;=NF;i++) {&lt;br /&gt;    if(i &lt;= 3) {&lt;br /&gt;      printf "%s,", $i &gt;&gt; "sub-file1"&lt;br /&gt;      if(i==3){&lt;br /&gt;        printf "\n" &gt;&gt; "sub-file1"&lt;br /&gt;      }&lt;br /&gt;    } else {&lt;br /&gt;      printf "%s,", $i &gt;&gt; "sub-file2"&lt;br /&gt;      if(i==NF){&lt;br /&gt;        printf "\n" &gt;&gt; "sub-file2"&lt;br /&gt;      }&lt;br /&gt;    }&lt;br /&gt;  }&lt;br /&gt;}' file.txt&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Sub-files generated after running the above awk script:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;$&lt;/span&gt; cat sub-file1&lt;br /&gt;A,B,C,&lt;br /&gt;1,2,3,&lt;br /&gt;I,II,III,&lt;br /&gt;a,b,c,&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;$&lt;/span&gt; cat sub-file2&lt;br /&gt;D,E,F,G,H,I,&lt;br /&gt;4,5,6,7,8,9,&lt;br /&gt;IV,V,VII,VIII,IX,&lt;br /&gt;d,e,f,g,h,i,&lt;br /&gt;&lt;/pre&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8796493175195266816-2930095935081275724?l=unstableme.blogspot.com'/&gt;&lt;/div&gt;</description><link>http://unstableme.blogspot.com/2009/10/awk-split-file-vertically-on-columns.html</link><author>noreply@blogger.com (Jadu Saikia)</author><thr:total xmlns:thr="http://purl.org/syndication/thread/1.0">0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-8796493175195266816.post-6319956537284464488</guid><pubDate>Wed, 14 Oct 2009 04:33:00 +0000</pubDate><atom:updated>2009-10-14T10:11:11.709+05:30</atom:updated><category domain="http://www.blogger.com/atom/ns#">linux tr</category><category domain="http://www.blogger.com/atom/ns#">Linux Utilities</category><category domain="http://www.blogger.com/atom/ns#">linux paste</category><category domain="http://www.blogger.com/atom/ns#">Linux Commands</category><category domain="http://www.blogger.com/atom/ns#">bash scripts</category><category domain="http://www.blogger.com/atom/ns#">bash shell newbie</category><category domain="http://www.blogger.com/atom/ns#">Bash</category><title>Insert lines from files using Linux paste command</title><description>I have already put a &lt;a href="http://unstableme.blogspot.com/2009/01/linux-paste-command-good-examples-uses.html"&gt;post&lt;/a&gt; on some good uses of Linux/UNIX 'paste' command; lets check another practical one using paste command.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;Input files:&lt;/span&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;$&lt;/span&gt; cat contestant.txt&lt;br /&gt;Christopher&lt;br /&gt;Williams&lt;br /&gt;Darwin&lt;br /&gt;Ajay&lt;br /&gt;Brain&lt;br /&gt;Amay&lt;br /&gt;Jiten&lt;br /&gt;Lila&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;$&lt;/span&gt; cat leader.txt&lt;br /&gt;Mr B&lt;br /&gt;Mrs C&lt;br /&gt;Mrs A&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;Output required:&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;For every single line of 'leader.txt'; insert 3 lines from file 'contestant.txt'; so that the output looks like this:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;Mr B&lt;br /&gt;Christopher&lt;br /&gt;Williams&lt;br /&gt;Darwin&lt;br /&gt;Mrs C&lt;br /&gt;Ajay&lt;br /&gt;Brain&lt;br /&gt;Amay&lt;br /&gt;Mrs A&lt;br /&gt;Jiten&lt;br /&gt;Lila&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;The step by step solution using Linux/UNIX paste command&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;$&lt;/span&gt; cat contestant.txt | paste - - -&lt;br /&gt;Output:&lt;br /&gt;Christopher     Williams        Darwin&lt;br /&gt;Ajay    Brain   Amay&lt;br /&gt;Jiten   Lila&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;$&lt;/span&gt; cat contestant.txt | paste - - - | paste leader.txt -&lt;br /&gt;Output:&lt;br /&gt;Mr B    Christopher     Williams        Darwin&lt;br /&gt;Mrs C   Ajay    Brain   Amay&lt;br /&gt;Mrs A   Jiten   Lila&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;$&lt;/span&gt; cat contestant.txt | paste - - - |paste leader.txt - |tr "\t" "\n"&lt;br /&gt;Output:&lt;br /&gt;Mr B&lt;br /&gt;Christopher&lt;br /&gt;Williams&lt;br /&gt;Darwin&lt;br /&gt;Mrs C&lt;br /&gt;Ajay&lt;br /&gt;Brain&lt;br /&gt;Amay&lt;br /&gt;Mrs A&lt;br /&gt;Jiten&lt;br /&gt;Lila&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Another similar one liner for the same:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;$&lt;/span&gt; &amp;lt; contestant.txt paste - - - | paste leader.txt - | tr "\t" "\n"&lt;br /&gt;&lt;/pre&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8796493175195266816-6319956537284464488?l=unstableme.blogspot.com'/&gt;&lt;/div&gt;</description><link>http://unstableme.blogspot.com/2009/10/insert-lines-from-files-using-linux.html</link><author>noreply@blogger.com (Jadu Saikia)</author><thr:total xmlns:thr="http://purl.org/syndication/thread/1.0">0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-8796493175195266816.post-3728925503700223893</guid><pubDate>Fri, 09 Oct 2009 03:56:00 +0000</pubDate><atom:updated>2009-10-09T09:57:10.876+05:30</atom:updated><category domain="http://www.blogger.com/atom/ns#">bash shell</category><category domain="http://www.blogger.com/atom/ns#">Awk</category><category domain="http://www.blogger.com/atom/ns#">awk groupby</category><category domain="http://www.blogger.com/atom/ns#">awk newbie</category><category domain="http://www.blogger.com/atom/ns#">bash shell newbie</category><category domain="http://www.blogger.com/atom/ns#">Bash</category><category domain="http://www.blogger.com/atom/ns#">awk array</category><title>Grouping files using awk in Bash shell</title><description>My directory contains a set of log files with filename of the following pattern:&lt;br /&gt;debug.vendor-name.some-serial-number.epoch-time-stamp. device-class.log&lt;br /&gt;&lt;br /&gt;where:&lt;br /&gt;epoch-time-stamp&lt;br /&gt;is the UNIX time stamp when the log file is generated.&lt;br /&gt;&lt;br /&gt;device-class&lt;br /&gt;first 4 character of this number represent the service-name of the device and next 6 character is for device class name&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;$&lt;/span&gt; ls -1&lt;br /&gt;debug.cisco.0001.1254059837.&lt;span style="color: rgb(0, 0, 153);"&gt;svc1&lt;/span&gt;class2.log&lt;br /&gt;debug.cisco.0001.1255058827.&lt;span style="color: rgb(0, 0, 153);"&gt;svc1&lt;/span&gt;class3.log&lt;br /&gt;debug.cisco.0001.1255058827.&lt;span style="color: rgb(0, 0, 153);"&gt;svc2&lt;/span&gt;class3.log&lt;br /&gt;debug.cisco.0001.1255058837.&lt;span style="color: rgb(0, 0, 153);"&gt;svc1&lt;/span&gt;class2.log&lt;br /&gt;debug.cisco.0001.1255059834.&lt;span style="color: rgb(0, 0, 153);"&gt;svc2&lt;/span&gt;class3.log&lt;br /&gt;debug.cisco.0002.1255059819.&lt;span style="color: rgb(0, 0, 153);"&gt;svc1&lt;/span&gt;grade2.log&lt;br /&gt;debug.cisco.0002.1255059849.&lt;span style="color: rgb(0, 0, 153);"&gt;svc1&lt;/span&gt;class1.log&lt;br /&gt;debug.cisco.0002.1255059849.&lt;span style="color: rgb(0, 0, 153);"&gt;svc2&lt;/span&gt;class1.log&lt;br /&gt;debug.juniper.0001.1255059831.&lt;span style="color: rgb(0, 0, 153);"&gt;svc1&lt;/span&gt;class2.log&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Lets try to group similar files (under different conditions) and count number of files in each of the groups.&lt;br /&gt;&lt;br /&gt;One: Group based on vendor-name(2nd field)&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;$&lt;/span&gt; ls | awk -F "." '{count[$2]++}END{for(j in count) print j,"["count[j]"]"}'&lt;br /&gt;&lt;br /&gt;Output:&lt;br /&gt;cisco [8]&lt;br /&gt;juniper [1]&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Two: Group based on vendor-name(2nd field) and serial-number(3rd field)&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;$&lt;/span&gt; ls | awk -F "." '{count[$2" "$3]++}END{for(j in count) print j,"["count[j]"]"}'&lt;br /&gt;&lt;br /&gt;Output:&lt;br /&gt;cisco 0002 [3]&lt;br /&gt;juniper 0001 [1]&lt;br /&gt;cisco 0001 [5]&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Three: Group based on vendor-name(2nd field) , serial-number(3rd field) and UNIX-time-stamp(4th field) in hour bucketing*&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;$&lt;/span&gt; ls | awk -F "." '{count[$2" "$3" "$4-($4%3600)]++}&lt;br /&gt;END{for(j in count) print j,"["count[j]"]"}'&lt;br /&gt;&lt;br /&gt;Output:&lt;br /&gt;juniper 0001 1255057200 [1]&lt;br /&gt;cisco 0001 1254056400 [1]&lt;br /&gt;cisco 0002 1255057200 [3]&lt;br /&gt;cisco 0001 1255057200 [4]&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;*hour bucketing :&lt;br /&gt;e.g: 'Fri Oct  9 09:51:55 UTC 2009' and 'Fri Oct  9 09:01:55 UTC 2009' will fall to the same bucket of Fri Oct  9 09:00:00 UTC 2009&lt;br /&gt;&lt;br /&gt;Four: Group based on vendor-name(2nd field) and first 4 characters of device-class (5th field)&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;$&lt;/span&gt; ls | awk -F "." '&lt;br /&gt;{ $5 = substr($5, 0, 4) }&lt;br /&gt;{count[$2" "$5]++}&lt;br /&gt;END{for(j in count) print j,"["count[j]"]"}'&lt;br /&gt;&lt;br /&gt;Output:&lt;br /&gt;juniper svc1 [1]&lt;br /&gt;cisco svc1 [5]&lt;br /&gt;cisco svc2 [3]&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Five: Group based on&lt;br /&gt;vendor-name(2nd field),&lt;br /&gt;serial-number(3rd field) ,&lt;br /&gt;UNIX-time-stamp(4th field) in hour bucketing&lt;br /&gt;and first 4 characters of device-class (5th field)&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;$&lt;/span&gt; ls | awk -F "." '&lt;br /&gt;{ $5 = substr($5, 0, 4) }&lt;br /&gt;{count[$2" "$3" "$4-($4%86400)" "$5]++}&lt;br /&gt;END {for(j in count) print j,"["count[j]"]"}'&lt;br /&gt;&lt;br /&gt;Output:&lt;br /&gt;cisco 0002 1255046400 svc1 [2]&lt;br /&gt;cisco 0002 1255046400 svc2 [1]&lt;br /&gt;juniper 0001 1255046400 svc1 [1]&lt;br /&gt;cisco 0001 1255046400 svc1 [2]&lt;br /&gt;cisco 0001 1255046400 svc2 [2]&lt;br /&gt;cisco 0001 1254009600 svc1 [1]&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;Hope you find it useful.&lt;br /&gt;&lt;br /&gt;Related post:&lt;br /&gt;&lt;br /&gt;- SQL Sum of and group by using &lt;a href="http://unstableme.blogspot.com/2008/09/sum-of-and-group-by-using-awk.html"&gt;awk&lt;/a&gt;&lt;br /&gt;- Group by Clause functionality using &lt;a href="http://unstableme.blogspot.com/2008/09/group-by-clause-functionality-in-awk.html"&gt;awk&lt;/a&gt;&lt;br /&gt;- Associative array in &lt;a href="http://unstableme.blogspot.com/2008/05/associative-array-in-awk.html"&gt;awk&lt;/a&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8796493175195266816-3728925503700223893?l=unstableme.blogspot.com'/&gt;&lt;/div&gt;</description><link>http://unstableme.blogspot.com/2009/10/grouping-files-using-awk-in-bash-shell.html</link><author>noreply@blogger.com (Jadu Saikia)</author><thr:total xmlns:thr="http://purl.org/syndication/thread/1.0">0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-8796493175195266816.post-644262891956657794</guid><pubDate>Wed, 07 Oct 2009 04:43:00 +0000</pubDate><atom:updated>2009-10-07T10:18:30.080+05:30</atom:updated><category domain="http://www.blogger.com/atom/ns#">bash parameter substitution</category><category domain="http://www.blogger.com/atom/ns#">awk newbie</category><category domain="http://www.blogger.com/atom/ns#">Bash Substring Removal</category><category domain="http://www.blogger.com/atom/ns#">bash shell newbie</category><category domain="http://www.blogger.com/atom/ns#">Bash</category><title>Extract sub-string from variable in bash</title><description>Suppose:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;$&lt;/span&gt; mypath=/dir1/dir2/dir3/dir4&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;$&lt;/span&gt; echo $mypath&lt;br /&gt;/dir1/dir2/dir3/dir4&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Now, if you need to print the parent path from the above path (i.e. print '/dir1/dir2/dir3')&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;$&lt;/span&gt; dirname $mypath&lt;br /&gt;/dir1/dir2/dir3&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;$&lt;/span&gt; parentpath=$(dirname $mypath)&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;$&lt;/span&gt; echo $parentpath&lt;br /&gt;/dir1/dir2/dir3&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Using Sub-string Removal ways in Bash shell&lt;br /&gt;&lt;br /&gt;${string%substring}&lt;br /&gt;It deletes shortest match of $substring from 'back' of $string.&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;$&lt;/span&gt; echo ${mypath%/*}&lt;br /&gt;/dir1/dir2/dir3&lt;br /&gt;&lt;br /&gt;or&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;$&lt;/span&gt; printf '%s\n' "${mypath%/*}"&lt;br /&gt;/dir1/dir2/dir3&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;If you need to print the last directory name from the above mypath, here are few ways:&lt;br /&gt;&lt;br /&gt;Using Sub-string Removal ways in Bash shell&lt;br /&gt;${string##substring}&lt;br /&gt;It deletes the "longest" match of $substring from 'front' of $string.&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;$&lt;/span&gt; echo ${mypath##*/}&lt;br /&gt;dir4&lt;br /&gt;&lt;br /&gt;Another way using awk:&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;$&lt;/span&gt; echo $mypath | awk '{print $NF}' FS=\/&lt;br /&gt;dir4&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;Similar post:&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;- Truncate string using &lt;a href="http://unstableme.blogspot.com/2009/09/truncate-string-using-bash-script.html"&gt;bash script&lt;/a&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8796493175195266816-644262891956657794?l=unstableme.blogspot.com'/&gt;&lt;/div&gt;</description><link>http://unstableme.blogspot.com/2009/10/extract-sub-string-from-variable-in.html</link><author>noreply@blogger.com (Jadu Saikia)</author><thr:total xmlns:thr="http://purl.org/syndication/thread/1.0">1</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-8796493175195266816.post-8183448527240664339</guid><pubDate>Fri, 02 Oct 2009 14:51:00 +0000</pubDate><atom:updated>2009-10-02T20:45:15.672+05:30</atom:updated><category domain="http://www.blogger.com/atom/ns#">du command</category><category domain="http://www.blogger.com/atom/ns#">Linux Utilities</category><category domain="http://www.blogger.com/atom/ns#">Linux Commands</category><category domain="http://www.blogger.com/atom/ns#">awk newbie</category><category domain="http://www.blogger.com/atom/ns#">bash shell newbie</category><category domain="http://www.blogger.com/atom/ns#">Bash</category><title>Directory size excluding sub-directories - Linux</title><description>Directory '/home/user/work/demo/' contains a few regular files and two directories say "part2"(size=41236 KB) and "libs"(size=20620 KB).&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;$&lt;/span&gt; du ~/work/demo/&lt;br /&gt;41236   /home/user/work/demo/part2&lt;br /&gt;20620   /home/user/work/demo/libs&lt;br /&gt;87640   /home/user/work/demo/&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;From Linux/UNIX DU(1) command man page:&lt;br /&gt;&lt;br /&gt;-s, --summarize&lt;br /&gt;display only a total for each argument.&lt;br /&gt;&lt;br /&gt;So, the following command is going to display the total size of the directory '/home/user/work/demo/'&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;$&lt;/span&gt; du -s ~/work/demo/&lt;br /&gt;87640   /home/user/work/demo/&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Now, if you need to find the size of the '/home/user/work/demo/' directory excluding the size of the sub-directories, there is a command line option with DU(1):&lt;br /&gt;&lt;br /&gt;-S, --separate-dirs&lt;br /&gt;do not include size of sub-directories&lt;br /&gt;&lt;br /&gt;So&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;$&lt;/span&gt; du -S ~/work/demo/&lt;br /&gt;41236   /home/user/work/demo/part2&lt;br /&gt;20620   /home/user/work/demo/libs&lt;br /&gt;&lt;span style="color: rgb(0, 0, 153);"&gt;25784&lt;/span&gt;   /home/user/work/demo/&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Now,&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;$&lt;/span&gt; du -S --max-depth=0 ~/work/demo/&lt;br /&gt;25784   /home/user/work/demo/&lt;br /&gt;&lt;br /&gt;or&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;$&lt;/span&gt; du -S ~/work/demo/ | awk 'END {print}'&lt;br /&gt;25784   /home/user/work/demo/&lt;br /&gt;&lt;/pre&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8796493175195266816-8183448527240664339?l=unstableme.blogspot.com'/&gt;&lt;/div&gt;</description><link>http://unstableme.blogspot.com/2009/10/directory-size-excluding-sub.html</link><author>noreply@blogger.com (Jadu Saikia)</author><thr:total xmlns:thr="http://purl.org/syndication/thread/1.0">0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-8796493175195266816.post-5766543678227053600</guid><pubDate>Wed, 30 Sep 2009 04:42:00 +0000</pubDate><atom:updated>2009-09-30T10:23:15.435+05:30</atom:updated><category domain="http://www.blogger.com/atom/ns#">Awk</category><category domain="http://www.blogger.com/atom/ns#">awk newbie</category><category domain="http://www.blogger.com/atom/ns#">bash shell newbie</category><category domain="http://www.blogger.com/atom/ns#">Bash</category><title>Subtract from total amount - Awk example</title><description>&lt;span style="font-weight: bold;"&gt;Input file:&lt;/span&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;$&lt;/span&gt; cat expense.txt&lt;br /&gt;Particulars,Item1,Item2,Item3&lt;br /&gt;BudgetAmount,12000,4560,5000&lt;br /&gt;Expense@2006,1800,3000,250&lt;br /&gt;Expense@2007,2210,2100,3000&lt;br /&gt;Expense@2008,100,1500,320&lt;br /&gt;Expense@2009,0,100,20&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;Output required:&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;For all the items, calculate the amount left after expense i.e.&lt;br /&gt;&lt;br /&gt;For an item:&lt;br /&gt;Amount Left = (BudgetAmount - (Expense@2006 + Expense@2007 + Expense@2008 + Expense@2009))&lt;br /&gt;&lt;br /&gt;i.e. required output:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;BudgetAmount,12000,4560,5000&lt;br /&gt;Expense@2006,1800,3000,250&lt;br /&gt;Expense@2007,2210,2100,3000&lt;br /&gt;Expense@2008,100,1500,320&lt;br /&gt;Expense@2009,0,100,20&lt;br /&gt;&lt;span style="color: rgb(0, 0, 153);"&gt;Amount Left,7890,-2140,1410&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;Microsoft Excel representation of the above:&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://3.bp.blogspot.com/_yqqceaP4wog/SsLhw6nHcCI/AAAAAAAACKg/woUOlqAQ3uU/s1600-h/awk-subtraction-with-excel-example.JPG"&gt;&lt;img style="margin: 0px auto 10px; display: block; text-align: center; cursor: pointer; width: 327px; height: 172px;" src="http://3.bp.blogspot.com/_yqqceaP4wog/SsLhw6nHcCI/AAAAAAAACKg/woUOlqAQ3uU/s400/awk-subtraction-with-excel-example.JPG" alt="" id="BLOGGER_PHOTO_ID_5387116334771826722" border="0" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;The awk program:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;$&lt;/span&gt; awk 'BEGIN { FS=OFS="," }&lt;br /&gt;$1 == "BudgetAmount" {&lt;br /&gt;         bI1 = $2&lt;br /&gt;         bI2 = $3&lt;br /&gt;         bI3 = $4&lt;br /&gt;         print&lt;br /&gt;}&lt;br /&gt;/^Expense@/ {&lt;br /&gt;         bI1 -= $2&lt;br /&gt;         bI2 -= $3&lt;br /&gt;         bI3 -= $4&lt;br /&gt;         print&lt;br /&gt;}&lt;br /&gt;END {&lt;br /&gt;         print "Amount Left",bI1, bI2, bI3&lt;br /&gt;}' expense.txt&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;Related post:&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;- Bash &lt;a href="http://unstableme.blogspot.com/2009/08/bash-script-for-sequential-subtraction.html"&gt;script&lt;/a&gt; for sequential subtraction of numbers&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8796493175195266816-5766543678227053600?l=unstableme.blogspot.com'/&gt;&lt;/div&gt;</description><link>http://unstableme.blogspot.com/2009/09/subtract-from-total-amount-awk-example.html</link><author>noreply@blogger.com (Jadu Saikia)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://3.bp.blogspot.com/_yqqceaP4wog/SsLhw6nHcCI/AAAAAAAACKg/woUOlqAQ3uU/s72-c/awk-subtraction-with-excel-example.JPG" height="72" width="72" /><thr:total xmlns:thr="http://purl.org/syndication/thread/1.0">1</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-8796493175195266816.post-1165714721164350236</guid><pubDate>Tue, 29 Sep 2009 03:51:00 +0000</pubDate><atom:updated>2009-09-29T09:36:05.747+05:30</atom:updated><category domain="http://www.blogger.com/atom/ns#">awk if else</category><category domain="http://www.blogger.com/atom/ns#">awk printf</category><category domain="http://www.blogger.com/atom/ns#">Awk</category><category domain="http://www.blogger.com/atom/ns#">awk newbie</category><category domain="http://www.blogger.com/atom/ns#">bash shell newbie</category><category domain="http://www.blogger.com/atom/ns#">Bash</category><title>If else examples in awk - bash</title><description>&lt;span style="font-weight: bold;"&gt;Input file&lt;/span&gt;: Each line of 'num.txt' contains 2 numbers (say A and B).&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 102, 102);"&gt;$&lt;/span&gt; cat num.txt&lt;br /&gt;34,140&lt;br /&gt;190,140&lt;br /&gt;89,120&lt;br /&gt;110,110&lt;br /&gt;210,115&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;Required&lt;/span&gt;: Calculate and print percentage (A/B)*100 with the following conditions:&lt;br /&gt;&lt;br /&gt;- If percentage is less than 100, print the calculated actual percentage&lt;br /&gt;- If percentage is more than 100, print the percentage as 100&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;First solution:&lt;/span&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 102, 102);"&gt;$&lt;/span&gt; awk '&lt;br /&gt;BEGIN {FS=OFS=","}&lt;br /&gt;{if($1&gt;$2) {print $0,100}&lt;br /&gt;else {print $0,($1/$2)*100}&lt;br /&gt;}' num.txt&lt;br /&gt;&lt;br /&gt;Output:&lt;br /&gt;34,140,24.2857&lt;br /&gt;190,140,100&lt;br /&gt;89,120,74.1667&lt;br /&gt;110,110,100&lt;br /&gt;210,115,100&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Lets do some text alignment and formatting using awk.&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 102, 102);"&gt;$&lt;/span&gt; awk '&lt;br /&gt;BEGIN {FS="," ;  {printf "%-10s%-8s%s\n","A","B","% age"}}&lt;br /&gt;{if($1&gt;=$2) {printf "%-10s%-8s%s\n",$1,$2,100}&lt;br /&gt;else {printf "%-10s%-8s%2.2f\n",$1,$2,($1/$2)*100}&lt;br /&gt;}' num.txt&lt;br /&gt;&lt;br /&gt;Output:&lt;br /&gt;A         B       % age&lt;br /&gt;34        140     24.29&lt;br /&gt;190       140     100&lt;br /&gt;89        120     74.17&lt;br /&gt;110       110     100&lt;br /&gt;210       115     100&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Or a different look of the above script:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 102, 102);"&gt;$&lt;/span&gt; awk '&lt;br /&gt;BEGIN {&lt;br /&gt;    FS="," ; FORMAT="%-10s%-8s%s\n" ;&lt;br /&gt;    {printf FORMAT,"A","B","% age"}&lt;br /&gt;}&lt;br /&gt;{&lt;br /&gt;    if($1&gt;=$2) {printf FORMAT,$1,$2,100}&lt;br /&gt;    else {printf FORMAT,$1,$2,($1/$2)*100}&lt;br /&gt;}' num.txt&lt;br /&gt;&lt;br /&gt;Output:&lt;br /&gt;A         B       % age&lt;br /&gt;34        140     24.2857&lt;br /&gt;190       140     100&lt;br /&gt;89        120     74.1667&lt;br /&gt;110       110     100&lt;br /&gt;210       115     100&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Another way of writing if else in AWK.&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 102, 102);"&gt;$&lt;/span&gt; awk '&lt;br /&gt;{printf("%-10s%-8s%2.2f\n",\&lt;br /&gt;$1,$2, ($1&lt;=$2) ? ($1/$2)*100 : 100)&lt;br /&gt;}' FS="," num.txt&lt;br /&gt;&lt;br /&gt;Output:&lt;br /&gt;34        140     24.29&lt;br /&gt;190       140     100.00&lt;br /&gt;89        120     74.17&lt;br /&gt;110       110     100.00&lt;br /&gt;210       115     100.00&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;Related post:&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;- &lt;a href="http://unstableme.blogspot.com/2008/09/calculate-percentage-using-awk-in-bash.html"&gt;Calculate percentage using awk in bash&lt;/a&gt;&lt;br /&gt;- Align text with awk &lt;a href="http://unstableme.blogspot.com/2009/07/text-alignment-with-awk-printf-function.html"&gt;printf&lt;/a&gt; function&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8796493175195266816-1165714721164350236?l=unstableme.blogspot.com'/&gt;&lt;/div&gt;</description><link>http://unstableme.blogspot.com/2009/09/if-else-examples-in-awk-bash.html</link><author>noreply@blogger.com (Jadu Saikia)</author><thr:total xmlns:thr="http://purl.org/syndication/thread/1.0">2</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-8796493175195266816.post-3139671943684707529</guid><pubDate>Thu, 24 Sep 2009 18:20:00 +0000</pubDate><atom:updated>2009-09-24T23:55:03.771+05:30</atom:updated><category domain="http://www.blogger.com/atom/ns#">awk if else</category><category domain="http://www.blogger.com/atom/ns#">Awk</category><category domain="http://www.blogger.com/atom/ns#">awk replacement</category><category domain="http://www.blogger.com/atom/ns#">sed replacement</category><category domain="http://www.blogger.com/atom/ns#">Sed</category><category domain="http://www.blogger.com/atom/ns#">awk newbie</category><category domain="http://www.blogger.com/atom/ns#">bash shell newbie</category><title>Insert after certain characters - awk and sed</title><description>&lt;pre&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;$&lt;/span&gt; add="20010db885a3000000008a2e03707334"&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;$&lt;/span&gt; echo $add&lt;br /&gt;20010db885a3000000008a2e03707334&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;Required output&lt;/span&gt;: Insert a colon ':' after every 4 characters in the above line.&lt;br /&gt;So the output required:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;2001:0db8:85a3:0000:0000:8a2e:0370:7334&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;Using awk:&lt;/span&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;$&lt;/span&gt; echo $add | awk -F "" '&lt;br /&gt;{for(i=1;i&lt;=NF;i++){printf("%s%s",$i,i%4?"":":")}}'|awk '{sub(/:$/,"")};1'&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Note: Mind the use of "" as the field separator.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;Using sed:&lt;/span&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;$&lt;/span&gt; echo $add | sed 's/..../&amp;amp;:/g;s/:$//'&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;Related post:&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;- &lt;a href="http://unstableme.blogspot.com/2008/07/break-line-into-multiple-lines-awk-sed.html"&gt;Break &lt;/a&gt;a line into multiple lines using awk and sed&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8796493175195266816-3139671943684707529?l=unstableme.blogspot.com'/&gt;&lt;/div&gt;</description><link>http://unstableme.blogspot.com/2009/09/insert-after-certain-characters-awk-and.html</link><author>noreply@blogger.com (Jadu Saikia)</author><thr:total xmlns:thr="http://purl.org/syndication/thread/1.0">4</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-8796493175195266816.post-4198461752990164700</guid><pubDate>Tue, 22 Sep 2009 17:44:00 +0000</pubDate><atom:updated>2009-09-22T23:23:43.865+05:30</atom:updated><category domain="http://www.blogger.com/atom/ns#">sed replacement</category><category domain="http://www.blogger.com/atom/ns#">Sed</category><category domain="http://www.blogger.com/atom/ns#">bash shell newbie</category><category domain="http://www.blogger.com/atom/ns#">Bash</category><title>Replace asterisk using sed - bash</title><description>For example:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;$&lt;/span&gt; countries="**India **South Africa **Sri Lanka **West Indies"&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;$&lt;/span&gt; echo $countries&lt;br /&gt;**India **South Africa **Sri Lanka **West Indies&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;Output required:&lt;/span&gt;&lt;br /&gt;Replace "**" with a newline, so that above line becomes:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;**India&lt;br /&gt;**South Africa&lt;br /&gt;**Sri Lanka&lt;br /&gt;**West Indies&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;The sed replacement:&lt;/span&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;$&lt;/span&gt; echo $countries | sed 's! **!\n**!g'&lt;br /&gt;sed: -e expression #1, char 12: Invalid preceding regular expression&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;So you would need to escape the asterisk above.&lt;br /&gt;i.e.&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;$&lt;/span&gt; echo $countries | sed 's! \*\*!\n**!g'&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Another way using sed:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;$&lt;/span&gt; echo $countries | sed 's! \*\*!\&lt;br /&gt;\*\*!g'&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Similarly:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;$&lt;/span&gt; echo "a,b,c,d" | sed 's!,!\n!g'&lt;br /&gt;&lt;br /&gt;Output:&lt;br /&gt;a&lt;br /&gt;b&lt;br /&gt;c&lt;br /&gt;d&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;Related post:&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;1)&lt;br /&gt;Suppose your i/p line is:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;1 b 3 4 e 6 g 8 i j k&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;And you wish to split the above line into multiple lines (each line with say 3 entries)&lt;br /&gt;&lt;br /&gt;i.e.&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;1 b 3&lt;br /&gt;4 e 6&lt;br /&gt;g 8 i&lt;br /&gt;j k&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;a href="http://unstableme.blogspot.com/2008/07/break-line-into-multiple-lines-awk-sed.html"&gt;here&lt;/a&gt; is a post&lt;br /&gt;&lt;br /&gt;2)&lt;br /&gt;One more related &lt;a href="http://unstableme.blogspot.com/2008/02/split-line-into-lines-awk-or-sed.html"&gt;post&lt;/a&gt; of breaking a line into multiple lines based on length&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8796493175195266816-4198461752990164700?l=unstableme.blogspot.com'/&gt;&lt;/div&gt;</description><link>http://unstableme.blogspot.com/2009/09/replace-asterisk-using-sed-bash.html</link><author>noreply@blogger.com (Jadu Saikia)</author><thr:total xmlns:thr="http://purl.org/syndication/thread/1.0">0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-8796493175195266816.post-1709954147433309031</guid><pubDate>Mon, 21 Sep 2009 15:06:00 +0000</pubDate><atom:updated>2009-09-21T20:47:28.868+05:30</atom:updated><category domain="http://www.blogger.com/atom/ns#">awk ORS</category><category domain="http://www.blogger.com/atom/ns#">Awk</category><category domain="http://www.blogger.com/atom/ns#">awk newbie</category><category domain="http://www.blogger.com/atom/ns#">bash shell newbie</category><category domain="http://www.blogger.com/atom/ns#">Bash</category><title>Print except few columns using awk - bash</title><description>&lt;span style="font-weight: bold;"&gt;Input file:&lt;/span&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;$&lt;/span&gt; cat details.txt&lt;br /&gt;AX|23.45|1932323|A|VI|-|Y|0&lt;br /&gt;TY|93.45|2932323|B|VI|-|Y|1&lt;br /&gt;RE|63.25|8932323|A|VI|0|N|1&lt;br /&gt;AY|83.85|0932323|C|VI|-|Y|0&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;Required: &lt;/span&gt;&lt;br /&gt;Print all columns from the above file except column number 2 and 7.&lt;br /&gt;i.e. required output:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;AX|1932323|A|VI|-|0&lt;br /&gt;TY|2932323|B|VI|-|1&lt;br /&gt;RE|8932323|A|VI|0|1&lt;br /&gt;AY|0932323|C|VI|-|0&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Basically for the above file we have to print column # 1,3,4,5,6,8&lt;br /&gt;&lt;br /&gt;i.e.&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;$&lt;/span&gt; awk '&lt;br /&gt;        BEGIN{FS=OFS="|"}{print $1,$3,$4,$5,$6,$8}&lt;br /&gt;' details.txt&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;But if number of fields is very large on the input file, the above method is not going to be so useful. So here is another technique.&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;$&lt;/span&gt; awk '&lt;br /&gt;    BEGIN{FS=OFS="|"}&lt;br /&gt;    { for (i=1; i&lt;=NF;i++)&lt;br /&gt;         if( i==2 || i==7 ) continue&lt;br /&gt;         else&lt;br /&gt;         printf("%s%s", $i,(i!=NF) ? OFS : ORS)}&lt;br /&gt;' details.txt&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;And if you want to exclude a range of column numbers (say exclude column 3 to column 6) here is my earlier &lt;a href="http://unstableme.blogspot.com/2008/11/print-range-of-columns-using-awk.html"&gt;post&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;An additional tip: &lt;/span&gt;&lt;br /&gt;Suppose you need to generate the print 'statement' for printing a number of consecutive fields for an awk program, here is a quick way:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;$&lt;/span&gt; seq -s ",$" 1 8 | sed 's/.*/{print $&amp;amp;}/'&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;Output:&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;{print $1,$2,$3,$4,$5,$6,$7,$8}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Or you can use the the for loop mentioned above.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8796493175195266816-1709954147433309031?l=unstableme.blogspot.com'/&gt;&lt;/div&gt;</description><link>http://unstableme.blogspot.com/2009/09/print-except-few-columns-using-awk-bash.html</link><author>noreply@blogger.com (Jadu Saikia)</author><thr:total xmlns:thr="http://purl.org/syndication/thread/1.0">0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-8796493175195266816.post-7915382791117516071</guid><pubDate>Fri, 18 Sep 2009 18:15:00 +0000</pubDate><atom:updated>2009-09-18T23:54:55.008+05:30</atom:updated><category domain="http://www.blogger.com/atom/ns#">Awk</category><category domain="http://www.blogger.com/atom/ns#">Linux Utilities</category><category domain="http://www.blogger.com/atom/ns#">Linux Commands</category><category domain="http://www.blogger.com/atom/ns#">ubuntu tips</category><category domain="http://www.blogger.com/atom/ns#">Bash</category><title>Find process running time in UNIX</title><description>Question. How to find out how long a process is running in an UNIX system ?&lt;br /&gt;&lt;br /&gt;Ans: Here are some tips to find the process' running time in an UNIX system.&lt;br /&gt;&lt;br /&gt;----------------&lt;br /&gt;For 2.6 kernels:&lt;br /&gt;----------------&lt;br /&gt;&lt;br /&gt;Identify your process Id&lt;br /&gt;&lt;br /&gt;and then do a&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;ls -ld /proc/PID-OF-YOUR-PROCESS&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;So the modification time listed on the above file(directory) is the time that the process has started.&lt;br /&gt;&lt;br /&gt;e.g. I have started a process say "sleep 10000" few minutes back&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 102, 102);"&gt;$&lt;/span&gt; ps -ef | grep "[s]leep 10000"&lt;br /&gt;jsaikia  &lt;span style="color: rgb(102, 0, 204);"&gt;24375&lt;/span&gt; 23306  0 22:13 pts/10   00:00:00 sleep 10000&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 102, 102);"&gt;$&lt;/span&gt; ls -ld /proc/&lt;span style="color: rgb(51, 51, 153);"&gt;24375&lt;/span&gt;&lt;br /&gt;dr-xr-xr-x 6 jsaikia staff 0 &lt;span style="color: rgb(0, 0, 153);"&gt;2009-09-18 22:14&lt;/span&gt; /proc/24375&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;So, "2009-09-18 22:14" is the start time of the above sleep process; if I subtract this time from the current time I can find how long this process has been running.&lt;br /&gt;&lt;br /&gt;For subtraction you can have a script like this:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;#!/bin/sh&lt;br /&gt;&lt;br /&gt;T1=$(date +%s -d "$1")&lt;br /&gt;T2=$(date +%s -d "$2")&lt;br /&gt;((diffsec=T1-T2))&lt;br /&gt;echo - \&lt;br /&gt;| awk -v D=$diffsec '{printf "%d:%d:%d\n",D/(60*60),D%(60*60)/60,D%60}'&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;So that you can execute like this:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 102, 102);"&gt;$&lt;/span&gt; sh cal-tdiff.sh "$(date)" "2009-09-18 22:14"&lt;br /&gt;0:22:56&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;----------------&lt;br /&gt;For 2.4 kernels:&lt;br /&gt;----------------&lt;br /&gt;For 2.4 kernels, the modification time on the "/proc/PID-OF-YOUR-PROCESS" will be the current system time (unlike 2.6 kernels where its the actual process start time)&lt;br /&gt;&lt;br /&gt;So how to find the running time of a process on a 2.4 kernel UNIX system ?&lt;br /&gt;&lt;br /&gt;Here is the way (this is going to work for 2.6 also)&lt;br /&gt;&lt;br /&gt;e.g.&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 102, 102);"&gt;$&lt;/span&gt; ps -ef | grep [s]leep&lt;br /&gt;root      7702  7689  0 17:34 pts/0    00:00:00 sleep 100000&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;so 7702 is the pid of the above process.&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 102, 102);"&gt;$&lt;/span&gt; pd=7702&lt;br /&gt;&lt;span style="color: rgb(255, 102, 102);"&gt;$&lt;/span&gt; expr $(awk '{print $1}' FS=\. /proc/uptime) - $(awk '{printf ("%10d\n",$22/100)}' /proc/$pd/stat)&lt;br /&gt;&lt;br /&gt;The output will show the number of seconds the process(with pid=pd) is running.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8796493175195266816-7915382791117516071?l=unstableme.blogspot.com'/&gt;&lt;/div&gt;</description><link>http://unstableme.blogspot.com/2009/09/find-process-running-time-in-unix.html</link><author>noreply@blogger.com (Jadu Saikia)</author><thr:total xmlns:thr="http://purl.org/syndication/thread/1.0">0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-8796493175195266816.post-2410622355655623677</guid><pubDate>Thu, 17 Sep 2009 17:38:00 +0000</pubDate><atom:updated>2009-09-17T23:16:56.076+05:30</atom:updated><category domain="http://www.blogger.com/atom/ns#">bash shell</category><category domain="http://www.blogger.com/atom/ns#">Awk</category><category domain="http://www.blogger.com/atom/ns#">eval in awk</category><category domain="http://www.blogger.com/atom/ns#">bash scripts</category><category domain="http://www.blogger.com/atom/ns#">bash shell newbie</category><category domain="http://www.blogger.com/atom/ns#">Bash</category><title>Bash script to copy required files</title><description>Contents of my "inputdir" is a set of files with filename like this:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;$&lt;/span&gt; ls -1 inputdir/&lt;br /&gt;log.10.16.1253168140.txt&lt;br /&gt;log.11.5.1253168345.txt&lt;br /&gt;log.11.9.1253168347.txt&lt;br /&gt;log.12.1.1253168347.txt&lt;br /&gt;log.19.1.1253168140.txt&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Directory "testcfgs" contains a set of config xmls.&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;$&lt;/span&gt; ls -1 testcfgs/&lt;br /&gt;cfg_10_16.xml&lt;br /&gt;cfg_10_5.xml&lt;br /&gt;cfg_11_5.xml&lt;br /&gt;cfg_11_9.xml&lt;br /&gt;cfg_12_1.xml&lt;br /&gt;cfg_19_1.xml&lt;br /&gt;cfg_19_2.xml&lt;br /&gt;cfg_91_9.xml&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Required:&lt;br /&gt;&lt;br /&gt;For each file of name "log.X.Y.timestamp.txt" in "inputdir", copy the corresponding "cfg_X_Y.xml" config file from "testcfgs" to a directory say "requiredcfgs".&lt;br /&gt;&lt;br /&gt;A simple practical bash one liner script:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;$&lt;/span&gt; for filename in $(ls -1 inputdir/)&lt;br /&gt;&lt;span style="color: rgb(51, 51, 255);"&gt;&gt;&lt;/span&gt; do&lt;br /&gt;&lt;span style="color: rgb(51, 51, 255);"&gt;&gt;&lt;/span&gt; X=$(echo "$filename" | cut -d"." -f2)&lt;br /&gt;&lt;span style="color: rgb(51, 51, 255);"&gt;&gt;&lt;/span&gt; Y=$(echo "$filename" | cut -d"." -f3)&lt;br /&gt;&lt;span style="color: rgb(51, 51, 255);"&gt;&gt;&lt;/span&gt; cp testcfgs/cfg_$X\_$Y.xml requiredcfgs/&lt;br /&gt;&lt;span style="color: rgb(51, 51, 255);"&gt;&gt;&lt;/span&gt; done&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;The two lines above for finding X and Y value can be replaced by a single line using 'eval with awk', like this:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;$&lt;/span&gt; for filename in $(ls -1 inputdir/)&lt;br /&gt;&lt;span style="color: rgb(51, 51, 255);"&gt;&gt;&lt;/span&gt; do&lt;br /&gt;&lt;span style="color: rgb(51, 51, 255);"&gt;&gt;&lt;/span&gt; eval $(echo "$filename" | awk -F "." '{print "X="$2";Y="$3}')&lt;br /&gt;&lt;span style="color: rgb(51, 51, 255);"&gt;&gt;&lt;/span&gt; cp testcfgs/cfg_$X\_$Y.xml requiredcfgs/&lt;br /&gt;&lt;span style="color: rgb(51, 51, 255);"&gt;&gt;&lt;/span&gt; done&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Contents of "requiredcfgs" directory after execution of the above bash script.&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;$&lt;/span&gt; ls -1 requiredcfgs/&lt;br /&gt;cfg_10_16.xml&lt;br /&gt;cfg_11_5.xml&lt;br /&gt;cfg_11_9.xml&lt;br /&gt;cfg_12_1.xml&lt;br /&gt;cfg_19_1.xml&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Related post on eval with awk:&lt;br /&gt;&lt;br /&gt;- &lt;a href="http://unstableme.blogspot.com/2008/08/subdivide-ip-address-awk-and-eval.html"&gt;Subdivide an ip address - assign each part to an variable using awk&lt;/a&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8796493175195266816-2410622355655623677?l=unstableme.blogspot.com'/&gt;&lt;/div&gt;</description><link>http://unstableme.blogspot.com/2009/09/bash-script-to-copy-required-files.html</link><author>noreply@blogger.com (Jadu Saikia)</author><thr:total xmlns:thr="http://purl.org/syndication/thread/1.0">3</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-8796493175195266816.post-1202529573249289946</guid><pubDate>Tue, 15 Sep 2009 03:51:00 +0000</pubDate><atom:updated>2009-09-15T09:37:38.004+05:30</atom:updated><category domain="http://www.blogger.com/atom/ns#">bash seq</category><category domain="http://www.blogger.com/atom/ns#">Linux Utilities</category><category domain="http://www.blogger.com/atom/ns#">Linux Commands</category><category domain="http://www.blogger.com/atom/ns#">bash shell newbie</category><title>Linux seq command format option example</title><description>I have already &lt;a href="http://unstableme.blogspot.com/2007/02/generating-loop-arguments-seq-command.html"&gt;post&lt;/a&gt; on Linux/UNIX seq command, using which we can generate sequence of numbers. Seq is very useful to generate loop arguments in UNIX bash scripting.&lt;br /&gt;&lt;br /&gt;One of very useful seq command line option is -f&lt;br /&gt;&lt;br /&gt;-f, --format=FORMAT&lt;br /&gt;use printf style floating-point FORMAT&lt;br /&gt;&lt;br /&gt;Lets see some simple examples on the same.&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;$&lt;/span&gt; seq -f "%04g" 3&lt;br /&gt;Output:&lt;br /&gt;0001&lt;br /&gt;0002&lt;br /&gt;0003&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;$&lt;/span&gt; seq -f "logfile%02g.txt" 10&lt;br /&gt;Output:&lt;br /&gt;logfile01.txt&lt;br /&gt;logfile02.txt&lt;br /&gt;logfile03.txt&lt;br /&gt;logfile04.txt&lt;br /&gt;logfile05.txt&lt;br /&gt;logfile06.txt&lt;br /&gt;logfile07.txt&lt;br /&gt;logfile08.txt&lt;br /&gt;logfile09.txt&lt;br /&gt;logfile10.txt&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Now, to create 10 files with names logfile01.txt, logfile02.txt ,....., logfile10.txt&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;$&lt;/span&gt; touch $(seq -f "logfile%02g.txt" 10)&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;$&lt;/span&gt; ls -1&lt;br /&gt;logfile01.txt&lt;br /&gt;logfile02.txt&lt;br /&gt;logfile03.txt&lt;br /&gt;logfile04.txt&lt;br /&gt;logfile05.txt&lt;br /&gt;logfile06.txt&lt;br /&gt;logfile07.txt&lt;br /&gt;logfile08.txt&lt;br /&gt;logfile09.txt&lt;br /&gt;logfile10.txt&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;FIRST INCREMENT LAST&lt;br /&gt;Sequence numbers between 1.0003 and 1.0012 with an increment of .00002&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;$&lt;/span&gt; seq -f "1.%04g" 3 2 12&lt;br /&gt;1.0003&lt;br /&gt;1.0005&lt;br /&gt;1.0007&lt;br /&gt;1.0009&lt;br /&gt;1.0011&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;-s is to specify separator between sequence numbers.&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;$&lt;/span&gt; seq -s "+" -f "1.%04g" 3 2 12&lt;br /&gt;1.0003+1.0005+1.0007+1.0009+1.0011&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;$&lt;/span&gt; seq -s "+" -f "1.%04g" 3 2 12 | bc&lt;br /&gt;5.0035&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Another good command for printing sequential and random data is jot, read &lt;a href="http://unstableme.blogspot.com/2007/12/jot-print-sequential-or-random-data.html"&gt;here&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;Related post:&lt;/span&gt;&lt;br /&gt;- &lt;a href="http://unstableme.blogspot.com/2008/12/ways-of-writing-bash-for-loop.html"&gt;Ways&lt;/a&gt; of writing for loops in bash scripting&lt;br /&gt;- &lt;a href="http://unstableme.blogspot.com/2009/03/print-text-in-style-box-bash-scripting.html"&gt;Print&lt;/a&gt; text within style box in bash scripting&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8796493175195266816-1202529573249289946?l=unstableme.blogspot.com'/&gt;&lt;/div&gt;</description><link>http://unstableme.blogspot.com/2009/09/linux-seq-command-format-option-example.html</link><author>noreply@blogger.com (Jadu Saikia)</author><thr:total xmlns:thr="http://purl.org/syndication/thread/1.0">0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-8796493175195266816.post-3221611076732837526</guid><pubDate>Thu, 10 Sep 2009 17:59:00 +0000</pubDate><atom:updated>2009-09-10T23:37:11.641+05:30</atom:updated><category domain="http://www.blogger.com/atom/ns#">Awk</category><category domain="http://www.blogger.com/atom/ns#">awk newbie</category><category domain="http://www.blogger.com/atom/ns#">bash shell newbie</category><category domain="http://www.blogger.com/atom/ns#">Bash</category><category domain="http://www.blogger.com/atom/ns#">awk array</category><title>Replace duplicate line with blank - awk</title><description>I just received an query as a comment on one of my older &lt;a href="http://unstableme.blogspot.com/2008/03/remove-duplicates-based-on-fields-awk.html"&gt;post&lt;/a&gt; on "removing duplicates based on fields using awk"&lt;br /&gt;&lt;br /&gt;Question was:&lt;br /&gt;&lt;br /&gt;Any Idea on how to replace duplicate line with blank line instead of deleting them?&lt;br /&gt;e.g.&lt;br /&gt;&lt;br /&gt;Input:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;test1&lt;br /&gt;test1&lt;br /&gt;test2&lt;br /&gt;test2&lt;br /&gt;test2&lt;br /&gt;test3&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Output:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;test1&lt;br /&gt;&lt;br /&gt;test2&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;test3&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Thought of making it a separate post here.&lt;br /&gt;&lt;br /&gt;The solution using awk:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;$&lt;/span&gt; awk 'x[$0]++ {$0=""} {print}' file.txt&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;Related post:&lt;br /&gt;- &lt;a href="http://unstableme.blogspot.com/2008/03/remove-duplicates-without-sorting-file.html"&gt;Remove duplicate without sorting file using awk&lt;/a&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8796493175195266816-3221611076732837526?l=unstableme.blogspot.com'/&gt;&lt;/div&gt;</description><link>http://unstableme.blogspot.com/2009/09/replace-duplicate-line-with-blank-awk.html</link><author>noreply@blogger.com (Jadu Saikia)</author><thr:total xmlns:thr="http://purl.org/syndication/thread/1.0">4</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-8796493175195266816.post-4418259021299180993</guid><pubDate>Wed, 09 Sep 2009 17:57:00 +0000</pubDate><atom:updated>2009-09-09T23:59:17.295+05:30</atom:updated><category domain="http://www.blogger.com/atom/ns#">bash shell</category><category domain="http://www.blogger.com/atom/ns#">Awk</category><category domain="http://www.blogger.com/atom/ns#">awk newbie</category><category domain="http://www.blogger.com/atom/ns#">bash shell newbie</category><category domain="http://www.blogger.com/atom/ns#">Bash</category><title>Split file using awk - few examples</title><description>One of good use of awk is splitting files (based on different conditions) into sub-files.&lt;br /&gt;Lets see some examples:&lt;br /&gt;&lt;br /&gt;Example 1:&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;Input file log1.txt:&lt;/span&gt;&lt;br /&gt;- Line starting with H is the main header line.&lt;br /&gt;- Line starting with "h" and subsequent lines starting with "s" (till the next "h" line) are part of the same entry/section.&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;$&lt;/span&gt; cat log1.txt&lt;br /&gt;H,&lt;span style="color: rgb(102, 102, 204);"&gt;555&lt;/span&gt;,etho0&lt;br /&gt;h,&lt;span style="color: rgb(204, 51, 204);"&gt;1&lt;/span&gt;,3&lt;br /&gt;s,1233,456456,1212&lt;br /&gt;s,4251,452456,7215&lt;br /&gt;s,6283,851456,1219&lt;br /&gt;h,&lt;span style="color: rgb(204, 51, 204);"&gt;9&lt;/span&gt;,2&lt;br /&gt;s,2233,156456,1912&lt;br /&gt;s,9233,256456,8212&lt;br /&gt;h,&lt;span style="color: rgb(204, 51, 204);"&gt;2&lt;/span&gt;,4&lt;br /&gt;s,4233,456456,1212&lt;br /&gt;s,7251,252456,7215&lt;br /&gt;s,1288,851456,9219&lt;br /&gt;s,9183,851456,6219&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;Required:&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;- Split or subdivide the above file into sub files corresponding to each entry (one entry being the section starting with "h" and "s" lines till the next "h" line)&lt;br /&gt;- Each sub-file should contain(start with) the main header line ("H" line).&lt;br /&gt;- The required output is 3 sub files with the following contents and filename convention.&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;$&lt;/span&gt; cat &lt;span style="color: rgb(102, 102, 204);"&gt;555&lt;/span&gt;.&lt;span style="color: rgb(204, 51, 204);"&gt;1&lt;/span&gt;.1.log&lt;br /&gt;H,555,etho0&lt;br /&gt;h,1,3&lt;br /&gt;s,1233,456456,1212&lt;br /&gt;s,4251,452456,7215&lt;br /&gt;s,6283,851456,1219&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;$&lt;/span&gt; cat &lt;span style="color: rgb(51, 102, 255);"&gt;555&lt;/span&gt;.&lt;span style="color: rgb(204, 51, 204);"&gt;9.&lt;/span&gt;2.log&lt;br /&gt;H,555,etho0&lt;br /&gt;h,9,2&lt;br /&gt;s,2233,156456,1912&lt;br /&gt;s,9233,256456,8212&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;$&lt;/span&gt; cat &lt;span style="color: rgb(102, 102, 204);"&gt;555&lt;/span&gt;.&lt;span style="color: rgb(153, 51, 153);"&gt;2&lt;/span&gt;.3.log&lt;br /&gt;H,555,etho0&lt;br /&gt;h,2,4&lt;br /&gt;s,4233,456456,1212&lt;br /&gt;s,7251,252456,7215&lt;br /&gt;s,1288,851456,9219&lt;br /&gt;s,9183,851456,6219&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;The awk program:&lt;/span&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;$&lt;/span&gt; awk -F "," '&lt;br /&gt;$1=="H" {mainH=$0;id=$2;next}&lt;br /&gt;/^h/{&lt;br /&gt;       hid=$2;close(id"."hid"."f".log")&lt;br /&gt;       f++&lt;br /&gt;       print mainH &amp;gt; id"."hid"."f".log"&lt;br /&gt;    }&lt;br /&gt;{print $0 &amp;gt; id"."hid"."f".log"}&lt;br /&gt;' log1.txt&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Example 2:&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;Input file:&lt;/span&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 102, 102);"&gt;$&lt;/span&gt; cat log.txt&lt;br /&gt;1252468812,yahoo,3.5&lt;br /&gt;1252468812,hotmail,2.4&lt;br /&gt;1252468819,yahoo,1.2&lt;br /&gt;1252468812,msn,8.9&lt;br /&gt;1252468923,gmail,12&lt;br /&gt;1252468819,live,3.4&lt;br /&gt;1252468929,yahoo,9.0&lt;br /&gt;1252468929,msn,1.2&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;Required:&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;a) Split the above files based on the first field (i.e. lines with same first field should go to the same file)&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;The awk one liner:&lt;/span&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 102, 102);"&gt;$&lt;/span&gt; awk -F "," '{close(f);f=$1}{print &amp;gt; f".txt"}' log.txt&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;Output:&lt;/span&gt;&lt;br /&gt;Above file is splited into the following sub-files.&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;$&lt;/span&gt; cat 1252468812.txt&lt;br /&gt;1252468812,yahoo,3.5&lt;br /&gt;1252468812,hotmail,2.4&lt;br /&gt;1252468812,msn,8.9&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 102, 102);"&gt;$&lt;/span&gt; cat 1252468819.txt&lt;br /&gt;1252468819,yahoo,1.2&lt;br /&gt;1252468819,live,3.4&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 102, 102);"&gt;$&lt;/span&gt; cat 1252468923.txt&lt;br /&gt;1252468923,gmail,12&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 102, 102);"&gt;$&lt;/span&gt; cat 1252468929.txt&lt;br /&gt;1252468929,yahoo,9.0&lt;br /&gt;1252468929,msn,1.2&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;b) Send every 3 lines of above file into a sub file.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;The awk code:&lt;/span&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 102, 102);"&gt;$&lt;/span&gt; awk '{print &amp;gt;("log_" int((NR+2)/3))}' log.txt&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;Output: &lt;/span&gt;&lt;br /&gt;The sub-files generated.&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;$&lt;/span&gt; cat log_1&lt;br /&gt;1252468812,yahoo,3.5&lt;br /&gt;1252468812,hotmail,2.4&lt;br /&gt;1252468819,yahoo,1.2&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;$ &lt;/span&gt;cat log_2&lt;br /&gt;1252468812,msn,8.9&lt;br /&gt;1252468923,gmail,12&lt;br /&gt;1252468819,live,3.4&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;$&lt;/span&gt; cat log_3&lt;br /&gt;1252468929,yahoo,9.0&lt;br /&gt;1252468929,msn,1.2&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;Related post:&lt;/span&gt;&lt;br /&gt;&lt;div style="text-align: left;"&gt;- &lt;a href="http://unstableme.blogspot.com/2009/04/split-file-based-on-start-pattern-awk.html"&gt;Split file based on start pattern using awk&lt;/a&gt;&lt;br /&gt;- &lt;a href="http://unstableme.blogspot.com/2008/12/split-file-based-on-pattern-awk.html"&gt;Awk - Split file into subfiles based on pattern&lt;/a&gt;&lt;br /&gt;- &lt;a href="http://unstableme.blogspot.com/2008/03/send-alternate-lines-to-separate-files.html"&gt;Send alternate lines of a file into separate sub-file using awk&lt;/a&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8796493175195266816-4418259021299180993?l=unstableme.blogspot.com'/&gt;&lt;/div&gt;</description><link>http://unstableme.blogspot.com/2009/09/split-file-using-awk-few-examples.html</link><author>noreply@blogger.com (Jadu Saikia)</author><thr:total xmlns:thr="http://purl.org/syndication/thread/1.0">2</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-8796493175195266816.post-3707023221606968845</guid><pubDate>Tue, 08 Sep 2009 03:11:00 +0000</pubDate><atom:updated>2009-09-08T08:58:13.722+05:30</atom:updated><category domain="http://www.blogger.com/atom/ns#">awk if else</category><category domain="http://www.blogger.com/atom/ns#">Awk Functions</category><category domain="http://www.blogger.com/atom/ns#">Awk</category><category domain="http://www.blogger.com/atom/ns#">awk newbie</category><category domain="http://www.blogger.com/atom/ns#">awk substr</category><title>Print first character of a field - awk substr</title><description>Input file:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;$&lt;/span&gt; cat file.txt&lt;br /&gt;8965,1212,c32,1&lt;br /&gt;1221,9000,d90,0&lt;br /&gt;1222,7823,,2&lt;br /&gt;9012,1901,c12,7&lt;br /&gt;9012,1342,t90,9&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Output Required: If 3rd field is non empty, print only the first character of the value, else(i.e. when the field is blank) print "NA" in the 3rd field.&lt;br /&gt;i.e. required output:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;8965,1212,c,1&lt;br /&gt;1221,9000,d,0&lt;br /&gt;1222,7823,NA,2&lt;br /&gt;9012,1901,c,7&lt;br /&gt;9012,1342,t,9&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;The awk program:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;$&lt;/span&gt; awk '&lt;br /&gt;    BEGIN {FS=OFS=","}&lt;br /&gt;   { if ( length($3) ) { $3 = substr($3, 0, 1) }&lt;br /&gt;     else { $3 = "NA" }&lt;br /&gt;     print&lt;br /&gt;   }&lt;br /&gt;' file.txt&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Related post:&lt;br /&gt;- &lt;a href="http://unstableme.blogspot.com/2008/05/awk-substr-function.html"&gt;Brief &lt;/a&gt;about awk substr function&lt;br /&gt;- &lt;a href="http://unstableme.blogspot.com/2008/01/blank-column-in-file-awk-newbie.html"&gt;Blank column&lt;/a&gt; in file - awk newbie&lt;br /&gt;- &lt;a href="http://unstableme.blogspot.com/2009/01/count-non-empty-fields-in-file-awk.html"&gt;Count non empty field in file using awk&lt;/a&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8796493175195266816-3707023221606968845?l=unstableme.blogspot.com'/&gt;&lt;/div&gt;</description><link>http://unstableme.blogspot.com/2009/09/print-first-character-of-field-awk.html</link><author>noreply@blogger.com (Jadu Saikia)</author><thr:total xmlns:thr="http://purl.org/syndication/thread/1.0">0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-8796493175195266816.post-8318008910577513620</guid><pubDate>Mon, 07 Sep 2009 03:01:00 +0000</pubDate><atom:updated>2009-09-07T08:34:48.864+05:30</atom:updated><category domain="http://www.blogger.com/atom/ns#">bash parameter substitution</category><category domain="http://www.blogger.com/atom/ns#">bash truncate</category><category domain="http://www.blogger.com/atom/ns#">bash scripts</category><category domain="http://www.blogger.com/atom/ns#">bash shell newbie</category><category domain="http://www.blogger.com/atom/ns#">Bash</category><title>Truncate string using bash script</title><description>Input file:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;$&lt;/span&gt; cat spears.txt&lt;br /&gt;Baby-One-More-Time.mp3&lt;br /&gt;Autumn-Goodbye.mp3&lt;br /&gt;Baby-One-More-Time.mp3&lt;br /&gt;Cant-Make-You-Love-Me.wmv&lt;br /&gt;Crazy.mp3&lt;br /&gt;Crazy---Stop-Remix.mp3&lt;br /&gt;Dont-Go-Knocking-on-My-Door.mp3&lt;br /&gt;Dont-Let-Me-Be-The-Last-to-Know.flv&lt;br /&gt;From-The-Bottom-of-My-Broken-Heart.mp3&lt;br /&gt;Im-Not-a-Girl-Not-Yet-a-Woman.mp3&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;Required: Truncate the lines of the above file (filename part, and not the extension) to 15 character long.&lt;br /&gt;Also insert a string "..." in between the filename part and extension in case the line is truncated.&lt;br /&gt;&lt;br /&gt;The bash script:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;#!/bin/sh&lt;br /&gt;#Bash Script to truncate string&lt;br /&gt;#&lt;br /&gt;&lt;br /&gt;while read filename&lt;br /&gt;    do&lt;br /&gt;        name=${filename%%.*}&lt;br /&gt;        extn=${filename##*.}&lt;br /&gt;        if [ ${#name} -gt 15 ]&lt;br /&gt;            then&lt;br /&gt;                nfile=$(echo $name | cut -c1-15)&lt;br /&gt;                fullname=${nfile}...${extn}&lt;br /&gt;                echo $fullname&lt;br /&gt;            else&lt;br /&gt;                echo $filename&lt;br /&gt;        fi&lt;br /&gt;    done &amp;lt; spears.txt &amp;gt; spears.txt.truncated&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;The output file produced after execution of the above bash script:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;$ &lt;/span&gt;cat spears.txt.truncated&lt;br /&gt;Baby-One-More-T...mp3&lt;br /&gt;Autumn-Goodbye.mp3&lt;br /&gt;Baby-One-More-T...mp3&lt;br /&gt;Cant-Make-You-L...wmv&lt;br /&gt;Crazy.mp3&lt;br /&gt;Crazy---Stop-Re...mp3&lt;br /&gt;Dont-Go-Knockin...mp3&lt;br /&gt;Dont-Let-Me-Be-...flv&lt;br /&gt;From-The-Bottom...mp3&lt;br /&gt;Im-Not-a-Girl-N...mp3&lt;br /&gt;&lt;/pre&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8796493175195266816-8318008910577513620?l=unstableme.blogspot.com'/&gt;&lt;/div&gt;</description><link>http://unstableme.blogspot.com/2009/09/truncate-string-using-bash-script.html</link><author>noreply@blogger.com (Jadu Saikia)</author><thr:total xmlns:thr="http://purl.org/syndication/thread/1.0">7</thr:total></item></channel></rss>
