<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/atom10full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><feed xmlns="http://www.w3.org/2005/Atom" xmlns:openSearch="http://a9.com/-/spec/opensearch/1.1/" xmlns:blogger="http://schemas.google.com/blogger/2008" xmlns:georss="http://www.georss.org/georss" xmlns:gd="http://schemas.google.com/g/2005" xmlns:thr="http://purl.org/syndication/thread/1.0" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" gd:etag="W/&quot;AkMDQXk6cSp7ImA9WhBaEU4.&quot;"><id>tag:blogger.com,1999:blog-8796493175195266816</id><updated>2013-05-21T17:44:30.719+05:30</updated><category term="linux dd" /><category term="nohup" /><category term="awk for loop" /><category term="linux diff" /><category term="awk variable assignment" /><category term="du command" /><category term="awk patterns" /><category term="bash egrep command" /><category term="bash for" /><category term="unix date" /><category term="Linux top" /><category term="awk variables" /><category term="bash command line argument" /><category term="Bash process substitution" /><category term="ubuntu tips" /><category term="bash and mysql" /><category term="Solaris" /><category term="Unix less" /><category term="awk report manipulation" /><category term="linux ls" /><category term="eval in awk" /><category term="awk replacement" /><category term="xml" /><category term="Linux Utilities" /><category term="SSH" /><category term="bash scripts questions" /><category term="Awk strftime" /><category term="linux dialog" /><category term="bash tricks" /><category term="bash until loop" /><category term="awk split" /><category term="bash shell" /><category term="Expect" /><category term="exec" /><category term="bash printf" /><category term="find command" /><category term="awk array" /><category term="Bash join" /><category term="sed replacement" /><category term="bash scripts" /><category term="linux paste" /><category term="Sed" /><category term="bash function" /><category term="Bash" /><category term="UNIX screen" /><category term="awk if else" /><category term="Awk Functions" /><category term="Bash Menu" /><category term="awk RS" /><category term="sed newbie" /><category term="Bash if else" /><category term="linux sudo" /><category term="awk NR" /><category term="bash read" /><category term="xargs" /><category term="linux tr" /><category term="UNIX rename" /><category term="python newbie" /><category term="bash desk calculator" /><category term="bash shell newbie" /><category term="NW" /><category term="bash extglob" /><category term="unix epoch" /><category term="awk asort" /><category term="awk FNR" /><category term="awk gsub" /><category term="awk substr" /><category term="vi handy commands" /><category term="bc" /><category term="command history" /><category term="CGI" /><category term="Awk" /><category term="vi editor tips" /><category term="awk groupby" /><category term="Linux Commands" /><category term="bash shopt" /><category term="getopts" /><category term="operating system" /><category term="vimdiff" /><category term="Bash Array" /><category term="UNIX time" /><category term="bash truncate" /><category term="awk next" /><category term="bash parameter substitution" /><category term="wcalc" /><category term="bash echo" /><category term="awk ORS" /><category term="linux cut" /><category term="awk newbie" /><category term="awk concatenation" /><category term="awk getline" /><category term="bash loop" /><category term="linux redirection" /><category term="bash seq" /><category term="linux grep" /><category term="Perl One liner" /><category term="bash env variable" /><category term="awk printf" /><category term="VIM" /><category term="matrix operations using awk" /><category term="ftp" /><category term="Bash Substring Removal" /><category term="bash while" /><category term="bash sort" /><category term="UNIX look command" /><title>UNIX BASH scripting</title><subtitle type="html">Dedicated to all BASH newbies and Linux one liner lovers.
Useful AWK,SED,BASH one liners.</subtitle><link rel="http://schemas.google.com/g/2005#feed" type="application/atom+xml" href="http://unstableme.blogspot.com/feeds/posts/default" /><link rel="alternate" type="text/html" href="http://unstableme.blogspot.com/" /><link rel="next" type="application/atom+xml" href="http://www.blogger.com/feeds/8796493175195266816/posts/default?start-index=26&amp;max-results=25&amp;redirect=false&amp;v=2" /><author><name>Jadu Saikia</name><uri>https://plus.google.com/106333484094067246933</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="//lh4.googleusercontent.com/-wlNtTFQsFC4/AAAAAAAAAAI/AAAAAAAAFR8/rdbW2k6jirU/s512-c/photo.jpg" /></author><generator version="7.00" uri="http://www.blogger.com">Blogger</generator><openSearch:totalResults>415</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openSearch:itemsPerPage><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/atom+xml" href="http://feeds.feedburner.com/bashscripting" /><feedburner:info uri="bashscripting" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><entry gd:etag="W/&quot;A0MGQXs6eyp7ImA9WhBUGUU.&quot;"><id>tag:blogger.com,1999:blog-8796493175195266816.post-8183031634177536371</id><published>2013-05-08T10:33:00.004+05:30</published><updated>2013-05-08T10:33:40.513+05:30</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2013-05-08T10:33:40.513+05:30</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="awk if else" /><category scheme="http://www.blogger.com/atom/ns#" term="Awk" /><category scheme="http://www.blogger.com/atom/ns#" term="awk newbie" /><title>Unix - Append 0 to single digit date</title><content type="html">&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;
Input file file.txt has dates in month/day/year format. 
&lt;br /&gt;
&lt;pre&gt;&lt;span style="color: red;"&gt;$&lt;/span&gt; cat file.txt 
3/4/2013
3/10/2013
10/4/2013
12/10/2012
&lt;/pre&gt;
Required: Add prefix 0 to first and second field if its a single digit. &lt;br /&gt;
&lt;br /&gt;
Awk solution:
&lt;br /&gt;
&lt;pre&gt;&lt;span style="color: red;"&gt;$&lt;/span&gt; awk 'BEGIN {FS=OFS="/"} 
    { 
 if (length($1) == 1) $1="0"$1
 if (length($2) == 1) $2="0"$2
        { print }
}' file.txt
&lt;/pre&gt;
Output:
&lt;br /&gt;
&lt;pre&gt;03/04/2013
03/10/2013
10/04/2013
12/10/2012
&lt;/pre&gt;
Related posts: &lt;br /&gt;
- &lt;a href="http://unstableme.blogspot.in/2010/11/newbie-tutorial-on-unix-awk-utility.html"&gt;A newbie tutorial on Unix Awk&lt;/a&gt;&lt;br /&gt;
- &lt;a href="http://unstableme.blogspot.in/2009/09/if-else-examples-in-awk-bash.html"&gt;Awk if else&lt;/a&gt;&lt;br /&gt;
- &lt;a href="http://unstableme.blogspot.in/2008/07/convert-date-format-using-sed-awk-perl.html"&gt;Convert date format in unix using awk and sed&lt;/a&gt;&lt;br /&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;img src="http://feeds.feedburner.com/~r/bashscripting/~4/2jEkbm9Gpr8" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://unstableme.blogspot.com/feeds/8183031634177536371/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=8796493175195266816&amp;postID=8183031634177536371" title="2 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/8796493175195266816/posts/default/8183031634177536371?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/8796493175195266816/posts/default/8183031634177536371?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/bashscripting/~3/2jEkbm9Gpr8/unix-append-0-to-single-digit-date.html" title="Unix - Append 0 to single digit date" /><author><name>Jadu Saikia</name><uri>https://plus.google.com/106333484094067246933</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="//lh4.googleusercontent.com/-wlNtTFQsFC4/AAAAAAAAAAI/AAAAAAAAFR8/rdbW2k6jirU/s512-c/photo.jpg" /></author><thr:total>2</thr:total><feedburner:origLink>http://unstableme.blogspot.com/2013/05/unix-append-0-to-single-digit-date.html</feedburner:origLink></entry><entry gd:etag="W/&quot;C08AQ3Y6fip7ImA9WhBREE8.&quot;"><id>tag:blogger.com,1999:blog-8796493175195266816.post-5118065520929847874</id><published>2013-02-28T08:54:00.000+05:30</published><updated>2013-02-28T08:54:02.816+05:30</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2013-02-28T08:54:02.816+05:30</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="UNIX time" /><category scheme="http://www.blogger.com/atom/ns#" term="bash shell" /><category scheme="http://www.blogger.com/atom/ns#" term="linux redirection" /><category scheme="http://www.blogger.com/atom/ns#" term="Linux Utilities" /><category scheme="http://www.blogger.com/atom/ns#" term="Linux Commands" /><category scheme="http://www.blogger.com/atom/ns#" term="bash shell newbie" /><category scheme="http://www.blogger.com/atom/ns#" term="Bash" /><title>UNIX time command output redirect to file</title><content type="html">&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;
As you know the 'time' command run programs and summarize system resource usage. Here is a way how you can redirect 'time' command output to file. I would recomend you to go through &lt;a href="http://www.trentu.ca/academic/physics/batkinson/scalapack_docs/node18.html"&gt;this page&lt;/a&gt; to understand more about 'time' command.&lt;br /&gt;
&lt;br /&gt;
e.g. We are executing 'prog.sh' along with 'time' command.
&lt;br /&gt;
&lt;pre&gt;&lt;span style="color: red;"&gt;$&lt;/span&gt; time ./prog.sh 
Initiating merge
Merge completed

real	0m8.803s
user	0m0.010s
sys	0m0.000s
&lt;/pre&gt;
#Trying to redirect the output to a file
&lt;br /&gt;
&lt;pre&gt;&lt;span style="color: red;"&gt;$&lt;/span&gt; time ./prog.sh &amp;gt; out.txt

real	0m8.804s
user	0m0.020s
sys	0m0.000s
&lt;/pre&gt;
#out.txt content:
&lt;br /&gt;
&lt;pre&gt;&lt;span style="color: red;"&gt;$&lt;/span&gt; cat out.txt 
Initiating merge
Merge completed
&lt;/pre&gt;
#So it only redirected the STDOUT of the script executed, but the 'time' command outut is not redirected. &lt;br /&gt;
#This is becuase the command time sends it's output to STDERR (instead of STDOUT)&lt;br /&gt;
#To capture output of 'time' command:
&lt;br /&gt;
&lt;pre&gt;&lt;span style="color: red;"&gt;$&lt;/span&gt; { time ./prog.sh ; } 2&amp;gt; out.txt
Initiating merge
Merge completed
&lt;/pre&gt;
#Now out.txt content:
&lt;br /&gt;
&lt;pre&gt;&lt;span style="color: red;"&gt;$&lt;/span&gt; cat out.txt 

real	0m8.303s
user	0m0.010s
sys	0m0.000s
&lt;/pre&gt;
#And to capture output of script as well as time command:
&lt;br /&gt;
&lt;pre&gt;&lt;span style="color: red;"&gt;$&lt;/span&gt; { time ./prog.sh ; } &amp;amp;&amp;gt; out.txt
&lt;/pre&gt;
#'out.txt' now has both the outputs.
&lt;br /&gt;
&lt;pre&gt;&lt;span style="color: red;"&gt;$&lt;/span&gt; cat out.txt 
Initiating merge
Merge completed

real	0m8.303s
user	0m0.020s
sys	0m0.000s
&lt;/pre&gt;
As I have mentioned in my earlier &lt;a href="http://unstableme.blogspot.in/2008/03/understand-your-codes-performance-using.html"&gt;post&lt;/a&gt; on UNIX 'time' command, there's two types of time command available:&lt;br /&gt;
1) Shell's in-build time: Gives only scheduler information&lt;br /&gt;
2) /usr/bin/time: Gives more information, also allows formatting the output&lt;br /&gt;
&lt;br /&gt;
The second (/usr/bin/time) one accepts output redirection without code block:
&lt;br /&gt;
&lt;pre&gt;&lt;span style="color: red;"&gt;$&lt;/span&gt; /usr/bin/time ./prog.sh &amp;amp;&amp;gt; newout.txt

&lt;span style="color: red;"&gt;$&lt;/span&gt; cat newout.txt 
Initiating merge
Merge completed
0.00user 0.01system 0:08.30elapsed 0%CPU (0avgtext+0avgdata 5728maxresident)k
0inputs+8outputs (0major+719minor)pagefaults 0swaps
&lt;/pre&gt;
Related posts:&lt;br /&gt;
- &lt;a href="http://unstableme.blogspot.in/2009/01/redirecting-man-pages-to-file-linux.html"&gt;UNIX redirect man pages to file&lt;/a&gt;&lt;br /&gt;
- &lt;a href="http://unstableme.blogspot.in/2008/12/redirect-top-command-output-to-file.html"&gt;Redirect UNIX top command output to file&lt;/a&gt;&lt;br /&gt;
- &lt;a href="http://unstableme.blogspot.in/2008/08/redirect-both-stderr-and-stdout-to.html"&gt;UNIX redirect both stderr and stdout to file&lt;/a&gt;&lt;br /&gt;
- &lt;a href="http://unstableme.blogspot.in/2012/02/run-unix-bash-loop-with-nohup.html"&gt;Run UNIX bash loop with nohup command&lt;/a&gt;&lt;/div&gt;
&lt;img src="http://feeds.feedburner.com/~r/bashscripting/~4/WPcl03Z8bbs" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://unstableme.blogspot.com/feeds/5118065520929847874/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=8796493175195266816&amp;postID=5118065520929847874" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/8796493175195266816/posts/default/5118065520929847874?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/8796493175195266816/posts/default/5118065520929847874?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/bashscripting/~3/WPcl03Z8bbs/unix-time-command-output-redirect-to.html" title="UNIX time command output redirect to file" /><author><name>Jadu Saikia</name><uri>https://plus.google.com/106333484094067246933</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="//lh4.googleusercontent.com/-wlNtTFQsFC4/AAAAAAAAAAI/AAAAAAAAFR8/rdbW2k6jirU/s512-c/photo.jpg" /></author><thr:total>0</thr:total><feedburner:origLink>http://unstableme.blogspot.com/2013/02/unix-time-command-output-redirect-to.html</feedburner:origLink></entry><entry gd:etag="W/&quot;DUIGQns7cSp7ImA9WhBTGUk.&quot;"><id>tag:blogger.com,1999:blog-8796493175195266816.post-4623789452762498863</id><published>2013-02-15T22:27:00.003+05:30</published><updated>2013-02-15T22:28:43.509+05:30</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2013-02-15T22:28:43.509+05:30</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="sed replacement" /><category scheme="http://www.blogger.com/atom/ns#" term="Sed" /><category scheme="http://www.blogger.com/atom/ns#" term="bash function" /><category scheme="http://www.blogger.com/atom/ns#" term="bash command line argument" /><category scheme="http://www.blogger.com/atom/ns#" term="getopts" /><category scheme="http://www.blogger.com/atom/ns#" term="bash shell newbie" /><category scheme="http://www.blogger.com/atom/ns#" term="sed newbie" /><category scheme="http://www.blogger.com/atom/ns#" term="bash while" /><title>Bash - convert delimited file to html table</title><content type="html">&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;
Here is a simple UNIX bash script that can be used to convert a simple delimited file to a HTML table format. Default delimiter if not mentioned as part of the command line argument to this script will be comma. 

&lt;br /&gt;
&lt;pre&gt;#!/bin/sh
#convert_2_html_table.sh 
#Converts a delimited file to a HTML table
#Jadu Saikia http://unstableme.blogspot.in

NOARG=64

#usage function
f_Usage () {
echo "Usage: $(basename $0) -d &amp;lt;delimiter&amp;gt; -f &amp;lt;delimited-file&amp;gt;"
}

#command line args
while getopts d:f: OPTION
do
    case $OPTION in
        d)  DELIMITER=$OPTARG ;;
        f)  INFILE=$OPTARG ;;
    esac
done

#Less than 2 command line argument, throw Usage
[ "$#" -lt 2 ] &amp;amp;&amp;amp; f_Usage &amp;amp;&amp;amp; exit $NOARG

DEFAULTDELIMITER=","
#If no delimiter is supplied, default delimiter is comma i.e. ,
SEPARATOR=${DELIMITER:-$DEFAULTDELIMITER}

if [ -f "${INFILE}" ]
        then
                printf "&amp;lt;table border=\"1\"&amp;gt;"
                sed "s/$SEPARATOR/&amp;lt;\/td&amp;gt;&amp;lt;td&amp;gt;/g" $INFILE | while read line
                        do
                                printf "&amp;lt;tr&amp;gt;&amp;lt;td&amp;gt;${line}&amp;lt;/td&amp;gt;&amp;lt;/tr&amp;gt;"
                done
                printf "&amp;lt;/table&amp;gt;"
                echo
fi

&lt;/pre&gt;
e.g. Input file:
&lt;br /&gt;
&lt;pre&gt;$ cat data.txt 
First Name:Last Name:Points
Alex:Hall:45
Niraj:Kumar:290
Brian:Smith:100
&lt;/pre&gt;
Executing it:
&lt;br /&gt;
&lt;pre&gt;$ ./convert_2_html_table.sh -d ":" -f data.txt  &amp;gt; data.html
&lt;/pre&gt;
&lt;a href="http://1.bp.blogspot.com/-r7PsGwwuMWM/UR5n2JMMGHI/AAAAAAAAFSc/-atCvP2OGM4/s1600/Unix-Bash-convert-delimited-file-to-html-table.png" imageanchor="1"&gt;&lt;img border="0" src="http://1.bp.blogspot.com/-r7PsGwwuMWM/UR5n2JMMGHI/AAAAAAAAFSc/-atCvP2OGM4/s320/Unix-Bash-convert-delimited-file-to-html-table.png" /&gt;&amp;nbsp;&lt;/a&gt;&lt;br /&gt;
&amp;nbsp;Related posts:&lt;br /&gt;
- &lt;a href="http://unstableme.blogspot.in/2007/12/setting-default-value-for-shell.html"&gt;Setting default value for Bash shell variable&lt;/a&gt;&lt;br /&gt;
- &lt;a href="http://unstableme.blogspot.in/2008/03/accessing-external-variable-in-awk-and.html"&gt;Accessing external variable in SED and AWK&lt;/a&gt;&lt;br /&gt;
- &lt;a href="http://unstableme.blogspot.in/2009/10/bash-while-loop-sum-issue-explained.html"&gt;Issue with Bash while loop during SUM&lt;/a&gt;&lt;/div&gt;
&lt;img src="http://feeds.feedburner.com/~r/bashscripting/~4/UDIFubzCQD4" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://unstableme.blogspot.com/feeds/4623789452762498863/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=8796493175195266816&amp;postID=4623789452762498863" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/8796493175195266816/posts/default/4623789452762498863?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/8796493175195266816/posts/default/4623789452762498863?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/bashscripting/~3/UDIFubzCQD4/bash-convert-delimited-file-to-html.html" title="Bash - convert delimited file to html table" /><author><name>Jadu Saikia</name><uri>https://plus.google.com/106333484094067246933</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="//lh4.googleusercontent.com/-wlNtTFQsFC4/AAAAAAAAAAI/AAAAAAAAFR8/rdbW2k6jirU/s512-c/photo.jpg" /></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://1.bp.blogspot.com/-r7PsGwwuMWM/UR5n2JMMGHI/AAAAAAAAFSc/-atCvP2OGM4/s72-c/Unix-Bash-convert-delimited-file-to-html-table.png" height="72" width="72" /><thr:total>0</thr:total><feedburner:origLink>http://unstableme.blogspot.com/2013/02/bash-convert-delimited-file-to-html.html</feedburner:origLink></entry><entry gd:etag="W/&quot;Dk8FQnszcSp7ImA9WhNQGEs.&quot;"><id>tag:blogger.com,1999:blog-8796493175195266816.post-2496397111758386033</id><published>2012-11-25T23:03:00.002+05:30</published><updated>2012-11-25T23:03:33.589+05:30</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2012-11-25T23:03:33.589+05:30</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="UNIX screen" /><category scheme="http://www.blogger.com/atom/ns#" term="Linux Utilities" /><category scheme="http://www.blogger.com/atom/ns#" term="Linux Commands" /><category scheme="http://www.blogger.com/atom/ns#" term="bash shell newbie" /><category scheme="http://www.blogger.com/atom/ns#" term="Bash" /><title>Beginning of line in UNIX screen session</title><content type="html">&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;
As you know every "screen" command begins with "Ctrl-a", then how to go to beginning of the line when you are working under a "screen" UNIX session (which we achieve by "Ctrl-a" in a normal UNIX session) ? &lt;br /&gt;&lt;br /&gt;Solution:&lt;br /&gt;Under a "screen" UNIX session you can do "Ctrl-a a" to move to the beginning of the line. &lt;br /&gt;&lt;br /&gt;So in order to print ^A (hex: \x01) in a UNIX "screen" session, usual&amp;nbsp; "Ctrl-v Ctrl-a" will not work, you will type "Ctrl-v Ctrl-a a" to print ^A.&lt;br /&gt;
&lt;br /&gt;
Related posts:&lt;br /&gt;
- &lt;a href="http://unstableme.blogspot.in/2009/08/bash-save-command-without-executing-it.html"&gt;Save command without executing it in Bash history&lt;/a&gt;&lt;br /&gt;
- &lt;a href="http://unstableme.blogspot.in/2009/04/ctrl-s-in-linux-shell-vi-hangs.html"&gt;Ctrl s freezes UNIX shell and vi editor&lt;/a&gt;&lt;br /&gt;
- &lt;a href="http://unstableme.blogspot.in/2010/01/prevent-exit-of-shell-with-ctrl-d.html"&gt;UNIX - prevent exit of shell with Ctrl-d&lt;/a&gt;&lt;br /&gt;
- &lt;a href="http://unstableme.blogspot.in/2009/01/linux-command-line-history-with.html"&gt;Print UNIX command line history with time-stamp of command execution &lt;/a&gt;&lt;/div&gt;
&lt;img src="http://feeds.feedburner.com/~r/bashscripting/~4/ajzClRi_4iQ" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://unstableme.blogspot.com/feeds/2496397111758386033/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=8796493175195266816&amp;postID=2496397111758386033" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/8796493175195266816/posts/default/2496397111758386033?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/8796493175195266816/posts/default/2496397111758386033?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/bashscripting/~3/ajzClRi_4iQ/beginning-of-line-in-unix-screen-session.html" title="Beginning of line in UNIX screen session" /><author><name>Jadu Saikia</name><uri>https://plus.google.com/106333484094067246933</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="//lh4.googleusercontent.com/-wlNtTFQsFC4/AAAAAAAAAAI/AAAAAAAAFR8/rdbW2k6jirU/s512-c/photo.jpg" /></author><thr:total>0</thr:total><feedburner:origLink>http://unstableme.blogspot.com/2012/11/beginning-of-line-in-unix-screen-session.html</feedburner:origLink></entry><entry gd:etag="W/&quot;DEIAQ3o9fSp7ImA9WhNQFEs.&quot;"><id>tag:blogger.com,1999:blog-8796493175195266816.post-4455746666953596743</id><published>2012-11-21T08:24:00.001+05:30</published><updated>2012-11-21T08:25:42.465+05:30</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2012-11-21T08:25:42.465+05:30</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="bash seq" /><category scheme="http://www.blogger.com/atom/ns#" term="bash for" /><category scheme="http://www.blogger.com/atom/ns#" term="Awk" /><category scheme="http://www.blogger.com/atom/ns#" term="linux paste" /><category scheme="http://www.blogger.com/atom/ns#" term="awk newbie" /><title>Transpose using UNIX paste command</title><content type="html">&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;
I have already posted about performing transpose of a matrix using Awk in my earlier posts &lt;a href="http://unstableme.blogspot.in/2008/05/transpose-using-awk-bash.html"&gt;post1&lt;/a&gt; and &lt;a href="http://unstableme.blogspot.in/2008/08/row-to-column-transpose-bash-scripting.html"&gt;post2&lt;/a&gt; . This example shows a not so efficient way of achieving the same. &lt;br/&gt;
&lt;br/&gt;Input file:
&lt;pre&gt;&lt;span style="color: red;"&gt;$&lt;/span&gt; cat file.txt 
 PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND                                                                                                                                                                      
32420 testuser 20   0 1219m 969m  49m R   34 12.1   1043:14 worker
&lt;/pre&gt;
Required output:
&lt;br /&gt;
&lt;pre&gt;PID 32420
USER testuser
PR 20
NI 0
VIRT 1219m
RES 969m
SHR 49m
S R
%CPU 34
%MEM 12.1
TIME+ 1043:14
COMMAND worker
&lt;/pre&gt;
&lt;pre&gt;&lt;span style="color: red;"&gt;$&lt;/span&gt; for i in $(seq 12); do awk -v X=$i '{print $X}' file.txt | paste - - ; done
&lt;/pre&gt;
Related posts:&lt;br/&gt;
- &lt;a href="http://unstableme.blogspot.in/2008/03/accessing-external-variable-in-awk-and.html"&gt;How to access external variable in Awk and SED&lt;/a&gt;&lt;br/&gt;
- &lt;a href="http://unstableme.blogspot.in/2007/02/generating-loop-arguments-seq-command.html"&gt;Generate bash loop arguments using seq command&lt;/a&gt;&lt;br/&gt;
- &lt;a href="http://unstableme.blogspot.in/2009/09/linux-seq-command-format-option-example.html"&gt;UNIX seq command format option example&lt;/a&gt;&lt;br/&gt;
- &lt;a href="http://unstableme.blogspot.in/2009/01/linux-paste-command-good-examples-uses.html"&gt;Some good usage of UNIX paste command&lt;/a&gt;&lt;br/&gt;
- &lt;a href="http://unstableme.blogspot.in/2012/09/compare-two-numeric-fields-of-two-files.html"&gt;Compare two numeric fields of two files in Bash&lt;/a&gt;&lt;br/&gt;
&lt;/div&gt;
&lt;img src="http://feeds.feedburner.com/~r/bashscripting/~4/cVGM0PoHXuo" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://unstableme.blogspot.com/feeds/4455746666953596743/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=8796493175195266816&amp;postID=4455746666953596743" title="1 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/8796493175195266816/posts/default/4455746666953596743?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/8796493175195266816/posts/default/4455746666953596743?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/bashscripting/~3/cVGM0PoHXuo/transpose-using-unix-paste-command.html" title="Transpose using UNIX paste command" /><author><name>Jadu Saikia</name><uri>https://plus.google.com/106333484094067246933</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="//lh4.googleusercontent.com/-wlNtTFQsFC4/AAAAAAAAAAI/AAAAAAAAFR8/rdbW2k6jirU/s512-c/photo.jpg" /></author><thr:total>1</thr:total><feedburner:origLink>http://unstableme.blogspot.com/2012/11/transpose-using-unix-paste-command.html</feedburner:origLink></entry><entry gd:etag="W/&quot;CEYFQXs8eip7ImA9WhNRGUw.&quot;"><id>tag:blogger.com,1999:blog-8796493175195266816.post-8200153849104201230</id><published>2012-11-14T22:18:00.000+05:30</published><updated>2012-11-14T22:25:10.572+05:30</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2012-11-14T22:25:10.572+05:30</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="Awk" /><category scheme="http://www.blogger.com/atom/ns#" term="awk newbie" /><category scheme="http://www.blogger.com/atom/ns#" term="UNIX rename" /><category scheme="http://www.blogger.com/atom/ns#" term="bash shell newbie" /><category scheme="http://www.blogger.com/atom/ns#" term="Bash" /><title>UNIX - append text to filename using Awk</title><content type="html">&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;
I have some files in my current directory whose file-name is of this pattern:
&lt;br /&gt;
&lt;pre&gt;&lt;span style="color: red;"&gt;$&lt;/span&gt; ls -1
log.1024.94.1326776200.1326776300.172.16.12.6.1326844995.0.s-1326528000.r-8192.txt
log.1024.94.1326776400.1326776400.172.16.12.5.1326844995.0.s-1326528000.r-2234.txt
log.1024.95.1326776420.1326776460.172.16.12.5.1326844995.0.s-1326528000.r-8192.txt
&lt;/pre&gt;
Requirement: Append a text "MY-2" as the 6th field (dot delimited) of the filename. E.g.
&lt;br /&gt;
&lt;pre&gt; 
log.1024.94.1326776200.1326776300.172.16.12.6.1326844995.0.s-1326528000.r-8192.txt 
&lt;/pre&gt;
should be renamed to
&lt;br /&gt;
&lt;pre&gt;log.1024.94.1326776200.1326776300.&lt;span style="color: blue;"&gt;MY-2&lt;/span&gt;.172.16.12.6.1326844995.0.s-1326528000.r-8192.txt
&lt;/pre&gt;
A bash script using awk to achieve this:
&lt;br /&gt;
&lt;pre&gt;for file in $(ls)
 do 
  newfilename=$(echo $file | awk 'BEGIN {FS=OFS="."} {$6="MY-2" OFS $6} {print}')
  mv -v $file $newfilename
done
&lt;/pre&gt;
All the files are renamed to:
&lt;br /&gt;
&lt;pre&gt;&lt;span style="color: red;"&gt;$&lt;/span&gt; ls -1
log.1024.94.1326776200.1326776300.&lt;span style="color: blue;"&gt;MY-2&lt;/span&gt;.172.16.12.6.1326844995.0.s-1326528000.r-8192.txt
log.1024.94.1326776400.1326776400.&lt;span style="color: blue;"&gt;MY-2&lt;/span&gt;.172.16.12.5.1326844995.0.s-1326528000.r-2234.txt
log.1024.95.1326776420.1326776460.&lt;span style="color: blue;"&gt;MY-2&lt;/span&gt;.172.16.12.5.1326844995.0.s-1326528000.r-8192.txt
&lt;/pre&gt;
Feel free to post (as comment below) any alternative to this, much appreciated and a big thank you in advance. 
&lt;br /&gt;
Related posts:&lt;br /&gt;
&lt;br /&gt;
- &lt;a href="http://unstableme.blogspot.in/2009/06/padding-zeros-in-file-name-using-bash.html"&gt;Padding zeros in filename using Bash in UNIX&lt;/a&gt;&lt;br /&gt;
- &lt;a href="http://unstableme.blogspot.in/2009/09/rename-file-to-uppercase-except.html"&gt;Rename file to uppercase except extension - Bash&lt;/a&gt;&lt;br /&gt;
- &lt;a href="http://unstableme.blogspot.in/2008/09/bash-rename-command-rename-multiple.html"&gt;Rename multiple files using UNIX rename command&lt;/a&gt; &lt;/div&gt;
&lt;img src="http://feeds.feedburner.com/~r/bashscripting/~4/itUZ9medE2Y" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://unstableme.blogspot.com/feeds/8200153849104201230/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=8796493175195266816&amp;postID=8200153849104201230" title="3 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/8796493175195266816/posts/default/8200153849104201230?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/8796493175195266816/posts/default/8200153849104201230?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/bashscripting/~3/itUZ9medE2Y/unix-append-text-to-filename-using-awk.html" title="UNIX - append text to filename using Awk" /><author><name>Jadu Saikia</name><uri>https://plus.google.com/106333484094067246933</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="//lh4.googleusercontent.com/-wlNtTFQsFC4/AAAAAAAAAAI/AAAAAAAAFR8/rdbW2k6jirU/s512-c/photo.jpg" /></author><thr:total>3</thr:total><feedburner:origLink>http://unstableme.blogspot.com/2012/11/unix-append-text-to-filename-using-awk.html</feedburner:origLink></entry><entry gd:etag="W/&quot;C0ENQXc7fSp7ImA9WhNSFkw.&quot;"><id>tag:blogger.com,1999:blog-8796493175195266816.post-8247474675029814035</id><published>2012-10-30T21:11:00.002+05:30</published><updated>2012-10-30T21:11:30.905+05:30</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2012-10-30T21:11:30.905+05:30</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="unix epoch" /><category scheme="http://www.blogger.com/atom/ns#" term="Awk" /><category scheme="http://www.blogger.com/atom/ns#" term="awk NR" /><category scheme="http://www.blogger.com/atom/ns#" term="awk groupby" /><category scheme="http://www.blogger.com/atom/ns#" term="awk newbie" /><category scheme="http://www.blogger.com/atom/ns#" term="Bash" /><category scheme="http://www.blogger.com/atom/ns#" term="awk array" /><title>Time bucketing using Awk in Unix</title><content type="html">Each line of input file file.txt is having UNIX epoch timestamp and some value. 
&lt;br /&gt;
&lt;pre&gt;&lt;span style="color: red;"&gt;$&lt;/span&gt; cat file.txt 
Epoch,Value
1351605000,120
1351605060,130
1351605120,340
1351605180,200
1351605240,120
1351605300,890
1351605360,124
1351605420,450
1351605480,120
1351605540,120
1351605600,200
1351605660,120
1351605720,340
1351605780,670
1351605840,990
&lt;/pre&gt;
The UNIX epoch timestamps are of 1 minute interval. 
Converting the UNIX epoch timestamps to human readable format using awk 'strftime' function:
&lt;br /&gt;
&lt;pre&gt;&lt;span style="color: red;"&gt;$&lt;/span&gt; awk 'BEGIN {FS=OFS=","} NR != 1 {$1=strftime("%c",$1)} {print}' file.txt 
&lt;/pre&gt;
Output:
&lt;br /&gt;
&lt;pre&gt;Epoch,Value
Tue 30 Oct 2012 01:50:00 PM GMT,120
Tue 30 Oct 2012 01:51:00 PM GMT,130
Tue 30 Oct 2012 01:52:00 PM GMT,340
Tue 30 Oct 2012 01:53:00 PM GMT,200
Tue 30 Oct 2012 01:54:00 PM GMT,120
Tue 30 Oct 2012 01:55:00 PM GMT,890
Tue 30 Oct 2012 01:56:00 PM GMT,124
Tue 30 Oct 2012 01:57:00 PM GMT,450
Tue 30 Oct 2012 01:58:00 PM GMT,120
Tue 30 Oct 2012 01:59:00 PM GMT,120
Tue 30 Oct 2012 02:00:00 PM GMT,200
Tue 30 Oct 2012 02:01:00 PM GMT,120
Tue 30 Oct 2012 02:02:00 PM GMT,340
Tue 30 Oct 2012 02:03:00 PM GMT,670
Tue 30 Oct 2012 02:04:00 PM GMT,990
&lt;/pre&gt;
Required:
Group each 5 minutes entries together to one time bucket and perform a corresponding sum(Value) of the entries. 
E.g. All entries from '01:50:00 PM' (1351605000) till '01:54:00 PM' (1351605240) should be grouped together to '01:50:00 PM' (1351605000) time bucket, '01:55:00 PM' (1351605300) till '01:59:00 PM' (1351605540) to '01:55:00 PM' (1351605300) bucket and so on. 
&lt;br /&gt;
&lt;pre&gt;&lt;span style="color: red;"&gt;$&lt;/span&gt; awk 'BEGIN {FS=OFS=","}  NR != 1 {arr[$1-($1%300)]+=$2} END {for (i in arr) print i,arr[i]}' file.txt 
&lt;/pre&gt;
Output:
&lt;br /&gt;
&lt;pre&gt; 
1351605000,910
1351605300,1704
1351605600,2320
&lt;/pre&gt;
Converting UNIX epoch timestamps to human readable format:
&lt;br /&gt;
&lt;pre&gt;&lt;span style="color: red;"&gt;$&lt;/span&gt; awk 'BEGIN {FS=OFS=","}  NR != 1 {arr[$1-($1%300)]+=$2} END {for (i in arr) print i,arr[i]}' file.txt  | awk '
BEGIN {FS=OFS=","} {$1=strftime("%c",$1)} {print}'
&lt;/pre&gt;
Output:
&lt;br /&gt;
&lt;pre&gt;Tue 30 Oct 2012 01:50:00 PM GMT,910
Tue 30 Oct 2012 01:55:00 PM GMT,1704
Tue 30 Oct 2012 02:00:00 PM GMT,2320
&lt;/pre&gt;
Related posts:&lt;br /&gt;
- &lt;a href="http://unstableme.blogspot.in/2009/10/grouping-files-using-awk-in-bash-shell.html"&gt;Grouping files using Awk in Bash shell&lt;/a&gt;&lt;br /&gt;
- &lt;a href="http://unstableme.blogspot.in/2011/06/awk-convert-epoch-to-date-in-same-file.html"&gt;Awk convert epoch to date format&lt;/a&gt; &lt;br /&gt;
- SUM and GROUP BY using Awk in UNIX &lt;a href="http://unstableme.blogspot.in/2008/09/sum-of-and-group-by-using-awk.html"&gt;post1&lt;/a&gt; &lt;a href="http://unstableme.blogspot.in/2008/09/group-by-clause-functionality-in-awk.html"&gt;post2&lt;/a&gt;&lt;br /&gt;
- &lt;a href="http://unstableme.blogspot.in/2008/05/count-number-of-occurrences-using-awk.html"&gt;COUNT number of occurrences using Awk&lt;/a&gt;&lt;br /&gt;
- &lt;a href="http://unstableme.blogspot.in/2008/05/associative-array-in-awk.html"&gt;Awk Associative array&lt;/a&gt; &lt;br /&gt;
&lt;img src="http://feeds.feedburner.com/~r/bashscripting/~4/z4Ezt1MEMcE" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://unstableme.blogspot.com/feeds/8247474675029814035/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=8796493175195266816&amp;postID=8247474675029814035" title="3 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/8796493175195266816/posts/default/8247474675029814035?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/8796493175195266816/posts/default/8247474675029814035?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/bashscripting/~3/z4Ezt1MEMcE/time-bucketing-using-awk-in-unix.html" title="Time bucketing using Awk in Unix" /><author><name>Jadu Saikia</name><uri>https://plus.google.com/106333484094067246933</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="//lh4.googleusercontent.com/-wlNtTFQsFC4/AAAAAAAAAAI/AAAAAAAAFR8/rdbW2k6jirU/s512-c/photo.jpg" /></author><thr:total>3</thr:total><feedburner:origLink>http://unstableme.blogspot.com/2012/10/time-bucketing-using-awk-in-unix.html</feedburner:origLink></entry><entry gd:etag="W/&quot;DkUNRX87eyp7ImA9WhJbGUQ.&quot;"><id>tag:blogger.com,1999:blog-8796493175195266816.post-3090955568447045663</id><published>2012-09-30T14:06:00.001+05:30</published><updated>2012-09-30T14:08:14.103+05:30</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2012-09-30T14:08:14.103+05:30</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="linux cut" /><category scheme="http://www.blogger.com/atom/ns#" term="awk for loop" /><category scheme="http://www.blogger.com/atom/ns#" term="Awk" /><category scheme="http://www.blogger.com/atom/ns#" term="awk newbie" /><category scheme="http://www.blogger.com/atom/ns#" term="bash shell newbie" /><category scheme="http://www.blogger.com/atom/ns#" term="Bash" /><title>Awk - separate last field with spaces</title><content type="html">&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;
Each line of following file 'file.txt' actually has total 4 fields/columns. As you can see some of the values of the last field (4th field, header : Description) has spaces in them. 
&lt;br /&gt;
&lt;pre&gt;&lt;span style="color: red;"&gt;$&lt;/span&gt; cat file.txt 
ID Name Active Description
2312 DEMO-1 1 Demo for VOD LIVE
1245 DEMO-4 1 LIVE
1002 CUST2 0 VOD Event
19000 DEMO-2 1 VOD
189 DEMO-3 1 Demo for LIVE
&lt;/pre&gt;
Required output:&lt;/br&gt;
We need to separate the 4 fields with a comma (,) delimiter such that all the values after 3rd field is considered as 4th field/column.
i.e. Required output is something like this:
&lt;br /&gt;
&lt;pre&gt;ID, Name, Active, Description
2312, DEMO-1, 1, Demo for VOD LIVE
1245, DEMO-4, 1, LIVE
1002, CUST2, 0, VOD Event
19000, DEMO-2, 1, VOD
189, DEMO-3, 1, Demo for LIVE
&lt;/pre&gt;
One way to achieve this is to add comma (,) to each of the first 3 fields of every line/row.
&lt;br /&gt;
&lt;pre&gt;&lt;span style="color: red;"&gt;$&lt;/span&gt; awk ' { for ( i=1;i&amp;lt;=3;i++ ) {$i=$i","} print }' file.txt
&lt;/pre&gt;
Output:
&lt;br /&gt;
&lt;pre&gt;ID, Name, Active, Description
2312, DEMO-1, 1, Demo for VOD LIVE
1245, DEMO-4, 1, LIVE
1002, CUST2, 0, VOD Event
19000, DEMO-2, 1, VOD
189, DEMO-3, 1, Demo for LIVE
&lt;/pre&gt;
Any other alternative ? Feel free to post in the comment section, much appreciated. Thanks. &lt;/br&gt;
&lt;/br&gt;
And to print the last field of the above input file one solution using UNIX cut command:
&lt;/br&gt;
&lt;pre&gt;&lt;span style="color: red;"&gt;$&lt;/span&gt; cut -d" " -f4- file.txt
&lt;/pre&gt;
Output:
&lt;br /&gt;
&lt;pre&gt;Description
Demo for VOD LIVE
LIVE
VOD Event
VOD
Demo for LIVE
&lt;/pre&gt;
Related posts:&lt;/br&gt;
- &lt;a href="http://unstableme.blogspot.in/2008/01/awk-change-field-separator-add-line.html"&gt;Awk change field separator or delimiter of a file&lt;/a&gt; &lt;/br&gt;
- &lt;a href="http://unstableme.blogspot.in/2010/11/sort-file-based-on-last-field-unix.html"&gt;UNIX sort file based on last field&lt;/a&gt;&lt;/br&gt;
- &lt;a href="http://unstableme.blogspot.in/2008/04/print-last-field-grep.html"&gt;Different ways to print last field of line in UNIX command line&lt;/a&gt;&lt;/br&gt;


&lt;/div&gt;
&lt;img src="http://feeds.feedburner.com/~r/bashscripting/~4/z_Qty3XKGOY" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://unstableme.blogspot.com/feeds/3090955568447045663/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=8796493175195266816&amp;postID=3090955568447045663" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/8796493175195266816/posts/default/3090955568447045663?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/8796493175195266816/posts/default/3090955568447045663?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/bashscripting/~3/z_Qty3XKGOY/awk-separate-last-field-with-spaces.html" title="Awk - separate last field with spaces" /><author><name>Jadu Saikia</name><uri>https://plus.google.com/106333484094067246933</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="//lh4.googleusercontent.com/-wlNtTFQsFC4/AAAAAAAAAAI/AAAAAAAAFR8/rdbW2k6jirU/s512-c/photo.jpg" /></author><thr:total>0</thr:total><feedburner:origLink>http://unstableme.blogspot.com/2012/09/awk-separate-last-field-with-spaces.html</feedburner:origLink></entry><entry gd:etag="W/&quot;C0IFR3kzcSp7ImA9WhJbFkU.&quot;"><id>tag:blogger.com,1999:blog-8796493175195266816.post-1068307941261542759</id><published>2012-09-26T23:15:00.002+05:30</published><updated>2012-09-26T23:15:16.789+05:30</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2012-09-26T23:15:16.789+05:30</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="Bash join" /><category scheme="http://www.blogger.com/atom/ns#" term="Awk" /><category scheme="http://www.blogger.com/atom/ns#" term="linux paste" /><category scheme="http://www.blogger.com/atom/ns#" term="awk newbie" /><category scheme="http://www.blogger.com/atom/ns#" term="bash shell newbie" /><category scheme="http://www.blogger.com/atom/ns#" term="Bash" /><title>Compare two numeric fields of two files - UNIX awk</title><content type="html">&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;
Following example will show how we can use UNIX paste and join command with awk to find difference of two numeric columns of two similar files. &lt;br /&gt;
&lt;br /&gt;
e.g. Input file:
&lt;br /&gt;
&lt;pre&gt;&lt;span style="color: red;"&gt;$&lt;/span&gt; cat file1.txt 
Aug-1,100
Aug-2,220
Aug-4,230
Aug-5,100

&lt;span style="color: red;"&gt;$&lt;/span&gt; cat file2.txt 
Aug-1,100
Aug-2,120
Aug-4,400
Aug-5,250
&lt;/pre&gt;
Required output:&lt;br /&gt;
As you can see both file1.txt and file2.txt has got first field (comma separated) in the same order. 
We will have to find out the corresponding (2nd field of file1.txt - 2nd field of file2.txt) and the output required is something like this:
&lt;br /&gt;
&lt;pre&gt;Aug-1,100,100,0
Aug-2,220,120,100
Aug-4,230,400,-170
Aug-5,100,250,-150
&lt;/pre&gt;
Solution#1:
&lt;br /&gt;
&lt;pre&gt;&lt;span style="color: red;"&gt;$&lt;/span&gt; paste -d "," file1.txt file2.txt
Aug-1,100,Aug-1,100
Aug-2,220,Aug-2,120
Aug-4,230,Aug-4,400
Aug-5,100,Aug-5,250

&lt;span style="color: red;"&gt;$&lt;/span&gt; paste -d "," file1.txt file2.txt | awk 'BEGIN {FS=OFS=","} {print $1,$2,$NF,$2-$NF}'
Aug-1,100,100,0
Aug-2,220,120,100
Aug-4,230,400,-170
Aug-5,100,250,-150
&lt;/pre&gt;
Solution#2:
&lt;br /&gt;
&lt;pre&gt;&lt;span style="color: red;"&gt;$&lt;/span&gt; join -t, -1 1 -2 1 file1.txt file2.txt 
Aug-1,100,100
Aug-2,220,120
Aug-4,230,400
Aug-5,100,250

&lt;span style="color: red;"&gt;$&lt;/span&gt; join -t, -1 1 -2 1 file1.txt file2.txt | awk ' BEGIN {FS=OFS=","} {print $0,$2-$NF}'
Aug-1,100,100,0
Aug-2,220,120,100
Aug-4,230,400,-170
Aug-5,100,250,-150
&lt;/pre&gt;
Some similar posts:&lt;br /&gt;
- &lt;a href="http://unstableme.blogspot.in/2009/01/linux-paste-command-good-examples-uses.html"&gt;A small tutorial on UNIX paste command&lt;/a&gt;&lt;br /&gt;
- &lt;a href="http://unstableme.blogspot.in/2008/03/performing-join-using-awk.html"&gt;Performing join using awk&lt;/a&gt;&lt;br /&gt;
- &lt;a href="http://unstableme.blogspot.in/2007/12/command-in-discussion-join-bash.html"&gt;Join in bash&lt;/a&gt;&lt;br /&gt;
- &lt;a href="http://unstableme.blogspot.in/2008/06/perform-outer-join-bash-join-command.html"&gt;Perform outer join in UNIX Bash&lt;/a&gt;&lt;br /&gt;
- &lt;a href="http://unstableme.blogspot.in/2010/02/bash-find-diff-based-on-field.html"&gt;Find diff based on field - UNIX Bash&lt;/a&gt;&lt;/div&gt;
&lt;img src="http://feeds.feedburner.com/~r/bashscripting/~4/1sVAWs6ymTs" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://unstableme.blogspot.com/feeds/1068307941261542759/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=8796493175195266816&amp;postID=1068307941261542759" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/8796493175195266816/posts/default/1068307941261542759?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/8796493175195266816/posts/default/1068307941261542759?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/bashscripting/~3/1sVAWs6ymTs/compare-two-numeric-fields-of-two-files.html" title="Compare two numeric fields of two files - UNIX awk" /><author><name>Jadu Saikia</name><uri>https://plus.google.com/106333484094067246933</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="//lh4.googleusercontent.com/-wlNtTFQsFC4/AAAAAAAAAAI/AAAAAAAAFR8/rdbW2k6jirU/s512-c/photo.jpg" /></author><thr:total>0</thr:total><feedburner:origLink>http://unstableme.blogspot.com/2012/09/compare-two-numeric-fields-of-two-files.html</feedburner:origLink></entry><entry gd:etag="W/&quot;A0cEQ3g8fip7ImA9WhJQEEw.&quot;"><id>tag:blogger.com,1999:blog-8796493175195266816.post-2948807718491628351</id><published>2012-07-23T10:20:00.000+05:30</published><updated>2012-07-23T10:20:02.676+05:30</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2012-07-23T10:20:02.676+05:30</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="bash shell" /><category scheme="http://www.blogger.com/atom/ns#" term="awk variable assignment" /><category scheme="http://www.blogger.com/atom/ns#" term="Awk" /><category scheme="http://www.blogger.com/atom/ns#" term="awk newbie" /><category scheme="http://www.blogger.com/atom/ns#" term="bash shell newbie" /><title>Print double quotes in unix Awk</title><content type="html">&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;
I have already described in one of the earlier &lt;a href="http://unstableme.blogspot.in/2009/11/printing-single-quote-in-awk-bash.html"&gt;posts&lt;/a&gt; on how to print string within single quote in Awk print statement. Here is how we can print strings without "double quotes" in Awk. &lt;br /&gt;
&lt;pre&gt;&lt;span style="color: red;"&gt;$&lt;/span&gt; cat file.txt 
6289693505455 Plan_DAIL_30D_AA
6289693505475 Plan_DAIL_30D_AA
6289693505462 Plan_DAIL_30D_AB
&lt;/pre&gt;
Output required:
&lt;br /&gt;
&lt;pre&gt;Plan_DAIL_30D_AA "6289693505455"
Plan_DAIL_30D_AA "6289693505475"
Plan_DAIL_30D_AB "6289693505462"
&lt;/pre&gt;
i.e. Print the first field of the file within "double quotes". &lt;br /&gt;
Here are some of the alternatives:
&lt;br /&gt;
&lt;pre&gt;&lt;span style="color: red;"&gt;$&lt;/span&gt; awk '{print $2,$1}' file.txt 
Plan_DAIL_30D_AA 6289693505455
Plan_DAIL_30D_AA 6289693505475
Plan_DAIL_30D_AB 6289693505462

&lt;span style="color: red;"&gt;$&lt;/span&gt; awk '{print $2,"\""$1"\""}' file.txt 
Plan_DAIL_30D_AA "6289693505455"
Plan_DAIL_30D_AA "6289693505475"
Plan_DAIL_30D_AB "6289693505462"

#Assigning the quotes sequence to a variable x 
&lt;span style="color: red;"&gt;$&lt;/span&gt; awk -v x="\"" '{print $2,x$1x}' file.txt 
Plan_DAIL_30D_AA "6289693505455"
Plan_DAIL_30D_AA "6289693505475"
Plan_DAIL_30D_AB "6289693505462"

#Using octal code of double quotes
&lt;span style="color: red;"&gt;$&lt;/span&gt; awk '{print $2,"\042"$1"\042"}' file.txt 
Plan_DAIL_30D_AA "6289693505455"
Plan_DAIL_30D_AA "6289693505475"
Plan_DAIL_30D_AB "6289693505462"

#Using ASCII code of double quotes
&lt;span style="color: red;"&gt;$&lt;/span&gt; awk '{print $2,"\x22"$1"\x22"}'  file.txt 
Plan_DAIL_30D_AA "6289693505455"
Plan_DAIL_30D_AA "6289693505475"
Plan_DAIL_30D_AB "6289693505462"
&lt;/pre&gt;
Related posts:
- &lt;a href="http://unstableme.blogspot.in/2008/03/accessing-external-variable-in-awk-and.html"&gt;Accessing external variable in Awk and Sed&lt;/a&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/bashscripting/~4/nd24EFfxhSw" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://unstableme.blogspot.com/feeds/2948807718491628351/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=8796493175195266816&amp;postID=2948807718491628351" title="1 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/8796493175195266816/posts/default/2948807718491628351?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/8796493175195266816/posts/default/2948807718491628351?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/bashscripting/~3/nd24EFfxhSw/print-double-quotes-in-unix-awk.html" title="Print double quotes in unix Awk" /><author><name>Jadu Saikia</name><uri>https://plus.google.com/106333484094067246933</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="//lh4.googleusercontent.com/-wlNtTFQsFC4/AAAAAAAAAAI/AAAAAAAAFR8/rdbW2k6jirU/s512-c/photo.jpg" /></author><thr:total>1</thr:total><feedburner:origLink>http://unstableme.blogspot.com/2012/07/print-double-quotes-in-unix-awk.html</feedburner:origLink></entry><entry gd:etag="W/&quot;A0UAQng6eSp7ImA9WhJREEs.&quot;"><id>tag:blogger.com,1999:blog-8796493175195266816.post-7437815175378290288</id><published>2012-07-12T10:30:00.000+05:30</published><updated>2012-07-12T10:30:43.611+05:30</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2012-07-12T10:30:43.611+05:30</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="bash sort" /><category scheme="http://www.blogger.com/atom/ns#" term="Linux Utilities" /><category scheme="http://www.blogger.com/atom/ns#" term="Linux Commands" /><category scheme="http://www.blogger.com/atom/ns#" term="bash shell newbie" /><title>Unix sort file ignoring first line</title><content type="html">&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;
&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;
&lt;br /&gt;&lt;/div&gt;
Input file:
&lt;br /&gt;
&lt;pre&gt;&lt;span style="color: red;"&gt;$&lt;/span&gt; cat file.txt 
chrom:index:forward:reverse
chr01:13:1:2
chr03:12:1:4
chr01:3445:1:6
chr02:2311:3:1
chr13:23432:4:7
chr01:212:5:2
chr02:345:12:6
chr01:45:45:0
&lt;/pre&gt;
Output required:
&lt;br /&gt;
&lt;pre&gt; 
chrom:index:forward:reverse
chr01:13:1:2
chr01:45:45:0
chr01:212:5:2
chr01:3445:1:6
chr02:345:12:6
chr02:2311:3:1
chr03:12:1:4
chr13:23432:4:7
&lt;/pre&gt;
i.e. 
Sort file.txt ignoring the header line (1st line).&lt;br /&gt;
Sort first field in ascending order (string)&lt;br /&gt;
Sort second field in ascending order (numeric)&lt;br /&gt;
One of the solution would be:&lt;br /&gt;
&lt;pre&gt;&lt;span style="color: red;"&gt;$&lt;/span&gt; head -1 file.txt ; tail -n +2 file.txt | sort -t : -k1,1 -k2,2n
&lt;/pre&gt;
As you can see two commands are combined together to satisfy the output. &lt;br /&gt;
i) First part is printing the first line of file.txt &lt;br /&gt;
ii) Second part is printing the lines except first line and then sorting the lines based on first field (string) and second field (numeric sort). &lt;br /&gt;
Related posts: &lt;br /&gt;
- &lt;a href="http://unstableme.blogspot.in/2010/11/sort-file-based-on-last-field-unix.html"&gt;Unix sort file based on last field&lt;/a&gt; &lt;br /&gt;
- &lt;a href="http://unstableme.blogspot.in/2009/08/sort-strings-by-length-using-awk-and.html"&gt;Sort strings by length using Awk&lt;/a&gt; &lt;br /&gt;
- &lt;a href="http://unstableme.blogspot.in/2008/08/sort-date-in-ddmmyyyy-format-awk-and.html"&gt;Unix sort date in ddmmyyyy format using Awk&lt;/a&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/bashscripting/~4/4BKtkKn8r7g" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://unstableme.blogspot.com/feeds/7437815175378290288/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=8796493175195266816&amp;postID=7437815175378290288" title="1 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/8796493175195266816/posts/default/7437815175378290288?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/8796493175195266816/posts/default/7437815175378290288?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/bashscripting/~3/4BKtkKn8r7g/unix-sort-file-ignoring-first-line.html" title="Unix sort file ignoring first line" /><author><name>Jadu Saikia</name><uri>https://plus.google.com/106333484094067246933</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="//lh4.googleusercontent.com/-wlNtTFQsFC4/AAAAAAAAAAI/AAAAAAAAFR8/rdbW2k6jirU/s512-c/photo.jpg" /></author><thr:total>1</thr:total><feedburner:origLink>http://unstableme.blogspot.com/2012/07/unix-sort-file-ignoring-first-line.html</feedburner:origLink></entry><entry gd:etag="W/&quot;D04CSXwyfCp7ImA9WhRaFUo.&quot;"><id>tag:blogger.com,1999:blog-8796493175195266816.post-3492789565525484771</id><published>2012-02-18T19:58:00.002+05:30</published><updated>2012-02-18T20:09:28.294+05:30</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2012-02-18T20:09:28.294+05:30</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="awk for loop" /><category scheme="http://www.blogger.com/atom/ns#" term="bash loop" /><category scheme="http://www.blogger.com/atom/ns#" term="nohup" /><category scheme="http://www.blogger.com/atom/ns#" term="Linux Utilities" /><category scheme="http://www.blogger.com/atom/ns#" term="Linux Commands" /><category scheme="http://www.blogger.com/atom/ns#" term="bash shell newbie" /><category scheme="http://www.blogger.com/atom/ns#" term="Bash" /><title>Run UNIX bash loop with nohup</title><content type="html">Suppose we have to run following UNIX bash shell loop construct under 'nohup'&lt;br /&gt;&lt;pre&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;$&lt;/span&gt; for i in $(seq 20); do echo $i;./somescript.sh $i; done&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Running the above loop under nohup directly will not work as 'nohup' expects a single-word command and its arguments. This is how we can run a UNIX bash shell loop construct using 'nohup'.&lt;br /&gt;&lt;pre&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;$&lt;/span&gt; nohup sh -c 'for i in $(seq 20); do echo $i;./somescript.sh $i; done' &amp;amp;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;From man page of 'sh'&lt;br /&gt;&lt;pre&gt;-c               Read commands from the command_string operand instead of from the&lt;br /&gt;standard input.  Special parameter 0 will be set from the command_name operand&lt;br /&gt;and the positional parameters ($1, $2, etc.)  set from the remaining argument operands.&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Related post:&lt;br /&gt;- &lt;a href="http://unstableme.blogspot.in/2008/03/setsid-keep-linux-program-running-while.html"&gt;Setsid : Run UNIX program in a new session&lt;/a&gt;&lt;br /&gt;- &lt;a href="http://unstableme.blogspot.in/2007/02/generating-loop-arguments-seq-command.html"&gt;More about UNIX seq command&lt;/a&gt;&lt;img src="http://feeds.feedburner.com/~r/bashscripting/~4/SVJlEzXybB8" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://unstableme.blogspot.com/feeds/3492789565525484771/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=8796493175195266816&amp;postID=3492789565525484771" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/8796493175195266816/posts/default/3492789565525484771?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/8796493175195266816/posts/default/3492789565525484771?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/bashscripting/~3/SVJlEzXybB8/run-unix-bash-loop-with-nohup.html" title="Run UNIX bash loop with nohup" /><author><name>Jadu Saikia</name><uri>https://plus.google.com/106333484094067246933</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="//lh4.googleusercontent.com/-wlNtTFQsFC4/AAAAAAAAAAI/AAAAAAAAFR8/rdbW2k6jirU/s512-c/photo.jpg" /></author><thr:total>0</thr:total><feedburner:origLink>http://unstableme.blogspot.com/2012/02/run-unix-bash-loop-with-nohup.html</feedburner:origLink></entry><entry gd:etag="W/&quot;D0MEQ34yfyp7ImA9WhdaF0o.&quot;"><id>tag:blogger.com,1999:blog-8796493175195266816.post-3289487118266619089</id><published>2011-10-28T09:32:00.002+05:30</published><updated>2011-10-28T09:46:42.097+05:30</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2011-10-28T09:46:42.097+05:30</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="Unix less" /><category scheme="http://www.blogger.com/atom/ns#" term="Linux Utilities" /><category scheme="http://www.blogger.com/atom/ns#" term="Linux Commands" /><category scheme="http://www.blogger.com/atom/ns#" term="bash shell newbie" /><category scheme="http://www.blogger.com/atom/ns#" term="Bash" /><title>Print line number with unix less command</title><content type="html">&lt;span style="font-weight: bold; font-style: italic;"&gt;Less command introduction:&lt;/span&gt;&lt;br /&gt;As you know Unix less command writes the contents of a file onto the screen a page at a time and this is one of the utilities using which one can view the content of a file without opening it in an editor.&lt;br /&gt;Press the [space-bar] if you want to see another page, and type [q] if you want to quit reading.&lt;br /&gt;&lt;br /&gt;This is how we can print line number with Unix less command.&lt;br /&gt;&lt;br /&gt;1)&lt;br /&gt;From man page of CAT(1):&lt;br /&gt;&lt;pre&gt;       -n, --number&lt;br /&gt;              number all output lines&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;So,&lt;br /&gt;&lt;pre&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;$&lt;/span&gt; cat -n file.txt | less&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;will print the line number in-front of each line.&lt;br /&gt;&lt;br /&gt;2)&lt;br /&gt;From man page of LESS(1):&lt;br /&gt;&lt;pre&gt;       -N or --LINE-NUMBERS&lt;br /&gt;              Causes a line number to be displayed at the beginning of each line in the display.&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;So,&lt;br /&gt;&lt;pre&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;$&lt;/span&gt; less -N file.txt&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;will do the same as 1) above.&lt;br /&gt;&lt;br /&gt;3) One can set the following to print line number with less by default:&lt;br /&gt;&lt;pre&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;$&lt;/span&gt; export LESS='-RS#3NM~g'&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://4.bp.blogspot.com/-B3ZVikgEGpk/TqopZz9IMzI/AAAAAAAADcA/4Tt6V1rLxnU/s1600/Unix%2Bless%2Bcommand%2B-%2Bline%2Bnumber.png"&gt;&lt;img style="display:block; margin:0px auto 10px; text-align:center;cursor:pointer; cursor:hand;width: 305px; height: 364px;" src="http://4.bp.blogspot.com/-B3ZVikgEGpk/TqopZz9IMzI/AAAAAAAADcA/4Tt6V1rLxnU/s400/Unix%2Bless%2Bcommand%2B-%2Bline%2Bnumber.png" alt="" id="BLOGGER_PHOTO_ID_5668388604419257138" border="0" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;span style="font-weight: bold; font-style: italic;"&gt;Related posts:&lt;/span&gt;&lt;br /&gt;- &lt;a href="http://unstableme.blogspot.com/2009/11/bash-numbering-lines-in-file-using-awk.html"&gt;Numbering lines in a file using Bash awk&lt;/a&gt;&lt;br /&gt;- &lt;a href="http://unstableme.blogspot.com/2010/11/vi-open-file-at-required-line-number.html"&gt;Open file at required line number in Unix vi editor&lt;/a&gt;&lt;img src="http://feeds.feedburner.com/~r/bashscripting/~4/oQt49G-_dHQ" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://unstableme.blogspot.com/feeds/3289487118266619089/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=8796493175195266816&amp;postID=3289487118266619089" title="5 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/8796493175195266816/posts/default/3289487118266619089?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/8796493175195266816/posts/default/3289487118266619089?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/bashscripting/~3/oQt49G-_dHQ/print-line-number-with-unix-less.html" title="Print line number with unix less command" /><author><name>Jadu Saikia</name><uri>https://plus.google.com/106333484094067246933</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="//lh4.googleusercontent.com/-wlNtTFQsFC4/AAAAAAAAAAI/AAAAAAAAFR8/rdbW2k6jirU/s512-c/photo.jpg" /></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://4.bp.blogspot.com/-B3ZVikgEGpk/TqopZz9IMzI/AAAAAAAADcA/4Tt6V1rLxnU/s72-c/Unix%2Bless%2Bcommand%2B-%2Bline%2Bnumber.png" height="72" width="72" /><thr:total>5</thr:total><feedburner:origLink>http://unstableme.blogspot.com/2011/10/print-line-number-with-unix-less.html</feedburner:origLink></entry><entry gd:etag="W/&quot;DkcDRnc9fSp7ImA9WhZaEUQ.&quot;"><id>tag:blogger.com,1999:blog-8796493175195266816.post-3273895674222926536</id><published>2011-06-27T22:07:00.004+05:30</published><updated>2011-06-27T22:31:17.965+05:30</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2011-06-27T22:31:17.965+05:30</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="unix epoch" /><category scheme="http://www.blogger.com/atom/ns#" term="Awk strftime" /><category scheme="http://www.blogger.com/atom/ns#" term="python newbie" /><category scheme="http://www.blogger.com/atom/ns#" term="awk newbie" /><category scheme="http://www.blogger.com/atom/ns#" term="bash shell newbie" /><category scheme="http://www.blogger.com/atom/ns#" term="awk substr" /><category scheme="http://www.blogger.com/atom/ns#" term="Bash" /><title>Awk - convert epoch to date in the same file</title><content type="html">&lt;span style="font-style: italic;"&gt;&lt;span style="font-weight: bold;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-weight: bold; font-style: italic;"&gt;Example 1)&lt;/span&gt;&lt;br /&gt;Input file:&lt;br /&gt;&lt;pre&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;$&lt;/span&gt; cat test1.txt&lt;br /&gt;2|Z|1219071600|AF|0&lt;br /&gt;3|N|1219158000|AF|89&lt;br /&gt;4|N|1220799600|AS|12&lt;br /&gt;1|Z|1220886000|AS|67&lt;br /&gt;5|N|1220972400|EU|23&lt;br /&gt;6|R|1221058800|OC|89&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Required output:&lt;br /&gt;&lt;pre&gt;2|Z|Mon 18 Aug 2008 03:00:00 PM UTC|AF|0&lt;br /&gt;3|N|Tue 19 Aug 2008 03:00:00 PM UTC|AF|89&lt;br /&gt;4|N|Sun 07 Sep 2008 03:00:00 PM UTC|AS|12&lt;br /&gt;1|Z|Mon 08 Sep 2008 03:00:00 PM UTC|AS|67&lt;br /&gt;5|N|Tue 09 Sep 2008 03:00:00 PM UTC|EU|23&lt;br /&gt;6|R|Wed 10 Sep 2008 03:00:00 PM UTC|OC|89&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;i.e. convert the UNIX epoch values on 3 rd field of the above file to standard human readable date format using Awk.&lt;br /&gt;&lt;br /&gt;The Awk solution using "strftime" function:&lt;br /&gt;&lt;pre&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;$&lt;/span&gt; awk 'BEGIN {FS=OFS="|"}{$3=strftime("%c",$3)} {print}' test1.txt&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;More about strftime format specifiers can be found &lt;a href="http://pubs.opengroup.org/onlinepubs/007908799/xsh/strftime.html"&gt;here&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;I have created a python program to achieve the above output and the script is &lt;a href="http://pythonstarter.blogspot.com/2009/04/apply-operation-on-field-python.html"&gt;here&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold; font-style: italic;"&gt;Example 2) &lt;/span&gt;&lt;br /&gt;Input file:&lt;br /&gt;&lt;pre&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;$&lt;/span&gt; cat test2.txt&lt;br /&gt;2|Z|time:1219071600|AF|0&lt;br /&gt;3|N|time:1219158000|AF|89&lt;br /&gt;4|N|time:1220799600|AS|12&lt;br /&gt;1|Z|time:1220886000|AS|67&lt;br /&gt;5|N|time:1220972400|EU|23&lt;br /&gt;6|R|time:1221058800|OC|89&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Required output:&lt;br /&gt;&lt;pre&gt;2|Z|time:Mon 18 Aug 2008 03:00:00 PM UTC|AF|0&lt;br /&gt;3|N|time:Tue 19 Aug 2008 03:00:00 PM UTC|AF|89&lt;br /&gt;4|N|time:Sun 07 Sep 2008 03:00:00 PM UTC|AS|12&lt;br /&gt;1|Z|time:Mon 08 Sep 2008 03:00:00 PM UTC|AS|67&lt;br /&gt;5|N|time:Tue 09 Sep 2008 03:00:00 PM UTC|EU|23&lt;br /&gt;6|R|time:Wed 10 Sep 2008 03:00:00 PM UTC|OC|89&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;The awk solution:&lt;br /&gt;&lt;pre&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;$&lt;/span&gt; awk 'BEGIN {FS=OFS="|"}{$3="time:"strftime("%c",substr($3,6,10))} {print}' test2.txt&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Related posts:&lt;br /&gt;- &lt;a href="http://unstableme.blogspot.com/2008/05/awk-substr-function.html"&gt;Awk Substr function explained&lt;/a&gt;&lt;br /&gt;- &lt;a href="http://unstableme.blogspot.com/2009/09/print-first-character-of-field-awk.html"&gt;Print first character of a field in a file using Awk&lt;/a&gt;&lt;br /&gt;- &lt;a href="http://unstableme.blogspot.com/2009/12/replace-first-5-characters-awk-and-sed.html"&gt;Replace first 5 characters of a file using UNIX Sed and Awk&lt;/a&gt;&lt;br /&gt;- &lt;a href="http://unstableme.blogspot.com/2009/11/convert-fixed-length-file-to-csv-awk.html"&gt;Convert fixed length file to a CSV file using Awk&lt;/a&gt;&lt;img src="http://feeds.feedburner.com/~r/bashscripting/~4/yftSxLzCqCA" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://unstableme.blogspot.com/feeds/3273895674222926536/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=8796493175195266816&amp;postID=3273895674222926536" title="9 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/8796493175195266816/posts/default/3273895674222926536?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/8796493175195266816/posts/default/3273895674222926536?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/bashscripting/~3/yftSxLzCqCA/awk-convert-epoch-to-date-in-same-file.html" title="Awk - convert epoch to date in the same file" /><author><name>Jadu Saikia</name><uri>https://plus.google.com/106333484094067246933</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="//lh4.googleusercontent.com/-wlNtTFQsFC4/AAAAAAAAAAI/AAAAAAAAFR8/rdbW2k6jirU/s512-c/photo.jpg" /></author><thr:total>9</thr:total><feedburner:origLink>http://unstableme.blogspot.com/2011/06/awk-convert-epoch-to-date-in-same-file.html</feedburner:origLink></entry><entry gd:etag="W/&quot;Ak4GQH08fyp7ImA9WhZbFUU.&quot;"><id>tag:blogger.com,1999:blog-8796493175195266816.post-8206084283696865652</id><published>2011-06-20T22:09:00.003+05:30</published><updated>2011-06-20T22:25:21.377+05:30</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2011-06-20T22:25:21.377+05:30</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="awk if else" /><category scheme="http://www.blogger.com/atom/ns#" term="bash for" /><category scheme="http://www.blogger.com/atom/ns#" term="Bash if else" /><category scheme="http://www.blogger.com/atom/ns#" term="Awk" /><category scheme="http://www.blogger.com/atom/ns#" term="awk newbie" /><category scheme="http://www.blogger.com/atom/ns#" term="bash shell newbie" /><category scheme="http://www.blogger.com/atom/ns#" term="bash while" /><category scheme="http://www.blogger.com/atom/ns#" term="awk split" /><category scheme="http://www.blogger.com/atom/ns#" term="Bash" /><category scheme="http://www.blogger.com/atom/ns#" term="awk array" /><title>Filter line using Awk split function</title><content type="html">Input file "file.txt" has data in the following format:&lt;br /&gt;&lt;pre&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;$&lt;/span&gt; cat file.txt&lt;br /&gt;232323 90 /T/1382/8558/365p/133 100 234 679&lt;br /&gt;S/1234 90 /N/1389/&lt;span style="color: rgb(0, 0, 153);"&gt;5000&lt;/span&gt;/365s/5000 800 134 679&lt;br /&gt;792363 80 /T/1381/9858/365q/133 100 234 779&lt;br /&gt;136383 90 /K/1382/&lt;span style="color: rgb(0, 0, 153);"&gt;5000&lt;/span&gt;/365p/5000 500 934 979&lt;br /&gt;136383 90 /T/1382/&lt;span style="color: rgb(0, 0, 153);"&gt;5000&lt;/span&gt;/279p/9000 100 134 601&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;Required output:&lt;/span&gt; From the above file print only the lines whose 3rd field has the value "5000" as the 4th field (separated by the delimiter "/") in it. i.e. required output:&lt;br /&gt;&lt;pre&gt;S/1234 90 /N/1389/&lt;span style="color: rgb(0, 0, 153);"&gt;5000&lt;/span&gt;/365s/5000 800 134 679&lt;br /&gt;136383 90 /K/1382/&lt;span style="color: rgb(0, 0, 153);"&gt;5000&lt;/span&gt;/365p/5000 500 934 979&lt;br /&gt;136383 90 /T/1382/&lt;span style="color: rgb(0, 0, 153);"&gt;5000&lt;/span&gt;/279p/9000 100 134 601&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Using awk:&lt;br /&gt;&lt;pre&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;$&lt;/span&gt; awk '{&lt;br /&gt;    split($3,arr,"/")&lt;br /&gt;    if(arr[4] == 5000) {&lt;br /&gt;        print $0&lt;br /&gt;        }&lt;br /&gt;}' file.txt&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;The awk function split(s,a,sep) splits a string "s" into an awk array "a" using the delimiter "sep".&lt;br /&gt;&lt;br /&gt;A simple UNIX bash script to solve this:&lt;br /&gt;&lt;pre&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;$&lt;/span&gt; while read line&lt;br /&gt;    do &lt;br /&gt;        thirdf=$(echo "$line" | awk '{print $3}')&lt;br /&gt;        fourthf=$(echo "$thirdf" | awk -F "/" '{print $4}')&lt;br /&gt;        [ "$fourthf" -eq 5000 ] &amp;amp;&amp;amp; echo $line&lt;br /&gt;done &amp;lt; file.txt&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Related posts:&lt;br /&gt;- &lt;a href="http://unstableme.blogspot.com/2008/06/add-prefix-to-lines-using-awk-bash.html"&gt;Add prefix to lines in a file using awk and bash&lt;/a&gt;&lt;br /&gt;- &lt;a href="http://unstableme.blogspot.com/2011/05/awk-replace-n-th-occurrence-of-pattern.html"&gt;Replace n-th occurrence of pattern in a file using Awk&lt;/a&gt;&lt;img src="http://feeds.feedburner.com/~r/bashscripting/~4/Do5DN0yMMKQ" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://unstableme.blogspot.com/feeds/8206084283696865652/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=8796493175195266816&amp;postID=8206084283696865652" title="2 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/8796493175195266816/posts/default/8206084283696865652?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/8796493175195266816/posts/default/8206084283696865652?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/bashscripting/~3/Do5DN0yMMKQ/filter-line-using-awk-split-function.html" title="Filter line using Awk split function" /><author><name>Jadu Saikia</name><uri>https://plus.google.com/106333484094067246933</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="//lh4.googleusercontent.com/-wlNtTFQsFC4/AAAAAAAAAAI/AAAAAAAAFR8/rdbW2k6jirU/s512-c/photo.jpg" /></author><thr:total>2</thr:total><feedburner:origLink>http://unstableme.blogspot.com/2011/06/filter-line-using-awk-split-function.html</feedburner:origLink></entry><entry gd:etag="W/&quot;A0YCQXs-cSp7ImA9WhZVEkk.&quot;"><id>tag:blogger.com,1999:blog-8796493175195266816.post-4890962513357319436</id><published>2011-05-24T20:15:00.004+05:30</published><updated>2011-05-24T20:29:20.559+05:30</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2011-05-24T20:29:20.559+05:30</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="Bash if else" /><category scheme="http://www.blogger.com/atom/ns#" term="Awk" /><category scheme="http://www.blogger.com/atom/ns#" term="awk NR" /><category scheme="http://www.blogger.com/atom/ns#" term="bash scripts" /><category scheme="http://www.blogger.com/atom/ns#" term="awk newbie" /><category scheme="http://www.blogger.com/atom/ns#" term="bash shell newbie" /><category scheme="http://www.blogger.com/atom/ns#" term="bash while" /><category scheme="http://www.blogger.com/atom/ns#" term="Bash" /><title>Bash insert newline after every 3 lines</title><content type="html">-----------&lt;br /&gt;Input file:&lt;br /&gt;-----------&lt;br /&gt;&lt;pre&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;$&lt;/span&gt; cat file.txt&lt;br /&gt;FR 24&lt;br /&gt;AA 33&lt;br /&gt;EE 34&lt;br /&gt;EE 46&lt;br /&gt;BE 30&lt;br /&gt;AA 31&lt;br /&gt;DE 90&lt;br /&gt;AL 10&lt;br /&gt;AA 50&lt;br /&gt;FR 67&lt;br /&gt;EE 94&lt;br /&gt;AA 80&lt;br /&gt;NK 80&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;---------&lt;br /&gt;Required:&lt;br /&gt;---------&lt;br /&gt;Insert a newline after every 3 lines of the above file. i.e. required output:&lt;br /&gt;&lt;pre&gt;FR 24&lt;br /&gt;AA 33&lt;br /&gt;EE 34&lt;br /&gt;&lt;br /&gt;EE 46&lt;br /&gt;BE 30&lt;br /&gt;AA 31&lt;br /&gt;&lt;br /&gt;DE 90&lt;br /&gt;AL 10&lt;br /&gt;AA 50&lt;br /&gt;&lt;br /&gt;FR 67&lt;br /&gt;EE 94&lt;br /&gt;AA 80&lt;br /&gt;&lt;br /&gt;NK 80&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;----------&lt;br /&gt;Solutions:&lt;br /&gt;----------&lt;br /&gt;a) Using awk:&lt;br /&gt;&lt;pre&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;$&lt;/span&gt; awk '!( NR % 3 ) {$0 = $0"\n"} 1' file.txt&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Which is same as:&lt;br /&gt;&lt;pre&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;$&lt;/span&gt; awk '!( NR % 3 ) {$0 = $0"\n"} {print}' file.txt&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;b) A UNIX bash script to achieve this:&lt;br /&gt;&lt;pre&gt;#!/bin/sh&lt;br /&gt;c=0&lt;br /&gt;while read line&lt;br /&gt;    do&lt;br /&gt;    ((s=c%3))&lt;br /&gt;    if [ "$s" -eq 0 ]; then&lt;br /&gt;        echo -e "\n$line"&lt;br /&gt;    else&lt;br /&gt;        echo "$line"&lt;br /&gt;    fi&lt;br /&gt;    ((c=c+1))&lt;br /&gt;done &amp;lt; file.txt | sed '1d'&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Following if-else block&lt;br /&gt;&lt;pre&gt; &lt;br /&gt;    if [ "$s" -eq 0 ]; then&lt;br /&gt;        echo -e "\n$line"&lt;br /&gt;    else&lt;br /&gt;        echo "$line"&lt;br /&gt;    fi&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;can also be written as:&lt;br /&gt;&lt;pre&gt;[ "$s" -eq 0 ] &amp;amp;&amp;amp; echo -e "\n$line" || echo "$line"&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;---------------------------&lt;br /&gt;Any other alternative ?&lt;br /&gt;---------------------------&lt;br /&gt;Feel free to comment any other alternative to achieve this, much appreciated, thanks.&lt;br /&gt;&lt;br /&gt;Related posts:&lt;br /&gt;- &lt;a href="http://unstableme.blogspot.com/2009/10/bash-while-loop-sum-issue-explained.html"&gt;UNIX Bash while loop sum issue explained&lt;/a&gt;&lt;br /&gt;- &lt;a href="http://unstableme.blogspot.com/2009/01/difference-between-awk-nr-and-fnr.html"&gt;Difference between awk NR and FNR&lt;/a&gt;&lt;img src="http://feeds.feedburner.com/~r/bashscripting/~4/nWl_m-2l0ng" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://unstableme.blogspot.com/feeds/4890962513357319436/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=8796493175195266816&amp;postID=4890962513357319436" title="9 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/8796493175195266816/posts/default/4890962513357319436?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/8796493175195266816/posts/default/4890962513357319436?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/bashscripting/~3/nWl_m-2l0ng/bash-insert-newline-after-every-3-lines.html" title="Bash insert newline after every 3 lines" /><author><name>Jadu Saikia</name><uri>https://plus.google.com/106333484094067246933</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="//lh4.googleusercontent.com/-wlNtTFQsFC4/AAAAAAAAAAI/AAAAAAAAFR8/rdbW2k6jirU/s512-c/photo.jpg" /></author><thr:total>9</thr:total><feedburner:origLink>http://unstableme.blogspot.com/2011/05/bash-insert-newline-after-every-3-lines.html</feedburner:origLink></entry><entry gd:etag="W/&quot;DkMNQHk8eSp7ImA9WhZXGEs.&quot;"><id>tag:blogger.com,1999:blog-8796493175195266816.post-6362085149203284526</id><published>2011-05-08T19:33:00.003+05:30</published><updated>2011-05-08T19:51:31.771+05:30</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2011-05-08T19:51:31.771+05:30</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="awk if else" /><category scheme="http://www.blogger.com/atom/ns#" term="Awk" /><category scheme="http://www.blogger.com/atom/ns#" term="awk replacement" /><category scheme="http://www.blogger.com/atom/ns#" term="awk newbie" /><category scheme="http://www.blogger.com/atom/ns#" term="bash shell newbie" /><category scheme="http://www.blogger.com/atom/ns#" term="Bash" /><title>Awk - replace n-th occurrence of pattern</title><content type="html">&lt;span style="font-weight: bold; font-style: italic;"&gt;Input file:&lt;/span&gt;&lt;br /&gt;&lt;pre&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;$&lt;/span&gt; cat file.txt&lt;br /&gt;FR 24&lt;br /&gt;AA 33&lt;br /&gt;EE 34&lt;br /&gt;EE 46&lt;br /&gt;BE 30&lt;br /&gt;AA 31&lt;br /&gt;DE 90&lt;br /&gt;AL 10&lt;br /&gt;AA 50&lt;br /&gt;FR 67&lt;br /&gt;EE 94&lt;br /&gt;AA 80&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;span style="font-weight: bold; font-style: italic;"&gt;Required:&lt;/span&gt;&lt;br /&gt;Replace the "third" occurrence of first field "AA" with text "XX" in the above file.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold; font-style: italic;"&gt;Awk Solutions: &lt;/span&gt;&lt;br /&gt;&lt;pre&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;$&lt;/span&gt; awk '$1=="AA" {&lt;br /&gt;    count++&lt;br /&gt;    if(count == 3){&lt;br /&gt;        sub("AA","XX",$1)&lt;br /&gt;    }&lt;br /&gt;}&lt;br /&gt;{print}' file.txt&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;span style="font-weight: bold; font-style: italic;"&gt;Output:&lt;/span&gt;&lt;br /&gt;&lt;pre&gt;FR 24&lt;br /&gt;AA 33&lt;br /&gt;EE 34&lt;br /&gt;EE 46&lt;br /&gt;BE 30&lt;br /&gt;AA 31&lt;br /&gt;DE 90&lt;br /&gt;AL 10&lt;br /&gt;&lt;span style="color: rgb(0, 0, 153);"&gt;XX 50&lt;/span&gt;&lt;br /&gt;FR 67&lt;br /&gt;EE 94&lt;br /&gt;AA 80&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;In-case you want to add 100 to the 2nd field of that line where first field "AA" has occurred for the 3rd time, here is a way:&lt;br /&gt;&lt;pre&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;$&lt;/span&gt; awk '$1=="AA" {&lt;br /&gt;    count++&lt;br /&gt;    if(count == 3){&lt;br /&gt;        $2=$2+100&lt;br /&gt;    }&lt;br /&gt;}&lt;br /&gt;{print}' file.txt&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;span style="font-weight: bold; font-style: italic;"&gt;Output:&lt;/span&gt;&lt;br /&gt;&lt;pre&gt;FR 24&lt;br /&gt;AA 33&lt;br /&gt;EE 34&lt;br /&gt;EE 46&lt;br /&gt;BE 30&lt;br /&gt;AA 31&lt;br /&gt;DE 90&lt;br /&gt;AL 10&lt;br /&gt;&lt;span style="color: rgb(0, 0, 153);"&gt;AA 150&lt;/span&gt;&lt;br /&gt;FR 67&lt;br /&gt;EE 94&lt;br /&gt;AA 80&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;span style="font-weight: bold; font-style: italic;"&gt;Related posts:&lt;/span&gt;&lt;br /&gt;- &lt;a href="http://unstableme.blogspot.com/2011/04/awk-print-up-to-nth-occurrence-of.html"&gt;Print up-to nth occurrence of a pattern in a file using awk &lt;/a&gt;&lt;br /&gt;- &lt;a href="http://unstableme.blogspot.com/2009/04/replace-field-other-than-first.html"&gt;Replace field other than the first occurrence using awk&lt;/a&gt;&lt;br /&gt;- &lt;a href="http://unstableme.blogspot.com/2009/12/awk-print-first-occurrence-of-set.html"&gt;Awk print first occurrence of a set in a file&lt;/a&gt;&lt;img src="http://feeds.feedburner.com/~r/bashscripting/~4/4Ru4pG1_5VA" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://unstableme.blogspot.com/feeds/6362085149203284526/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=8796493175195266816&amp;postID=6362085149203284526" title="2 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/8796493175195266816/posts/default/6362085149203284526?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/8796493175195266816/posts/default/6362085149203284526?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/bashscripting/~3/4Ru4pG1_5VA/awk-replace-n-th-occurrence-of-pattern.html" title="Awk - replace n-th occurrence of pattern" /><author><name>Jadu Saikia</name><uri>https://plus.google.com/106333484094067246933</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="//lh4.googleusercontent.com/-wlNtTFQsFC4/AAAAAAAAAAI/AAAAAAAAFR8/rdbW2k6jirU/s512-c/photo.jpg" /></author><thr:total>2</thr:total><feedburner:origLink>http://unstableme.blogspot.com/2011/05/awk-replace-n-th-occurrence-of-pattern.html</feedburner:origLink></entry><entry gd:etag="W/&quot;CkQFSXw_cCp7ImA9WhZXEEw.&quot;"><id>tag:blogger.com,1999:blog-8796493175195266816.post-3567173173807729867</id><published>2011-04-28T22:20:00.004+05:30</published><updated>2011-04-28T22:35:18.248+05:30</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2011-04-28T22:35:18.248+05:30</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="awk for loop" /><category scheme="http://www.blogger.com/atom/ns#" term="Awk" /><category scheme="http://www.blogger.com/atom/ns#" term="awk newbie" /><category scheme="http://www.blogger.com/atom/ns#" term="Bash" /><title>Awk - print up-to nth occurrence of pattern</title><content type="html">Input file:&lt;br /&gt;&lt;pre&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;$&lt;/span&gt; cat file.txt&lt;br /&gt;FR 24&lt;br /&gt;AA 33&lt;br /&gt;AA 36&lt;br /&gt;EE 34&lt;br /&gt;BE 30&lt;br /&gt;AA 31&lt;br /&gt;DE 90&lt;br /&gt;AL 10&lt;br /&gt;AA 50&lt;br /&gt;FR 67&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Required: Print the portion of the above file up-to the 3rd occurrence of the pattern "AA" as the first field. &lt;br /&gt;&lt;pre&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;$&lt;/span&gt; awk '&lt;br /&gt;    $1=="AA" {c++} {print $0}&lt;br /&gt;    c==3 {exit}&lt;br /&gt;' file.txt&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Output:&lt;br /&gt;&lt;pre&gt;FR 24&lt;br /&gt;AA 33&lt;br /&gt;AA 36&lt;br /&gt;EE 34&lt;br /&gt;BE 30&lt;br /&gt;AA 31&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Related posts:&lt;br /&gt;- &lt;a href="http://unstableme.blogspot.com/2009/12/awk-print-first-occurrence-of-set.html"&gt;Print first occurrence of a set using Awk&lt;/a&gt;&lt;img src="http://feeds.feedburner.com/~r/bashscripting/~4/Zxoo-O-oMXE" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://unstableme.blogspot.com/feeds/3567173173807729867/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=8796493175195266816&amp;postID=3567173173807729867" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/8796493175195266816/posts/default/3567173173807729867?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/8796493175195266816/posts/default/3567173173807729867?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/bashscripting/~3/Zxoo-O-oMXE/awk-print-up-to-nth-occurrence-of.html" title="Awk - print up-to nth occurrence of pattern" /><author><name>Jadu Saikia</name><uri>https://plus.google.com/106333484094067246933</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="//lh4.googleusercontent.com/-wlNtTFQsFC4/AAAAAAAAAAI/AAAAAAAAFR8/rdbW2k6jirU/s512-c/photo.jpg" /></author><thr:total>0</thr:total><feedburner:origLink>http://unstableme.blogspot.com/2011/04/awk-print-up-to-nth-occurrence-of.html</feedburner:origLink></entry><entry gd:etag="W/&quot;A08ARH08eSp7ImA9Wx9VF00.&quot;"><id>tag:blogger.com,1999:blog-8796493175195266816.post-7028507780361525512</id><published>2011-02-03T10:25:00.004+05:30</published><updated>2011-02-03T10:40:45.371+05:30</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2011-02-03T10:40:45.371+05:30</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="bash egrep command" /><category scheme="http://www.blogger.com/atom/ns#" term="Bash if else" /><category scheme="http://www.blogger.com/atom/ns#" term="bash scripts" /><category scheme="http://www.blogger.com/atom/ns#" term="bash shell newbie" /><category scheme="http://www.blogger.com/atom/ns#" term="Bash" /><title>Unix Bash script - Check if integer or not</title><content type="html">Please find below two UNIX bash scripts to test if the entered input is integer or not. Request all to suggest if any other alternatives to check this. Thanks in advance.&lt;br /&gt;1) Script 1:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;$&lt;/span&gt; cat check-integer.sh&lt;br /&gt;#!/bin/sh&lt;br /&gt;#Check if input is integer or not&lt;br /&gt;&lt;br /&gt;[ -z $1 ] &amp;amp;&amp;amp; echo "No input, exiting .." &amp;amp;&amp;amp; exit&lt;br /&gt;[[ $1 = *[![:digit:]]* ]]  &amp;amp;&amp;amp; echo "Not Integer" || echo "Integer"&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Executing/testing it:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;$&lt;/span&gt; ./check-integer.sh     &lt;br /&gt;No input, exiting ..&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;$&lt;/span&gt; ./check-integer.sh 12&lt;br /&gt;Integer&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;$&lt;/span&gt; ./check-integer.sh 12.1&lt;br /&gt;Not Integer&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;$&lt;/span&gt; ./check-integer.sh testvar&lt;br /&gt;Not Integer&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;2) Script 2:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;#!/bin/sh&lt;br /&gt;#Check if input is integer or not&lt;br /&gt;&lt;br /&gt;[ -z $1 ] &amp;amp;&amp;amp; echo "No input, exiting .." &amp;amp;&amp;amp; exit&lt;br /&gt;result=$(echo $1 | egrep ^[[:digit:]]+$)&lt;br /&gt;if [ "$result" = "" ] ; then&lt;br /&gt;    echo "Not Integer"&lt;br /&gt;else&lt;br /&gt;    echo "Integer"&lt;br /&gt;fi&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Executing/testing it:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;$&lt;/span&gt; ./check-integer.sh    &lt;br /&gt;No input, exiting ..&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;$&lt;/span&gt; ./check-integer.sh 12&lt;br /&gt;Integer&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;$&lt;/span&gt; ./check-integer.sh 12.1&lt;br /&gt;Not Integer&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;$&lt;/span&gt; ./check-integer.sh testvar&lt;br /&gt;Not Integer&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;$&lt;/span&gt; ./check-integer.sh 12U   &lt;br /&gt;Not Integer&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;$&lt;/span&gt; ./check-integer.sh U12&lt;br /&gt;Not Integer&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;The if else in the second script can also be written using &amp;amp;&amp;amp; and ||, something like:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;[ "$result" = "" ] &amp;amp;&amp;amp; echo "Not Integer" || echo "Integer"&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Related posts:&lt;br /&gt;- &lt;a href="http://unstableme.blogspot.com/2009/01/evaluate-expression-inside-another.html"&gt;Use of $() as an alternative to backtick evaluation in bash &lt;/a&gt;&lt;br /&gt;- &lt;a href="http://unstableme.blogspot.com/2009/02/bash-function-to-compare-multiple.html"&gt;UNIX Bash function to compare equality of multiple  numbers&lt;/a&gt;&lt;img src="http://feeds.feedburner.com/~r/bashscripting/~4/qk62jGb04X8" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://unstableme.blogspot.com/feeds/7028507780361525512/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=8796493175195266816&amp;postID=7028507780361525512" title="1 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/8796493175195266816/posts/default/7028507780361525512?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/8796493175195266816/posts/default/7028507780361525512?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/bashscripting/~3/qk62jGb04X8/unix-bash-script-check-if-integer-or.html" title="Unix Bash script - Check if integer or not" /><author><name>Jadu Saikia</name><uri>https://plus.google.com/106333484094067246933</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="//lh4.googleusercontent.com/-wlNtTFQsFC4/AAAAAAAAAAI/AAAAAAAAFR8/rdbW2k6jirU/s512-c/photo.jpg" /></author><thr:total>1</thr:total><feedburner:origLink>http://unstableme.blogspot.com/2011/02/unix-bash-script-check-if-integer-or.html</feedburner:origLink></entry><entry gd:etag="W/&quot;A0MCR3s5eip7ImA9Wx9XEkU.&quot;"><id>tag:blogger.com,1999:blog-8796493175195266816.post-9200317424577307987</id><published>2011-01-06T10:19:00.001+05:30</published><updated>2011-01-06T10:21:06.522+05:30</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2011-01-06T10:21:06.522+05:30</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="vi editor tips" /><category scheme="http://www.blogger.com/atom/ns#" term="vi handy commands" /><category scheme="http://www.blogger.com/atom/ns#" term="bash shell newbie" /><category scheme="http://www.blogger.com/atom/ns#" term="VIM" /><category scheme="http://www.blogger.com/atom/ns#" term="Bash" /><title>Vim abbreviation not working - solution</title><content type="html">Abbreviations in vi editor are the shortcuts for strings. For example, if you frequently need to write the text "Change review not needed", you can have a shortcut like "crn" for this. So every-time you require to type "Change review not needed", you type crn (then a space) and your abbreviation will be expanded (in insert mode).&lt;br /&gt;&lt;br /&gt;With my vi editor version:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;VIM - Vi IMproved 7.1 (2007 May 12, compiled Jan  8 2009 02:24:14)&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;I created an abbreviation on my .vimrc:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;$&lt;/span&gt; tail -4 ~/.vimrc&lt;br /&gt;:se shiftwidth=4&lt;br /&gt;:se ts=4&lt;br /&gt;:se paste&lt;br /&gt;:ab crn Change review not needed&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;This abbreviation ("crn") was not working as expected and later I came to know its because of the "set paste" option set on my .vimrc. Vi abbreviations are disabled if the 'paste' option is on. I removed the ":se paste" from my .vimrc and the above abbreviation worked.&lt;br /&gt;&lt;br /&gt;Another alternative to set an abbreviation in vim&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;:inoremap crn Change review not needed&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;This also require the 'paste' option to be unset/not set.&lt;br /&gt;&lt;br /&gt;To unset an abbreviation in vi editor, type in ex mode:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;:una crn&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;To see what all abbreviations are already set, type in ex mode:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;:ab&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;For help on vi abbreviation type, type in ex mode:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;:h iab&lt;br /&gt;:h ab&lt;br /&gt;&lt;/pre&gt;&lt;img src="http://feeds.feedburner.com/~r/bashscripting/~4/aHcn_JKK69c" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://unstableme.blogspot.com/feeds/9200317424577307987/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=8796493175195266816&amp;postID=9200317424577307987" title="1 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/8796493175195266816/posts/default/9200317424577307987?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/8796493175195266816/posts/default/9200317424577307987?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/bashscripting/~3/aHcn_JKK69c/vim-abbreviation-not-working-solution.html" title="Vim abbreviation not working - solution" /><author><name>Jadu Saikia</name><uri>https://plus.google.com/106333484094067246933</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="//lh4.googleusercontent.com/-wlNtTFQsFC4/AAAAAAAAAAI/AAAAAAAAFR8/rdbW2k6jirU/s512-c/photo.jpg" /></author><thr:total>1</thr:total><feedburner:origLink>http://unstableme.blogspot.com/2011/01/vim-abbreviation-not-working-solution.html</feedburner:origLink></entry><entry gd:etag="W/&quot;A0IDQ3w4eSp7ImA9Wx9QGU0.&quot;"><id>tag:blogger.com,1999:blog-8796493175195266816.post-811349074299050485</id><published>2011-01-02T00:27:00.003+05:30</published><updated>2011-01-02T00:49:32.231+05:30</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2011-01-02T00:49:32.231+05:30</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="vi editor tips" /><category scheme="http://www.blogger.com/atom/ns#" term="bash shell newbie" /><category scheme="http://www.blogger.com/atom/ns#" term="VIM" /><category scheme="http://www.blogger.com/atom/ns#" term="Bash" /><title>Vi editor - directory modification time changes</title><content type="html">Why last modified time of a directory changes when any file inside the directory is viewed (i.e. opened in vi editor but not saved)  ?&lt;br /&gt;e.g.&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;#Created a directory named 'testdir'&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;$&lt;/span&gt; mkdir testdir&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;$&lt;/span&gt; ls -lrt | tail -1&lt;br /&gt;drwxr-xr-x 2 root root      4096 Dec  5 14:37 testdir&lt;br /&gt;&lt;br /&gt;#Created a file named 'testfile.txt' inside directory 'testdir'&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;$&lt;/span&gt; cd testdir/&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;$&lt;/span&gt; touch testfile.txt&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;$&lt;/span&gt; ls -l&lt;br /&gt;total 0&lt;br /&gt;-rw-r--r-- 1 root root 0 Dec  5 14:38 testfile.txt&lt;br /&gt;&lt;br /&gt;#Modification time of the directory 'testdir' is changed to 'Dec  5 14:38' as expected.&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;$&lt;/span&gt; ls -lrt ../ | tail -1&lt;br /&gt;drwxr-xr-x 2 root root      4096 Dec  5 14:38 testdir&lt;br /&gt;&lt;br /&gt;#Opened the file 'testfile.txt' in Vi editor, but I quit using "!q", so nothing saved.&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;$&lt;/span&gt; vi testfile.txt&lt;br /&gt;&lt;br /&gt;#No Change in modification time of file 'testfile.txt'&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;$&lt;/span&gt; ls -l&lt;br /&gt;total 0&lt;br /&gt;-rw-r--r-- 1 root root 0 Dec  5 14:38 testfile.txt&lt;br /&gt;&lt;br /&gt;#But modification time of the directory 'testdir' is changed.&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;$&lt;/span&gt; ls -lrt ../ | tail -1&lt;br /&gt;drwxr-xr-x 2 root root      4096 Dec  5 14:40 testdir&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Its because when we open a file in vi, a hidden swap file (with .filename.swp extension) is created. The swap file is used for recovery of the vi session in case vi crashes (e.g. when system reboots, suppose you accidentally close your vi session with 'Ctrl q' etc). This swap file is deleted when we save and exit the file in vi editor. The creation of the swap file and later deletion of this swap file on normal vi exit is the cause of the change in modification time of a directory.&lt;img src="http://feeds.feedburner.com/~r/bashscripting/~4/TPzCPVInbsg" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://unstableme.blogspot.com/feeds/811349074299050485/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=8796493175195266816&amp;postID=811349074299050485" title="2 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/8796493175195266816/posts/default/811349074299050485?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/8796493175195266816/posts/default/811349074299050485?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/bashscripting/~3/TPzCPVInbsg/vi-editor-directory-modification-time.html" title="Vi editor - directory modification time changes" /><author><name>Jadu Saikia</name><uri>https://plus.google.com/106333484094067246933</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="//lh4.googleusercontent.com/-wlNtTFQsFC4/AAAAAAAAAAI/AAAAAAAAFR8/rdbW2k6jirU/s512-c/photo.jpg" /></author><thr:total>2</thr:total><feedburner:origLink>http://unstableme.blogspot.com/2011/01/vi-editor-directory-modification-time.html</feedburner:origLink></entry><entry gd:etag="W/&quot;CUANSX0zfyp7ImA9Wx9QFUw.&quot;"><id>tag:blogger.com,1999:blog-8796493175195266816.post-4949570493167971257</id><published>2010-12-28T10:28:00.006+05:30</published><updated>2010-12-28T10:53:18.387+05:30</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2010-12-28T10:53:18.387+05:30</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="bash shell" /><category scheme="http://www.blogger.com/atom/ns#" term="Awk" /><category scheme="http://www.blogger.com/atom/ns#" term="eval in awk" /><category scheme="http://www.blogger.com/atom/ns#" term="awk newbie" /><category scheme="http://www.blogger.com/atom/ns#" term="bash shell newbie" /><category scheme="http://www.blogger.com/atom/ns#" term="bash while" /><category scheme="http://www.blogger.com/atom/ns#" term="Bash" /><title>Bash - Concatenate with underscore in between</title><content type="html">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;X 2 10&lt;br /&gt;X 2 11&lt;br /&gt;Y 5 12&lt;br /&gt;Z 3 11&lt;br /&gt;X 6 78&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Required: For each of the lines of the above file, we are required to create sub directories of the following naming convention inside the directory outdir/.&lt;br /&gt;&lt;br /&gt;i.e. for line&lt;br /&gt;X 2 10&lt;br /&gt;we need to create the sub directory:&lt;br /&gt;outdir/b_config_X_2&lt;br /&gt;&lt;br /&gt;The bash script I wrote:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;$&lt;/span&gt; cat createdir.sh&lt;br /&gt;#!/bin/sh&lt;br /&gt;&lt;br /&gt;while read line&lt;br /&gt;    do&lt;br /&gt;        eval $(echo "$line" | awk '{print "AID="$1";BID="$2";CID="$3}')&lt;br /&gt;        echo "$AID,$BID,$CID"&lt;br /&gt;        dir=b_config_$AID_$BID&lt;br /&gt;        mkdir -p outdir/$dir&lt;br /&gt;done &lt; file.txt&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; ./createdir.sh&lt;br /&gt;X,2,10&lt;br /&gt;X,2,11&lt;br /&gt;Y,5,12&lt;br /&gt;Z,3,11&lt;br /&gt;X,6,78&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Lets see what got created under outdir/&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;$&lt;/span&gt; ls -l outdir/&lt;br /&gt;total 16&lt;br /&gt;drwxr-xr-x 2 root root 4096 Dec 28 04:54 b_config_2&lt;br /&gt;drwxr-xr-x 2 root root 4096 Dec 28 04:54 b_config_3&lt;br /&gt;drwxr-xr-x 2 root root 4096 Dec 28 04:54 b_config_5&lt;br /&gt;drwxr-xr-x 2 root root 4096 Dec 28 04:54 b_config_6&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;To make it work, I did the following change in the above script:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;dir=b_config_$AID\_$BID&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;So the modified script:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;$&lt;/span&gt; cat createdir.sh&lt;br /&gt;#!/bin/sh&lt;br /&gt;&lt;br /&gt;while read line&lt;br /&gt;    do&lt;br /&gt;        eval $(echo "$line" | awk '{print "AID="$1";BID="$2";CID="$3}')&lt;br /&gt;        echo "$AID,$BID,$CID"&lt;br /&gt;        dir=b_config_$AID\_$BID&lt;br /&gt;        mkdir -p outdir/$dir&lt;br /&gt;done &lt; file.txt&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;After execution, it created the correct sub directories:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;$&lt;/span&gt; ls -l outdir/&lt;br /&gt;total 16&lt;br /&gt;drwxr-xr-x 2 root root 4096 Dec 28 04:56 b_config_X_2&lt;br /&gt;drwxr-xr-x 2 root root 4096 Dec 28 04:56 b_config_X_6&lt;br /&gt;drwxr-xr-x 2 root root 4096 Dec 28 04:56 b_config_Y_5&lt;br /&gt;drwxr-xr-x 2 root root 4096 Dec 28 04:56 b_config_Z_3&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;The other alternative to concatenate values of two variables with underscore in between is:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;dir=b_config_${AID}_${BID}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;What is the use of the following Awk eval function ?&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;eval $(echo "$line" | awk '{print "AID="$1";BID="$2";CID="$3}')&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;is a replacement of:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;AID=$(echo $line | awk '{print $1}')&lt;br /&gt;BID=$(echo $line | awk '{print $2}')&lt;br /&gt;CID=$(echo $line | awk '{print $3}')&lt;br /&gt;&lt;br /&gt;or&lt;br /&gt;&lt;br /&gt;AID=$(echo $line | cut -d " " -f1)&lt;br /&gt;BID=$(echo $line | cut -d " " -f2)&lt;br /&gt;CID=$(echo $line | cut -d " " -f3)&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Related post:&lt;br /&gt;- &lt;a href="http://unstableme.blogspot.com/2009/09/bash-script-to-copy-required-files.html"&gt;UNIX Bash script to copy required files&lt;/a&gt;&lt;br /&gt;- &lt;a href="http://unstableme.blogspot.com/2009/12/bash-cat-command-space-issue-explained.html"&gt;UNIX Bash cat command space issue explained&lt;/a&gt;&lt;br /&gt;- &lt;a href="http://unstableme.blogspot.com/2009/10/bash-while-loop-sum-issue-explained.html"&gt;Bash Script while loop issue explained&lt;/a&gt;&lt;img src="http://feeds.feedburner.com/~r/bashscripting/~4/YAjcQYEobxI" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://unstableme.blogspot.com/feeds/4949570493167971257/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=8796493175195266816&amp;postID=4949570493167971257" title="3 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/8796493175195266816/posts/default/4949570493167971257?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/8796493175195266816/posts/default/4949570493167971257?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/bashscripting/~3/YAjcQYEobxI/bash-concatenate-with-underscore-in.html" title="Bash - Concatenate with underscore in between" /><author><name>Jadu Saikia</name><uri>https://plus.google.com/106333484094067246933</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="//lh4.googleusercontent.com/-wlNtTFQsFC4/AAAAAAAAAAI/AAAAAAAAFR8/rdbW2k6jirU/s512-c/photo.jpg" /></author><thr:total>3</thr:total><feedburner:origLink>http://unstableme.blogspot.com/2010/12/bash-concatenate-with-underscore-in.html</feedburner:origLink></entry><entry gd:etag="W/&quot;CkAMQXs4fSp7ImA9Wx9REUo.&quot;"><id>tag:blogger.com,1999:blog-8796493175195266816.post-271571513815543118</id><published>2010-12-12T21:44:00.002+05:30</published><updated>2010-12-12T21:49:40.535+05:30</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2010-12-12T21:49:40.535+05:30</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="awk for loop" /><category scheme="http://www.blogger.com/atom/ns#" term="awk printf" /><category scheme="http://www.blogger.com/atom/ns#" term="Awk" /><category scheme="http://www.blogger.com/atom/ns#" term="awk newbie" /><category scheme="http://www.blogger.com/atom/ns#" term="bash shell newbie" /><category scheme="http://www.blogger.com/atom/ns#" term="Bash" /><title>Awk - Sum of multiple columns of file</title><content type="html">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;500:120:100&lt;br /&gt;100:120:700&lt;br /&gt;200:900:125&lt;br /&gt;120:120:900&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;span style="font-weight: bold; font-style: italic;"&gt;Required:&lt;/span&gt;&lt;br /&gt;Compute the sum of individual columns of the above file. i.e. required output:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;920:1260:1825&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Awk solution - 1:&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;    NR == 1 { n1 = $1; n2 = $2; n3 = $3;  next }&lt;br /&gt;    { n1 += $1; n2 += $2; n3 += $3 }&lt;br /&gt;END { print n1, n2, n3 }' file.txt&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Output:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;920:1260:1825&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Awk solution - 2:&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;{ for (i=1; i&lt;=NF; ++i) sum[i] += $i; j=NF }&lt;br /&gt;END { for (i=1; i &lt;= j; ++i) printf "%s ", sum[i]; printf "\n"; }&lt;br /&gt;' file.txt&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Output:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;920 1260 1825&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;And the solution for finding sum of numbers in each row of a file (i.e. horizontal sum) is &lt;a href="http://unstableme.blogspot.com/2009/12/sum-numbers-in-each-row-awk.html"&gt;here&lt;/a&gt;&lt;img src="http://feeds.feedburner.com/~r/bashscripting/~4/oxljYe4jwCU" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://unstableme.blogspot.com/feeds/271571513815543118/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=8796493175195266816&amp;postID=271571513815543118" title="2 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/8796493175195266816/posts/default/271571513815543118?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/8796493175195266816/posts/default/271571513815543118?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/bashscripting/~3/oxljYe4jwCU/awk-sum-of-multiple-columns-of-file.html" title="Awk - Sum of multiple columns of file" /><author><name>Jadu Saikia</name><uri>https://plus.google.com/106333484094067246933</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="//lh4.googleusercontent.com/-wlNtTFQsFC4/AAAAAAAAAAI/AAAAAAAAFR8/rdbW2k6jirU/s512-c/photo.jpg" /></author><thr:total>2</thr:total><feedburner:origLink>http://unstableme.blogspot.com/2010/12/awk-sum-of-multiple-columns-of-file.html</feedburner:origLink></entry><entry gd:etag="W/&quot;CUYGQXs5eip7ImA9Wx9TFE0.&quot;"><id>tag:blogger.com,1999:blog-8796493175195266816.post-7354652610660898869</id><published>2010-11-22T10:02:00.006+05:30</published><updated>2010-11-22T10:48:40.522+05:30</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2010-11-22T10:48:40.522+05:30</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="Awk" /><category scheme="http://www.blogger.com/atom/ns#" term="awk newbie" /><category scheme="http://www.blogger.com/atom/ns#" term="bash shell newbie" /><category scheme="http://www.blogger.com/atom/ns#" term="Bash" /><title>A newbie tutorial on UNIX Awk utility</title><content type="html">Sharing a few Awk one liners (very basic and random) which will help the Awk newbies to gain a quick confidence on one of the beautiful UNIX tools - Awk. Some of my recommendations  for learning UNIX Awk are mentioned at the bottom of this post. Have a great time with Awk.&lt;br /&gt;&lt;br /&gt;Meaning of some of the Awk Built-in Variables used below:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;NF : Number of fields in current line/record&lt;br /&gt;NR : Ordial number of current line/record&lt;br /&gt;FS : Field Separator (Also -F can be used)&lt;br /&gt;OFS : Output Field Separator (default=blank)&lt;br /&gt;FILENAME : Name of current input file&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;All of following Awk one liners are based on the following input file 'test1.txt'&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;$&lt;/span&gt; cat test1.txt&lt;br /&gt;Continent:Val&lt;br /&gt;AS:12000&lt;br /&gt;AF:9800&lt;br /&gt;AS:12300&lt;br /&gt;NA:3400&lt;br /&gt;OC:12000&lt;br /&gt;AF:500&lt;br /&gt;AS:1000&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;A)&lt;br /&gt;#Print 'line number' NR and 'Number of fields' NF for each line&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;$&lt;/span&gt; awk -F ":" '{print NR,NF}' test1.txt&lt;br /&gt;1 2&lt;br /&gt;2 2&lt;br /&gt;3 2&lt;br /&gt;4 2&lt;br /&gt;5 2&lt;br /&gt;6 2&lt;br /&gt;7 2&lt;br /&gt;8 2&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;B)&lt;br /&gt;#Print first field, colon delimited&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;$&lt;/span&gt; awk -F ":" '{print $1}' test1.txt&lt;br /&gt;Continent&lt;br /&gt;AS&lt;br /&gt;AF&lt;br /&gt;AS&lt;br /&gt;NA&lt;br /&gt;OC&lt;br /&gt;AF&lt;br /&gt;AS&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;C)&lt;br /&gt;#Same as B, but excluding the 'first line' (NR!=1)&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;$&lt;/span&gt; awk -F ":" 'NR!=1 {print $1}' test1.txt&lt;br /&gt;AS&lt;br /&gt;AF&lt;br /&gt;AS&lt;br /&gt;NA&lt;br /&gt;OC&lt;br /&gt;AF&lt;br /&gt;AS&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;D)&lt;br /&gt;#Same as B but only for line number 1 (NR==1)&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;$&lt;/span&gt; awk -F ":" 'NR==1 {print $1}' test1.txt&lt;br /&gt;Continent&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;E)&lt;br /&gt;#Print first and second field&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;$&lt;/span&gt; awk -F ":" 'NR!=1 {print $1,$2}' test1.txt&lt;br /&gt;AS 12000&lt;br /&gt;AF 9800&lt;br /&gt;AS 12300&lt;br /&gt;NA 3400&lt;br /&gt;OC 12000&lt;br /&gt;AF 500&lt;br /&gt;AS 1000&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;F)&lt;br /&gt;#Setting output field separator as pipe&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;$&lt;/span&gt; awk -F ":" 'BEGIN{OFS="|"} NR!=1 {print $1,$2}' test1.txt&lt;br /&gt;AS|12000&lt;br /&gt;AF|9800&lt;br /&gt;AS|12300&lt;br /&gt;NA|3400&lt;br /&gt;OC|12000&lt;br /&gt;AF|500&lt;br /&gt;AS|1000&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;G)&lt;br /&gt;#FS and OFS can be included in BEGIN section&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="|"} NR!=1 {print $1,$2}' test1.txt&lt;br /&gt;AS|12000&lt;br /&gt;AF|9800&lt;br /&gt;AS|12300&lt;br /&gt;NA|3400&lt;br /&gt;OC|12000&lt;br /&gt;AF|500&lt;br /&gt;AS|1000&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;H)&lt;br /&gt;#Anything on BEGIN executes first&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="|"; print "Con|SomeVal"} NR!=1 {print $1,$2}' test1.txt&lt;br /&gt;Con|SomeVal&lt;br /&gt;AS|12000&lt;br /&gt;AF|9800&lt;br /&gt;AS|12300&lt;br /&gt;NA|3400&lt;br /&gt;OC|12000&lt;br /&gt;AF|500&lt;br /&gt;AS|1000&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;I)&lt;br /&gt;#Printing FILENAME, will be printed for all the lines&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;$&lt;/span&gt; awk -F ":" '{print FILENAME}' test1.txt&lt;br /&gt;test1.txt&lt;br /&gt;test1.txt&lt;br /&gt;test1.txt&lt;br /&gt;test1.txt&lt;br /&gt;test1.txt&lt;br /&gt;test1.txt&lt;br /&gt;test1.txt&lt;br /&gt;test1.txt&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;#Same as above but printing only last instance using END clause&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;$&lt;/span&gt; awk -F ":" ' END {print FILENAME}' test1.txt&lt;br /&gt;test1.txt&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;J)&lt;br /&gt;#Revisiting NF, number of fields in each line&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;$&lt;/span&gt; awk -F ":" '{print NF}' test1.txt&lt;br /&gt;2&lt;br /&gt;2&lt;br /&gt;2&lt;br /&gt;2&lt;br /&gt;2&lt;br /&gt;2&lt;br /&gt;2&lt;br /&gt;2&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;K)&lt;br /&gt;#Printing the last field of the file, same as printing $2 as there are only 2 fields&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;$&lt;/span&gt; awk -F ":" '{print $NF}' test1.txt&lt;br /&gt;Val&lt;br /&gt;12000&lt;br /&gt;9800&lt;br /&gt;12300&lt;br /&gt;3400&lt;br /&gt;12000&lt;br /&gt;500&lt;br /&gt;1000&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;L)&lt;br /&gt;#Matching, printing lines begin with "AS"&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;$&lt;/span&gt; awk -F ":" '/^AS/' test1.txt&lt;br /&gt;AS:12000&lt;br /&gt;AS:12300&lt;br /&gt;AS:1000&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;#'Not condition' of the L&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;$&lt;/span&gt; awk -F ":" '!/^AS/' test1.txt&lt;br /&gt;Continent:Val&lt;br /&gt;AF:9800&lt;br /&gt;NA:3400&lt;br /&gt;OC:12000&lt;br /&gt;AF:500&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;#Direct matching, first field as "AS"&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;$&lt;/span&gt; awk -F ":" '$1=="AS"' test1.txt&lt;br /&gt;AS:12000&lt;br /&gt;AS:12300&lt;br /&gt;AS:1000&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;$&lt;/span&gt; awk -F ":" '$1=="AS" {print $2}' test1.txt&lt;br /&gt;12000&lt;br /&gt;12300&lt;br /&gt;1000&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;#$0 prints the full line, same as {print}&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;$&lt;/span&gt; awk -F ":" '$1=="AS" {print $0}' test1.txt&lt;br /&gt;AS:12000&lt;br /&gt;AS:12300&lt;br /&gt;AS:1000&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;$&lt;/span&gt; awk -F ":" '$1=="AS" {print}' test1.txt&lt;br /&gt;AS:12000&lt;br /&gt;AS:12300&lt;br /&gt;AS:1000&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;M)&lt;br /&gt;# 'Or' condition&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;$&lt;/span&gt; awk -F ":" '$1=="AS" || $1=="OC" {print}' test1.txt&lt;br /&gt;AS:12000&lt;br /&gt;AS:12300&lt;br /&gt;OC:12000&lt;br /&gt;AS:1000&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;# 'Or' and 'AND' together&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;$&lt;/span&gt; awk -F ":" '($1=="AS" || $1=="OC") &amp;amp;&amp;amp; $NF &amp;gt; 11000 {print}' test1.txt&lt;br /&gt;AS:12000&lt;br /&gt;AS:12300&lt;br /&gt;OC:12000&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;$&lt;/span&gt; awk -F ":" '($1=="AS" || $1=="OC") &amp;amp;&amp;amp; $NF &amp;gt; 11000 {print $2-10000}' test1.txt&lt;br /&gt;2000&lt;br /&gt;2300&lt;br /&gt;2000&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;$&lt;/span&gt; awk -F ":" '($1=="AS" || $1=="OC") &amp;amp;&amp;amp; $NF &amp;gt; 11000 {print ($2-($2%100))}' test1.txt&lt;br /&gt;12000&lt;br /&gt;12300&lt;br /&gt;12000&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;N)&lt;br /&gt;#Partial Matching&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;$&lt;/span&gt; awk -F ":" '$1 ~ /AS/ {print}' test1.txt&lt;br /&gt;AS:12000&lt;br /&gt;AS:12300&lt;br /&gt;AS:1000&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;$&lt;/span&gt; awk -F ":" '$1 ~ /A/ {print}' test1.txt&lt;br /&gt;AS:12000&lt;br /&gt;AF:9800&lt;br /&gt;AS:12300&lt;br /&gt;NA:3400&lt;br /&gt;AF:500&lt;br /&gt;AS:1000&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;O)&lt;br /&gt;#Reading from  STDOUT&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;$&lt;/span&gt; cat test1.txt | awk -F ":" '!/Continent/ {print $1}' | sort | uniq&lt;br /&gt;AF&lt;br /&gt;AS&lt;br /&gt;NA&lt;br /&gt;OC&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;P)&lt;br /&gt;#Add value 1000 to the 2nd field, where first field is "AF" and then print the output file&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;$&lt;/span&gt; awk -F ":" '$1=="AF" {$2+=1000} {print}' test1.txt&lt;br /&gt;Continent:Val&lt;br /&gt;AS:12000&lt;br /&gt;AF 10800&lt;br /&gt;AS:12300&lt;br /&gt;NA:3400&lt;br /&gt;OC:12000&lt;br /&gt;AF 1500&lt;br /&gt;AS:1000&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;#As no OFS is mentioned above, by default, OFS is blank, specifying it now&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;$&lt;/span&gt; awk -F ":" 'BEGIN {OFS=":"} $1=="AF" {$2+=1000} {print}' test1.txt&lt;br /&gt;Continent:Val&lt;br /&gt;AS:12000&lt;br /&gt;AF:10800&lt;br /&gt;AS:12300&lt;br /&gt;NA:3400&lt;br /&gt;OC:12000&lt;br /&gt;AF:1500&lt;br /&gt;AS:1000&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Q)&lt;br /&gt;#Sum of 2nd fields, exclude first line&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;$&lt;/span&gt; awk -F ":" 'NR!=1 {sum+=$NF} END {print sum}' test1.txt&lt;br /&gt;51000&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;#If END is not mentioned ?&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;$&lt;/span&gt; awk -F ":" 'NR!=1 {sum+=$NF} {print sum}' test1.txt&lt;br /&gt;12000&lt;br /&gt;21800&lt;br /&gt;34100&lt;br /&gt;37500&lt;br /&gt;49500&lt;br /&gt;50000&lt;br /&gt;51000&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;#Average of 2nd field, as first field is excluded, (NR-1) instead of NR for total number of items&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;$&lt;/span&gt; awk -F ":" 'NR!=1 {sum+=$NF} END {print sum/(NR-1)}' test1.txt&lt;br /&gt;7285.71&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;$&lt;/span&gt; awk -F ":" 'NR!=1 &amp;amp;&amp;amp; $1=="AS" {sum+=$NF} END {print sum}' test1.txt&lt;br /&gt;25300&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;R) 'Group by' and 'Count' using associative array in awk&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;NR==1 {print "Continent:Count:Sum(val)"}&lt;br /&gt;NR&gt;1 {a[$1]++;b[$1]=b[$1]+$2} END{for (i in a) print i,a[i],b[i]}' test1.txt&lt;br /&gt;&lt;br /&gt;Continent:Count:Sum(val)&lt;br /&gt;OC:1:12000&lt;br /&gt;NA:1:3400&lt;br /&gt;AF:2:10300&lt;br /&gt;AS:3:25300&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;#Count of each of the continents(1st field)&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;NR==1 {next}&lt;br /&gt;NR&gt;1 {a[$1]++} END{for (i in a) print i,a[i]}' test1.txt&lt;br /&gt;OC 1&lt;br /&gt;NA 1&lt;br /&gt;AF 2&lt;br /&gt;AS 3&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;#An alternative&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;$&lt;/span&gt; awk -F ":" 'NR!=1 {print $1}' test1.txt  | sort | uniq -c&lt;br /&gt;   2 AF&lt;br /&gt;   3 AS&lt;br /&gt;   1 NA&lt;br /&gt;   1 OC&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;#Reading from STDOUT&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;$&lt;/span&gt; cat test1.txt | awk 'BEGIN {OFS=":"} {print NR-1,$0}'&lt;br /&gt;0:Continent:Val&lt;br /&gt;1:AS:12000&lt;br /&gt;2:AF:9800&lt;br /&gt;3:AS:12300&lt;br /&gt;4:NA:3400&lt;br /&gt;5:OC:12000&lt;br /&gt;6:AF:500&lt;br /&gt;7:AS:1000&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;S)&lt;br /&gt;#Accessing external variable in awk:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;$&lt;/span&gt; awk -F ":" '$1=="AS" {print $2}' test1.txt&lt;br /&gt;12000&lt;br /&gt;12300&lt;br /&gt;1000&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;#Suppose value of filterval is "AS"&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;$&lt;/span&gt; filterval="AS"&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;$&lt;/span&gt; echo $filterval&lt;br /&gt;AS&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;#Accessing value of variable filterval inside Awk&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;$&lt;/span&gt; awk -F ":" -v con=$filterval '$1==con {print $2}' test1.txt&lt;br /&gt;12000&lt;br /&gt;12300&lt;br /&gt;1000&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;#Set 2nd value as 0 where first field is "AS"&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;$&lt;/span&gt; awk -F ":" 'BEGIN {OFS=":"} $1=="AS" {$2=0} {print}' test1.txt&lt;br /&gt;Continent:Val&lt;br /&gt;AS:0&lt;br /&gt;AF:9800&lt;br /&gt;AS:0&lt;br /&gt;NA:3400&lt;br /&gt;OC:12000&lt;br /&gt;AF:500&lt;br /&gt;AS:0&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;My recommendation for AWK:&lt;br /&gt;- &lt;a href="http://www.vectorsite.net/tsawk.html"&gt;A complete tutorial on AWK from vectorsite&lt;/a&gt;&lt;br /&gt;- &lt;a href="http://www.pement.org/awk/awk1line.txt"&gt;Awk One line page&lt;/a&gt;&lt;br /&gt;- Peteris Krumins  has done a great job by explaining each and every one lines in details, a must read : &lt;a href="http://www.catonmat.net/blog/awk-one-liners-explained-part-one/"&gt;part1&lt;/a&gt; &lt;a href="http://www.catonmat.net/blog/awk-one-liners-explained-part-two"&gt;part2&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;Related posts from my blog:&lt;br /&gt;- &lt;a href="http://unstableme.blogspot.com/2008/05/awk-substr-function.html"&gt;A post on awk substr function&lt;/a&gt;&lt;br /&gt;- &lt;a href="http://unstableme.blogspot.com/2009/01/difference-between-awk-nr-and-fnr.html"&gt;Difference between awk NR and FNR variables&lt;/a&gt;&lt;img src="http://feeds.feedburner.com/~r/bashscripting/~4/BYcj95CuyY8" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://unstableme.blogspot.com/feeds/7354652610660898869/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=8796493175195266816&amp;postID=7354652610660898869" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/8796493175195266816/posts/default/7354652610660898869?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/8796493175195266816/posts/default/7354652610660898869?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/bashscripting/~3/BYcj95CuyY8/newbie-tutorial-on-unix-awk-utility.html" title="A newbie tutorial on UNIX Awk utility" /><author><name>Jadu Saikia</name><uri>https://plus.google.com/106333484094067246933</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="//lh4.googleusercontent.com/-wlNtTFQsFC4/AAAAAAAAAAI/AAAAAAAAFR8/rdbW2k6jirU/s512-c/photo.jpg" /></author><thr:total>0</thr:total><feedburner:origLink>http://unstableme.blogspot.com/2010/11/newbie-tutorial-on-unix-awk-utility.html</feedburner:origLink></entry><entry gd:etag="W/&quot;DEYMQnk4eCp7ImA9Wx9TEE0.&quot;"><id>tag:blogger.com,1999:blog-8796493175195266816.post-6920189581019698312</id><published>2010-11-17T20:20:00.002+05:30</published><updated>2010-11-17T20:33:03.730+05:30</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2010-11-17T20:33:03.730+05:30</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="linux cut" /><category scheme="http://www.blogger.com/atom/ns#" term="bash shell" /><category scheme="http://www.blogger.com/atom/ns#" term="bash sort" /><category scheme="http://www.blogger.com/atom/ns#" term="Awk" /><category scheme="http://www.blogger.com/atom/ns#" term="Linux Commands" /><category scheme="http://www.blogger.com/atom/ns#" term="awk newbie" /><category scheme="http://www.blogger.com/atom/ns#" term="bash shell newbie" /><title>Sort file based on last field - UNIX</title><content type="html">From SORT(1) man page:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;-n, --numeric-sort&lt;br /&gt;compare according to string numerical value&lt;br /&gt;&lt;br /&gt;-k, --key=POS1[,POS2]&lt;br /&gt;start a key at POS1, end it at POS2 (origin 1)&lt;br /&gt;&lt;br /&gt;-t, --field-separator=SEP&lt;br /&gt;use SEP instead of non-blank to blank transition&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;1) File with same number of fields in each line&lt;br /&gt;&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;IN,90,453&lt;br /&gt;US,12,120&lt;br /&gt;NZ,89,200&lt;br /&gt;WI,20,500&lt;br /&gt;TS,12,124&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; sort -t, -n -k3 file.txt&lt;br /&gt;US,12,120&lt;br /&gt;TS,12,124&lt;br /&gt;NZ,89,200&lt;br /&gt;IN,90,453&lt;br /&gt;WI,20,500&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;2) File with uneven number of fields in each line&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;IN,90,453&lt;br /&gt;US,12,1,120&lt;br /&gt;NZ,89,200&lt;br /&gt;WI,500&lt;br /&gt;TS,12,124&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Solution : The idea is join the last field(column) of each line at the beginning of each of the lines (with a different delimiter, in this case I am using pipe "|")&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="|"}{print $NF,$0}' file.txt&lt;br /&gt;453|IN,90,453&lt;br /&gt;120|US,12,1,120&lt;br /&gt;200|NZ,89,200&lt;br /&gt;500|WI,500&lt;br /&gt;124|TS,12,124&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Then sort on 1st field with pipe ("|") delimited.&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="|"}{print $NF,$0}' file.txt | sort -n -t"|" -k1&lt;br /&gt;120|US,12,1,120&lt;br /&gt;124|TS,12,124&lt;br /&gt;200|NZ,89,200&lt;br /&gt;453|IN,90,453&lt;br /&gt;500|WI,500&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Then discard the first field i.e. print the 2nd field with pipe ("|") delimited.&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="|"}{print $NF,$0}' file.txt | sort -n -t"|" -k1 | awk -F "|" '{print $NF}'&lt;br /&gt;US,12,1,120&lt;br /&gt;TS,12,124&lt;br /&gt;NZ,89,200&lt;br /&gt;IN,90,453&lt;br /&gt;WI,500&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; awk 'BEGIN {FS=","; OFS="|"}{print $NF,$0}' file.txt | sort -n -t"|" -k1 | awk -F "|" '{print $2}'&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; awk 'BEGIN {FS=","; OFS="|"}{print $NF,$0}' file.txt | sort -n -t"|" -k1 | cut -d"|" -f2&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;A question to unstableme readers:&lt;br /&gt;- Is there a better way to directly mention the last field number (as counting field number incase line has huge number of fields is never going to be easy) ?  Thanks in advance, much appreciated.&lt;br /&gt;&lt;br /&gt;Related posts:&lt;br /&gt;- &lt;a href="http://unstableme.blogspot.com/2009/08/sort-strings-by-length-using-awk-and.html"&gt;UNIX sort strings by string length using awk in Bash&lt;/a&gt;&lt;br /&gt;- &lt;a href="http://unstableme.blogspot.com/2009/03/changing-temporary-directory-for-sort.html"&gt;Changing temporary directory for UNIX sort command&lt;/a&gt;&lt;br /&gt;- &lt;a href="http://unstableme.blogspot.com/2008/08/sort-date-in-ddmmyyyy-format-awk-and.html"&gt;Sort date in ddmmyyyy format using awk&lt;/a&gt;&lt;img src="http://feeds.feedburner.com/~r/bashscripting/~4/wHREJ6ccAfs" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://unstableme.blogspot.com/feeds/6920189581019698312/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=8796493175195266816&amp;postID=6920189581019698312" title="3 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/8796493175195266816/posts/default/6920189581019698312?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/8796493175195266816/posts/default/6920189581019698312?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/bashscripting/~3/wHREJ6ccAfs/sort-file-based-on-last-field-unix.html" title="Sort file based on last field - UNIX" /><author><name>Jadu Saikia</name><uri>https://plus.google.com/106333484094067246933</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="//lh4.googleusercontent.com/-wlNtTFQsFC4/AAAAAAAAAAI/AAAAAAAAFR8/rdbW2k6jirU/s512-c/photo.jpg" /></author><thr:total>3</thr:total><feedburner:origLink>http://unstableme.blogspot.com/2010/11/sort-file-based-on-last-field-unix.html</feedburner:origLink></entry></feed>
