<?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: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;DEIEQ3o5eyp7ImA9WhRUEkk.&quot;"><id>tag:blogger.com,1999:blog-8796493175195266816</id><updated>2012-01-22T21:05:02.423+05:30</updated><category term="linux dd" /><category term="awk for loop" /><category term="linux diff" /><category term="du command" /><category term="awk patterns" /><category term="bash egrep command" /><category term="bash for" /><category term="Linux top" /><category term="unix date" /><category term="awk variables" /><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="Awk Functions" /><category term="awk if else" /><category term="Bash Menu" /><category term="awk RS" /><category term="sed newbie" /><category term="Bash if else" /><category term="awk NR" /><category term="linux sudo" /><category term="bash read" /><category term="xargs" /><category term="linux tr" /><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="operating system" /><category term="vimdiff" /><category term="Bash Array" /><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>http://www.blogger.com/profile/00278385409494551967</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="28" height="32" src="http://4.bp.blogspot.com/_yqqceaP4wog/TR9u0hQQFNI/AAAAAAAACug/8mt6wpsc8Lk/S220/Jadu%2BKumar%2BSaikia.png" /></author><generator version="7.00" uri="http://www.blogger.com">Blogger</generator><openSearch:totalResults>403</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;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;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8796493175195266816-3289487118266619089?l=unstableme.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/RCzrgLEduwRDjorP0Mvv688ZxrM/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/RCzrgLEduwRDjorP0Mvv688ZxrM/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/RCzrgLEduwRDjorP0Mvv688ZxrM/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/RCzrgLEduwRDjorP0Mvv688ZxrM/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&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="4 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>http://www.blogger.com/profile/00278385409494551967</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="28" height="32" src="http://4.bp.blogspot.com/_yqqceaP4wog/TR9u0hQQFNI/AAAAAAAACug/8mt6wpsc8Lk/S220/Jadu%2BKumar%2BSaikia.png" /></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>4</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;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8796493175195266816-3273895674222926536?l=unstableme.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/MhUAjTBEyLEQ_cN1B1Wm39r5Srk/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/MhUAjTBEyLEQ_cN1B1Wm39r5Srk/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/MhUAjTBEyLEQ_cN1B1Wm39r5Srk/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/MhUAjTBEyLEQ_cN1B1Wm39r5Srk/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&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>http://www.blogger.com/profile/00278385409494551967</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="28" height="32" src="http://4.bp.blogspot.com/_yqqceaP4wog/TR9u0hQQFNI/AAAAAAAACug/8mt6wpsc8Lk/S220/Jadu%2BKumar%2BSaikia.png" /></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;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8796493175195266816-8206084283696865652?l=unstableme.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/cZdZfDQdC_5roZ7azw5y5tbYgi8/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/cZdZfDQdC_5roZ7azw5y5tbYgi8/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/cZdZfDQdC_5roZ7azw5y5tbYgi8/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/cZdZfDQdC_5roZ7azw5y5tbYgi8/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&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>http://www.blogger.com/profile/00278385409494551967</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="28" height="32" src="http://4.bp.blogspot.com/_yqqceaP4wog/TR9u0hQQFNI/AAAAAAAACug/8mt6wpsc8Lk/S220/Jadu%2BKumar%2BSaikia.png" /></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;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8796493175195266816-4890962513357319436?l=unstableme.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/49FSu9Jna223EpizrdLBJEHaRw8/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/49FSu9Jna223EpizrdLBJEHaRw8/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/49FSu9Jna223EpizrdLBJEHaRw8/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/49FSu9Jna223EpizrdLBJEHaRw8/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&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>http://www.blogger.com/profile/00278385409494551967</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="28" height="32" src="http://4.bp.blogspot.com/_yqqceaP4wog/TR9u0hQQFNI/AAAAAAAACug/8mt6wpsc8Lk/S220/Jadu%2BKumar%2BSaikia.png" /></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;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8796493175195266816-6362085149203284526?l=unstableme.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/39nRlOQXxSGFSOiRXTl_w_Qh1M8/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/39nRlOQXxSGFSOiRXTl_w_Qh1M8/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/39nRlOQXxSGFSOiRXTl_w_Qh1M8/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/39nRlOQXxSGFSOiRXTl_w_Qh1M8/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&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>http://www.blogger.com/profile/00278385409494551967</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="28" height="32" src="http://4.bp.blogspot.com/_yqqceaP4wog/TR9u0hQQFNI/AAAAAAAACug/8mt6wpsc8Lk/S220/Jadu%2BKumar%2BSaikia.png" /></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;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8796493175195266816-3567173173807729867?l=unstableme.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/2dVgN4TnQKamF-bCZgQZl5mAnjw/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/2dVgN4TnQKamF-bCZgQZl5mAnjw/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/2dVgN4TnQKamF-bCZgQZl5mAnjw/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/2dVgN4TnQKamF-bCZgQZl5mAnjw/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&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>http://www.blogger.com/profile/00278385409494551967</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="28" height="32" src="http://4.bp.blogspot.com/_yqqceaP4wog/TR9u0hQQFNI/AAAAAAAACug/8mt6wpsc8Lk/S220/Jadu%2BKumar%2BSaikia.png" /></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;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8796493175195266816-7028507780361525512?l=unstableme.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/jFa7cI6WkAPzuvJ3u4QqbbOZNaY/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/jFa7cI6WkAPzuvJ3u4QqbbOZNaY/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/jFa7cI6WkAPzuvJ3u4QqbbOZNaY/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/jFa7cI6WkAPzuvJ3u4QqbbOZNaY/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&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>http://www.blogger.com/profile/00278385409494551967</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="28" height="32" src="http://4.bp.blogspot.com/_yqqceaP4wog/TR9u0hQQFNI/AAAAAAAACug/8mt6wpsc8Lk/S220/Jadu%2BKumar%2BSaikia.png" /></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;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8796493175195266816-9200317424577307987?l=unstableme.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/W9n06AWvvVBq13Bef4ASL3KZmrA/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/W9n06AWvvVBq13Bef4ASL3KZmrA/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/W9n06AWvvVBq13Bef4ASL3KZmrA/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/W9n06AWvvVBq13Bef4ASL3KZmrA/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&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>http://www.blogger.com/profile/00278385409494551967</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="28" height="32" src="http://4.bp.blogspot.com/_yqqceaP4wog/TR9u0hQQFNI/AAAAAAAACug/8mt6wpsc8Lk/S220/Jadu%2BKumar%2BSaikia.png" /></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;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8796493175195266816-811349074299050485?l=unstableme.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/I0CrI5UkCMbK3fy3ZWHdM37CYB0/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/I0CrI5UkCMbK3fy3ZWHdM37CYB0/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/I0CrI5UkCMbK3fy3ZWHdM37CYB0/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/I0CrI5UkCMbK3fy3ZWHdM37CYB0/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&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>http://www.blogger.com/profile/00278385409494551967</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="28" height="32" src="http://4.bp.blogspot.com/_yqqceaP4wog/TR9u0hQQFNI/AAAAAAAACug/8mt6wpsc8Lk/S220/Jadu%2BKumar%2BSaikia.png" /></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;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8796493175195266816-4949570493167971257?l=unstableme.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/pp6B0nGYL0vBy2vVUUOrI3cJZ88/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/pp6B0nGYL0vBy2vVUUOrI3cJZ88/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/pp6B0nGYL0vBy2vVUUOrI3cJZ88/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/pp6B0nGYL0vBy2vVUUOrI3cJZ88/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&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>http://www.blogger.com/profile/00278385409494551967</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="28" height="32" src="http://4.bp.blogspot.com/_yqqceaP4wog/TR9u0hQQFNI/AAAAAAAACug/8mt6wpsc8Lk/S220/Jadu%2BKumar%2BSaikia.png" /></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;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8796493175195266816-271571513815543118?l=unstableme.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/pSoqcPTbSpxAaget11fx6IFmOzs/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/pSoqcPTbSpxAaget11fx6IFmOzs/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/pSoqcPTbSpxAaget11fx6IFmOzs/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/pSoqcPTbSpxAaget11fx6IFmOzs/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&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>http://www.blogger.com/profile/00278385409494551967</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="28" height="32" src="http://4.bp.blogspot.com/_yqqceaP4wog/TR9u0hQQFNI/AAAAAAAACug/8mt6wpsc8Lk/S220/Jadu%2BKumar%2BSaikia.png" /></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;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8796493175195266816-7354652610660898869?l=unstableme.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/n_8KkScaoIkhuZEhlFTymbtb48Y/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/n_8KkScaoIkhuZEhlFTymbtb48Y/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/n_8KkScaoIkhuZEhlFTymbtb48Y/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/n_8KkScaoIkhuZEhlFTymbtb48Y/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&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>http://www.blogger.com/profile/00278385409494551967</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="28" height="32" src="http://4.bp.blogspot.com/_yqqceaP4wog/TR9u0hQQFNI/AAAAAAAACug/8mt6wpsc8Lk/S220/Jadu%2BKumar%2BSaikia.png" /></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;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8796493175195266816-6920189581019698312?l=unstableme.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/sRS2S57riIhD_t3KjKqDz2neVWg/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/sRS2S57riIhD_t3KjKqDz2neVWg/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/sRS2S57riIhD_t3KjKqDz2neVWg/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/sRS2S57riIhD_t3KjKqDz2neVWg/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&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>http://www.blogger.com/profile/00278385409494551967</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="28" height="32" src="http://4.bp.blogspot.com/_yqqceaP4wog/TR9u0hQQFNI/AAAAAAAACug/8mt6wpsc8Lk/S220/Jadu%2BKumar%2BSaikia.png" /></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><entry gd:etag="W/&quot;DEQCRX47eyp7ImA9Wx5aGE8.&quot;"><id>tag:blogger.com,1999:blog-8796493175195266816.post-5750918430929426591</id><published>2010-11-15T18:19:00.003+05:30</published><updated>2010-11-15T18:36:04.003+05:30</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2010-11-15T18:36:04.003+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" /><title>Vi open file at required line number</title><content type="html">Sometime we are required to open a file in vim and then need to go to a particular line number in order to check something or to edit something. This is how we can open a file in vim and directly put the cursor on the required line of our interest.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold; font-style: italic;"&gt;Way1:&lt;/span&gt;&lt;br /&gt;Using vim -c option:&lt;br /&gt;&lt;br /&gt;From VIM(1) man page:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;-c {command}&lt;br /&gt;{command} will be executed after the first file has been read.  {command} is interpreted as an&lt;br /&gt; Ex command.  If the {command} contains spaces it must be enclosed in double quotes (this&lt;br /&gt;depends on the shell  that is used).  Example: Vim "+set si" main.c&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Following command will open file 'run_parallel_qs.py' in vim with the cursor on line number 23.&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;$&lt;/span&gt; vim -c 23 run_parallel_qs.py&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold; font-style: italic;"&gt;Way2:&lt;/span&gt;&lt;br /&gt;One can use the + to specify the cursor starting point.&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;$&lt;/span&gt; vim run_parallel_qs.py +23&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; vim +23 run_parallel_qs.py&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Without line number, i.e. the following command will open the file in vim and will put the cursor on last line&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;$&lt;/span&gt; vim  run_parallel_queries.py +&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;And as we know just opening a file in vi/vim, the cursor will be on line number one.&lt;br /&gt;&lt;br /&gt;Related UNIX vim editor tips:&lt;br /&gt;- &lt;a href="http://unstableme.blogspot.com/2009/12/vim-tip-open-file-under-cursor.html"&gt;Open file under cursor in Vi Editor&lt;/a&gt;&lt;br /&gt;- &lt;a href="http://unstableme.blogspot.com/2009/04/using-tabs-in-vim-short-tutorial.html"&gt;A short tutorial on using tab window in Vi editor&lt;/a&gt;&lt;br /&gt;- &lt;a href="http://unstableme.blogspot.com/2009/04/pull-word-under-cursor-vim-tip.html"&gt;Pull word under cursor in Vim&lt;/a&gt;&lt;br /&gt;- &lt;a href="http://unstableme.blogspot.com/2008/10/start-new-shell-from-vi-editor-vim-tips.html"&gt;Start a new shell from Vi editor&lt;/a&gt;&lt;br /&gt;- &lt;a href="http://unstableme.blogspot.com/2008/08/edit-remote-file-using-vi-and-scp-linux.html"&gt;How to edit remote file using ssh in Vi editor&lt;/a&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8796493175195266816-5750918430929426591?l=unstableme.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/VcxHQun3e5lcGJEmUHjSvfRx_wo/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/VcxHQun3e5lcGJEmUHjSvfRx_wo/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/VcxHQun3e5lcGJEmUHjSvfRx_wo/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/VcxHQun3e5lcGJEmUHjSvfRx_wo/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/bashscripting/~4/6gpK6cbTjYw" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://unstableme.blogspot.com/feeds/5750918430929426591/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=8796493175195266816&amp;postID=5750918430929426591" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/8796493175195266816/posts/default/5750918430929426591?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/8796493175195266816/posts/default/5750918430929426591?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/bashscripting/~3/6gpK6cbTjYw/vi-open-file-at-required-line-number.html" title="Vi open file at required line number" /><author><name>Jadu Saikia</name><uri>http://www.blogger.com/profile/00278385409494551967</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="28" height="32" src="http://4.bp.blogspot.com/_yqqceaP4wog/TR9u0hQQFNI/AAAAAAAACug/8mt6wpsc8Lk/S220/Jadu%2BKumar%2BSaikia.png" /></author><thr:total>0</thr:total><feedburner:origLink>http://unstableme.blogspot.com/2010/11/vi-open-file-at-required-line-number.html</feedburner:origLink></entry><entry gd:etag="W/&quot;DUYGRnc8eip7ImA9Wx5UF04.&quot;"><id>tag:blogger.com,1999:blog-8796493175195266816.post-5339055245058104987</id><published>2010-10-22T14:04:00.005+05:30</published><updated>2010-10-22T14:15:27.972+05:30</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2010-10-22T14:15:27.972+05:30</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="Awk" /><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="Bash" /><title>Check equality of multiple numbers - awk</title><content type="html">My input file 'file.txt' contains 4 values of a certain metric for each of the following 'Continents'.&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;Continent Val1 Val2 Val3 Val4&lt;br /&gt;AS 440518 440518 440516 440516&lt;br /&gt;AF 253317 253317 253315 253317&lt;br /&gt;EU 245397 245397 245397 245397&lt;br /&gt;OC 226410 226410 226410 226410&lt;br /&gt;NA 221961 221961 221962 221961&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Required : I was required to find out only those 'Continents' for which 'all' values are 'same'.&lt;br /&gt;&lt;br /&gt;Solutions:&lt;br /&gt;&lt;br /&gt;1) Using awk:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;$&lt;/span&gt; awk '&lt;br /&gt;    /^Continent/ {print $1; next}&lt;br /&gt;    $2==$3 &amp;amp;&amp;amp; $3==$4 &amp;amp;&amp;amp; $4==$5 {print $1}&lt;br /&gt;' file.txt&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Output:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;Continent&lt;br /&gt;EU&lt;br /&gt;OC&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;2) Wrote this python program using python 'sets' (Unordered collections of unique elements) to achieve the same. Something like:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;from sets import Set&lt;br /&gt;for line in open("file.txt"):&lt;br /&gt;    if line.startswith('Continent'):&lt;br /&gt;        print line.split()[0]&lt;br /&gt;    firstfield = line.split()[0]&lt;br /&gt;    remaining = line.split()[1:]&lt;br /&gt;    vals = Set(remaining)&lt;br /&gt;    if len(vals) == 1:&lt;br /&gt;        print firstfield&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; python printequal.py&lt;br /&gt;Continent&lt;br /&gt;EU&lt;br /&gt;OC&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;3) Any other solution using Bash, Awk or any other scripting languages ? Readers, please put your solutions here in the comment section. Much appreciated.&lt;br /&gt;&lt;br /&gt;Related:&lt;br /&gt;- &lt;a href="http://unstableme.blogspot.com/2009/02/bash-function-to-compare-multiple.html"&gt;Bash function to compare multiple numbers equality&lt;/a&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8796493175195266816-5339055245058104987?l=unstableme.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/K4GE2Phhl-jEoeLlAWxFlYdQ95c/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/K4GE2Phhl-jEoeLlAWxFlYdQ95c/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/K4GE2Phhl-jEoeLlAWxFlYdQ95c/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/K4GE2Phhl-jEoeLlAWxFlYdQ95c/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/bashscripting/~4/NBWtNQlr47Y" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://unstableme.blogspot.com/feeds/5339055245058104987/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=8796493175195266816&amp;postID=5339055245058104987" title="3 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/8796493175195266816/posts/default/5339055245058104987?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/8796493175195266816/posts/default/5339055245058104987?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/bashscripting/~3/NBWtNQlr47Y/check-equality-of-multiple-numbers-awk.html" title="Check equality of multiple numbers - awk" /><author><name>Jadu Saikia</name><uri>http://www.blogger.com/profile/00278385409494551967</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="28" height="32" src="http://4.bp.blogspot.com/_yqqceaP4wog/TR9u0hQQFNI/AAAAAAAACug/8mt6wpsc8Lk/S220/Jadu%2BKumar%2BSaikia.png" /></author><thr:total>3</thr:total><feedburner:origLink>http://unstableme.blogspot.com/2010/10/check-equality-of-multiple-numbers-awk.html</feedburner:origLink></entry><entry gd:etag="W/&quot;CkANQHY5eSp7ImA9Wx5UFUo.&quot;"><id>tag:blogger.com,1999:blog-8796493175195266816.post-782503375217444175</id><published>2010-10-20T14:28:00.005+05:30</published><updated>2010-10-20T16:03:11.821+05:30</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2010-10-20T16:03:11.821+05:30</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="unix epoch" /><category scheme="http://www.blogger.com/atom/ns#" term="Linux Utilities" /><category scheme="http://www.blogger.com/atom/ns#" term="unix date" /><category scheme="http://www.blogger.com/atom/ns#" term="bash shell newbie" /><title>Print future date using unix date command</title><content type="html">I have already discussed in one of &lt;a href="http://unstableme.blogspot.com/2008/01/more-of-date-command.html"&gt;my earlier post&lt;/a&gt; about how we can print future and past dates using UNIX date command, revisiting it again to show a very interesting date that we are going to witness today.&lt;br /&gt;&lt;br /&gt;From DATE(1) man pages, some of the options used in this example are:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;%H     hour (00..23)&lt;br /&gt;%M     minute (00..59)&lt;br /&gt;%d     day of month (e.g, 01)&lt;br /&gt;%m     month (01..12)&lt;br /&gt;%Y     year&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;This  is an interesting time in 2010, 8:10 PM of 20th Oct 2010, it going to be 20:10 20/10 2010. Lets try to print this using UNIX date command.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://4.bp.blogspot.com/_yqqceaP4wog/TL68D-dsDZI/AAAAAAAACok/2_0WTARENTk/s1600/Bash+-+Print+future+dates+using+date+command.png"&gt;&lt;img style="display: block; margin: 0px auto 10px; text-align: center; cursor: pointer; width: 400px; height: 72px;" src="http://4.bp.blogspot.com/_yqqceaP4wog/TL68D-dsDZI/AAAAAAAACok/2_0WTARENTk/s400/Bash+-+Print+future+dates+using+date+command.png" alt="" id="BLOGGER_PHOTO_ID_5530064168950697362" border="0" /&gt;&lt;/a&gt;&lt;br /&gt;Hope you found it interesting !&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8796493175195266816-782503375217444175?l=unstableme.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/DIy33scwFQXJKTLC5fGaTbiARPg/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/DIy33scwFQXJKTLC5fGaTbiARPg/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/DIy33scwFQXJKTLC5fGaTbiARPg/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/DIy33scwFQXJKTLC5fGaTbiARPg/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/bashscripting/~4/mW6QA85kboU" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://unstableme.blogspot.com/feeds/782503375217444175/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=8796493175195266816&amp;postID=782503375217444175" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/8796493175195266816/posts/default/782503375217444175?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/8796493175195266816/posts/default/782503375217444175?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/bashscripting/~3/mW6QA85kboU/print-future-date-using-unix-date.html" title="Print future date using unix date command" /><author><name>Jadu Saikia</name><uri>http://www.blogger.com/profile/00278385409494551967</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="28" height="32" src="http://4.bp.blogspot.com/_yqqceaP4wog/TR9u0hQQFNI/AAAAAAAACug/8mt6wpsc8Lk/S220/Jadu%2BKumar%2BSaikia.png" /></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://4.bp.blogspot.com/_yqqceaP4wog/TL68D-dsDZI/AAAAAAAACok/2_0WTARENTk/s72-c/Bash+-+Print+future+dates+using+date+command.png" height="72" width="72" /><thr:total>0</thr:total><feedburner:origLink>http://unstableme.blogspot.com/2010/10/print-future-date-using-unix-date.html</feedburner:origLink></entry><entry gd:etag="W/&quot;CUAFRHg6cCp7ImA9Wx5VGU0.&quot;"><id>tag:blogger.com,1999:blog-8796493175195266816.post-3377780525950629771</id><published>2010-10-12T22:23:00.004+05:30</published><updated>2010-10-12T22:45:15.618+05:30</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2010-10-12T22:45:15.618+05:30</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="bash scripts questions" /><category scheme="http://www.blogger.com/atom/ns#" term="Awk" /><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" /><title>Awk - Print particular instances of a file</title><content type="html">I am requesting everyone to provide a better alternative (in any scripting language) to this problem. Thanks in advance.&lt;br /&gt;&lt;br /&gt;My input file has the following format:&lt;br /&gt;- A instance is a combination of 'h' 'v' and one or more 'i' lines.&lt;br /&gt;- All lines starting with 'h' are header lines and the 3rd field in that line is the 'header number'.&lt;br /&gt;- All lines starting with 'v' are version lines and the second field in that line is the 'version number'.&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;&lt;span style="color: rgb(51, 102, 255);"&gt;h,1,&lt;span style="color: rgb(255, 0, 0);"&gt;100&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 102, 255);"&gt;v,&lt;span style="color: rgb(255, 0, 0);"&gt;1&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 102, 255);"&gt;i,rt,200&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 102, 255);"&gt;i,rt,210&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 102, 255);"&gt;i,rt,810&lt;/span&gt;&lt;br /&gt;h,1,101&lt;br /&gt;v,5&lt;br /&gt;i,rt,500&lt;br /&gt;i,rt,700&lt;br /&gt;h,1,100&lt;br /&gt;v,2&lt;br /&gt;i,rt,100&lt;br /&gt;i,rt,910&lt;br /&gt;h,1,500&lt;br /&gt;v,1&lt;br /&gt;i,rt,190&lt;br /&gt;&lt;span style="color: rgb(51, 51, 255);"&gt;h,1,&lt;span style="color: rgb(255, 0, 0);"&gt;100&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 51, 255);"&gt;v,&lt;span style="color: rgb(255, 0, 0);"&gt;1&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 51, 255);"&gt;i,rt,900&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 51, 255);"&gt;i,rt,210&lt;/span&gt;&lt;br /&gt;h,1,300&lt;br /&gt;v,1&lt;br /&gt;i,rt,800&lt;br /&gt;i,rt,210&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Required:&lt;br /&gt;- Print all the 'i' lines associated with header number '100' and version number '1'&lt;br /&gt;i.e. required output:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;i,rt,200&lt;br /&gt;i,rt,210&lt;br /&gt;i,rt,810&lt;br /&gt;i,rt,900&lt;br /&gt;i,rt,210&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;The quick solution I can think about is to associate 'header number' and 'version number' with all 'i' lines&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;$&lt;/span&gt; awk -F "," '$1=="h" {h_value=$NF}&lt;br /&gt;    $1=="v" {v_value=$NF}&lt;br /&gt;    $1=="i" {print h_value,v_value,$0}&lt;br /&gt;' file.txt                                    &lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Output:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;100 1 i,rt,200&lt;br /&gt;100 1 i,rt,210&lt;br /&gt;100 1 i,rt,810&lt;br /&gt;101 5 i,rt,500&lt;br /&gt;101 5 i,rt,700&lt;br /&gt;100 2 i,rt,100&lt;br /&gt;100 2 i,rt,910&lt;br /&gt;500 1 i,rt,190&lt;br /&gt;100 1 i,rt,900&lt;br /&gt;100 1 i,rt,210&lt;br /&gt;300 1 i,rt,800&lt;br /&gt;300 1 i,rt,210&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;And then print only the lines with header number=100 and version number=1.&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;$&lt;/span&gt; awk -F "," '$1=="h" {h_value=$NF}&lt;br /&gt;    $1=="v" {v_value=$NF}&lt;br /&gt;    $1=="i" {print h_value,v_value,$0}&lt;br /&gt;' file.txt | awk '$1==100 &amp;amp;&amp;amp; $2==1 {print $NF}'&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Output:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;i,rt,200&lt;br /&gt;i,rt,210&lt;br /&gt;i,rt,810&lt;br /&gt;i,rt,900&lt;br /&gt;i,rt,210&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;I am sure there can be a better solution to this problem. Readers, please put your solutions here in the comment section. Much appreciated.&lt;br /&gt;&lt;br /&gt;Related posts:&lt;br /&gt;- &lt;a href="http://pythonstarter.blogspot.com/2010/01/python-count-instances-without-specific.html"&gt;Count instances without specific line in UNIX using Python&lt;/a&gt;&lt;br /&gt;- &lt;a href="http://pythonstarter.blogspot.com/2009/09/print-last-instance-of-file-python.html"&gt;Print last instance of a file in UNIX&lt;/a&gt;&lt;br /&gt;- &lt;a href="http://pythonstarter.blogspot.com/2009/05/print-first-few-instances-of-file.html"&gt;Print first few instances of a file - python&lt;/a&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8796493175195266816-3377780525950629771?l=unstableme.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/j5zH0B981EY8chb3RKqtM_jD49U/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/j5zH0B981EY8chb3RKqtM_jD49U/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/j5zH0B981EY8chb3RKqtM_jD49U/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/j5zH0B981EY8chb3RKqtM_jD49U/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/bashscripting/~4/PYV3EuPmi5Y" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://unstableme.blogspot.com/feeds/3377780525950629771/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=8796493175195266816&amp;postID=3377780525950629771" title="8 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/8796493175195266816/posts/default/3377780525950629771?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/8796493175195266816/posts/default/3377780525950629771?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/bashscripting/~3/PYV3EuPmi5Y/awk-print-particular-instances-of-file.html" title="Awk - Print particular instances of a file" /><author><name>Jadu Saikia</name><uri>http://www.blogger.com/profile/00278385409494551967</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="28" height="32" src="http://4.bp.blogspot.com/_yqqceaP4wog/TR9u0hQQFNI/AAAAAAAACug/8mt6wpsc8Lk/S220/Jadu%2BKumar%2BSaikia.png" /></author><thr:total>8</thr:total><feedburner:origLink>http://unstableme.blogspot.com/2010/10/awk-print-particular-instances-of-file.html</feedburner:origLink></entry><entry gd:etag="W/&quot;CkEBQX04fCp7ImA9Wx5QFkQ.&quot;"><id>tag:blogger.com,1999:blog-8796493175195266816.post-7048744007786845832</id><published>2010-09-05T18:02:00.002+05:30</published><updated>2010-09-05T18:14:10.334+05:30</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2010-09-05T18:14:10.334+05:30</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="UNIX look command" /><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="linux grep" /><title>UNIX look command to print lines</title><content type="html">Let me introduce a simple UNIX/Linux command which is very useful but not that popular.&lt;br /&gt;Its called 'look'. And as the name suggests, this command displays lines beginning with a given string.&lt;br /&gt;&lt;br /&gt;Usage:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;look string &amp;lt;file&amp;gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;The look utility displays any lines in 'file' which contain 'string' as a prefix. This can be a simple alternative of using&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;$&lt;/span&gt; grep "^string" &amp;lt;file&amp;gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;If file is not specified, the file /usr/share/dict/words is used, only alphanumeric characters are compared and the case of alphabetic characters is ignored.&lt;br /&gt;And I feel this lookup of dictionary words from 'usr/share/dict/words' can be a very common use of this utility.&lt;br /&gt;e.g.&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;$&lt;/span&gt; look advis&lt;br /&gt;advisability&lt;br /&gt;advisability's&lt;br /&gt;advisable&lt;br /&gt;advise&lt;br /&gt;advised&lt;br /&gt;advisedly&lt;br /&gt;advisement&lt;br /&gt;advisement's&lt;br /&gt;adviser&lt;br /&gt;adviser's&lt;br /&gt;advisers&lt;br /&gt;advises&lt;br /&gt;advising&lt;br /&gt;advisor&lt;br /&gt;advisories&lt;br /&gt;advisors&lt;br /&gt;advisory&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;In order to ignore the case of alphabetic characters you can use its -f option. (similar to grep -i)&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;Exit Status:&lt;/span&gt;&lt;br /&gt;The 'look' utility exits 0 if one or more lines were found and displayed, 1 if no lines were found, and &amp;gt;1 if an error occurred.&lt;br /&gt;&lt;br /&gt;Relates posts:&lt;br /&gt;- &lt;a href="http://unstableme.blogspot.com/2008/05/generate-random-words-in-linux.html"&gt;Generate random word in UNIX/Linux from dict/words&lt;/a&gt;&lt;br /&gt;- &lt;a href="http://unstableme.blogspot.com/2009/10/grep-and-print-control-characters-in.html"&gt;UNIX - grep and print control characters from file&lt;/a&gt;&lt;br /&gt;- &lt;a href="http://unstableme.blogspot.com/2010/06/exact-match-with-unix-grep-and-awk.html"&gt;Perform exact match with UNIX grep command&lt;/a&gt;&lt;br /&gt;- &lt;a href="http://unstableme.blogspot.com/2009/02/print-only-matched-string-not-line-in.html"&gt;UNIX grep command - print only matched string and not line&lt;/a&gt;&lt;br /&gt;- &lt;a href="http://unstableme.blogspot.com/2009/03/highlight-match-with-color-in-grep.html"&gt;Highlight match with color in grep&lt;/a&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8796493175195266816-7048744007786845832?l=unstableme.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/8H3JyUtpWxPJ3YyH7ZQOKSy8q9s/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/8H3JyUtpWxPJ3YyH7ZQOKSy8q9s/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/8H3JyUtpWxPJ3YyH7ZQOKSy8q9s/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/8H3JyUtpWxPJ3YyH7ZQOKSy8q9s/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/bashscripting/~4/C8T5tViJTKY" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://unstableme.blogspot.com/feeds/7048744007786845832/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=8796493175195266816&amp;postID=7048744007786845832" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/8796493175195266816/posts/default/7048744007786845832?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/8796493175195266816/posts/default/7048744007786845832?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/bashscripting/~3/C8T5tViJTKY/unix-look-command-to-print-lines.html" title="UNIX look command to print lines" /><author><name>Jadu Saikia</name><uri>http://www.blogger.com/profile/00278385409494551967</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="28" height="32" src="http://4.bp.blogspot.com/_yqqceaP4wog/TR9u0hQQFNI/AAAAAAAACug/8mt6wpsc8Lk/S220/Jadu%2BKumar%2BSaikia.png" /></author><thr:total>0</thr:total><feedburner:origLink>http://unstableme.blogspot.com/2010/09/unix-look-command-to-print-lines.html</feedburner:origLink></entry><entry gd:etag="W/&quot;AkQEQX4-fSp7ImA9Wx5REU0.&quot;"><id>tag:blogger.com,1999:blog-8796493175195266816.post-1838531957475290105</id><published>2010-08-18T10:30:00.002+05:30</published><updated>2010-08-18T10:41:40.055+05:30</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2010-08-18T10:41:40.055+05:30</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="xargs" /><category scheme="http://www.blogger.com/atom/ns#" term="Awk" /><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="awk newbie" /><category scheme="http://www.blogger.com/atom/ns#" term="bash shell newbie" /><title>UNIX - handle Kill when no PID</title><content type="html">I was grepping for a particular process say 'Sync' in my process listing&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;$&lt;/span&gt; ps -ef | grep Sync&lt;br /&gt;testuser  26057     1  0 06:21 ?        00:00:00 /opt/nms/ety_master Sync -c 10&lt;br /&gt;testuser  26066     1  0 06:21 ?        00:00:00 /opt/nms/ety_master Sync -c 14&lt;br /&gt;testuser  26067     1  0 06:21 ?        00:00:00 /opt/nms/ety_master Sync -c 22&lt;br /&gt;testuser  26266 23299  0 06:21 pts/3    00:00:00 grep Sync&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;The 'grep' from the above listing can be removed by using a regular expression:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;$&lt;/span&gt; ps -ef | grep [S]ync&lt;br /&gt;testuser  26057     1  0 06:21 ?        00:00:00 /opt/nms/ety_master Sync -c 10&lt;br /&gt;testuser  26066     1  0 06:21 ?        00:00:00 /opt/nms/ety_master Sync -c 14&lt;br /&gt;testuser  26067     1  0 06:21 ?        00:00:00 /opt/nms/ety_master Sync -c 22&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;I wrote a one liner which basically extracts the PIDs from the above output and then kill them.&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;$&lt;/span&gt; kill -9 $(ps -ef | awk '/[S]ync/ {print $2}')&lt;br /&gt;&lt;br /&gt;ps -ef | grep [S]ync&lt;br /&gt;is same as&lt;br /&gt;ps -ef | awk '/[S]ync/'&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Now if there is no 'Sync' process running , the above command is going to throw a message like this:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;kill: usage: kill [-s sigspec | -n signum | -sigspec] pid | jobspec ... or kill -l [sigspec]&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;To handle this, here is the solution:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;$&lt;/span&gt; ps -ef | awk '/[S]ync/ {print $2}' | xargs -r kill -9&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;From XARGS(1) man page:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;--no-run-if-empty, -r&lt;br /&gt;If the standard input does not contain any nonblanks, do not run the command.&lt;br /&gt;Normally, the command is run once even if there is no input.  This option is a GNU extension.&lt;br /&gt;&lt;/pre&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8796493175195266816-1838531957475290105?l=unstableme.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/-Lh9O0IXxy_UDbU6YN39p_8mg-A/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/-Lh9O0IXxy_UDbU6YN39p_8mg-A/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/-Lh9O0IXxy_UDbU6YN39p_8mg-A/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/-Lh9O0IXxy_UDbU6YN39p_8mg-A/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/bashscripting/~4/onapxJzQBG0" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://unstableme.blogspot.com/feeds/1838531957475290105/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=8796493175195266816&amp;postID=1838531957475290105" title="4 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/8796493175195266816/posts/default/1838531957475290105?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/8796493175195266816/posts/default/1838531957475290105?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/bashscripting/~3/onapxJzQBG0/unix-handle-kill-when-no-pid.html" title="UNIX - handle Kill when no PID" /><author><name>Jadu Saikia</name><uri>http://www.blogger.com/profile/00278385409494551967</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="28" height="32" src="http://4.bp.blogspot.com/_yqqceaP4wog/TR9u0hQQFNI/AAAAAAAACug/8mt6wpsc8Lk/S220/Jadu%2BKumar%2BSaikia.png" /></author><thr:total>4</thr:total><feedburner:origLink>http://unstableme.blogspot.com/2010/08/unix-handle-kill-when-no-pid.html</feedburner:origLink></entry><entry gd:etag="W/&quot;DEIGQH0_fSp7ImA9Wx5SGEs.&quot;"><id>tag:blogger.com,1999:blog-8796493175195266816.post-7233244773501791441</id><published>2010-08-15T15:24:00.003+05:30</published><updated>2010-08-15T15:32:01.345+05:30</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2010-08-15T15:32:01.345+05:30</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="Awk" /><category scheme="http://www.blogger.com/atom/ns#" term="python newbie" /><category scheme="http://www.blogger.com/atom/ns#" term="Sed" /><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="sed newbie" /><category scheme="http://www.blogger.com/atom/ns#" term="Bash" /><title>UNIX - Print first and last line of file - sed, awk</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;50&lt;br /&gt;57&lt;br /&gt;52&lt;br /&gt;60&lt;br /&gt;52&lt;br /&gt;90&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;span style="font-weight: bold; font-style: italic;"&gt;Required output:&lt;/span&gt; Print the first and the last line of the above file.&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;50&lt;br /&gt;90&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Few alternatives using bash, sed and awk :&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;$&lt;/span&gt; head -1 file.txt; tail -1 file.txt&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;$&lt;/span&gt; sed -n '1p;$p' file.txt&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;$&lt;/span&gt; awk 'NR==1; END{print}' file.txt&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;And a simple python program to achieve the same:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;$&lt;/span&gt; cat print-first-last-line.py&lt;br /&gt;fp = open('file.txt','r')&lt;br /&gt;data = fp.readlines()&lt;br /&gt;fp.close()&lt;br /&gt;print data[0],&lt;br /&gt;print data[len(data)-1],&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Related:&lt;br /&gt;&lt;br /&gt;- &lt;a href="http://unstableme.blogspot.com/2009/05/print-next-few-lines-after-pattern-awk.html"&gt;Print next few lines after pattern is found using awk&lt;/a&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8796493175195266816-7233244773501791441?l=unstableme.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/Dg3FydkPuar7AmM95VOwBp_XVGc/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/Dg3FydkPuar7AmM95VOwBp_XVGc/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/Dg3FydkPuar7AmM95VOwBp_XVGc/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/Dg3FydkPuar7AmM95VOwBp_XVGc/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/bashscripting/~4/Y6B9ynwfvuQ" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://unstableme.blogspot.com/feeds/7233244773501791441/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=8796493175195266816&amp;postID=7233244773501791441" title="3 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/8796493175195266816/posts/default/7233244773501791441?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/8796493175195266816/posts/default/7233244773501791441?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/bashscripting/~3/Y6B9ynwfvuQ/unix-print-first-and-last-line-of-file.html" title="UNIX - Print first and last line of file - sed, awk" /><author><name>Jadu Saikia</name><uri>http://www.blogger.com/profile/00278385409494551967</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="28" height="32" src="http://4.bp.blogspot.com/_yqqceaP4wog/TR9u0hQQFNI/AAAAAAAACug/8mt6wpsc8Lk/S220/Jadu%2BKumar%2BSaikia.png" /></author><thr:total>3</thr:total><feedburner:origLink>http://unstableme.blogspot.com/2010/08/unix-print-first-and-last-line-of-file.html</feedburner:origLink></entry><entry gd:etag="W/&quot;DEMFRHc4fyp7ImA9WxFUF0U.&quot;"><id>tag:blogger.com,1999:blog-8796493175195266816.post-4815515324417661178</id><published>2010-06-29T10:01:00.004+05:30</published><updated>2010-06-29T10:10:15.937+05:30</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2010-06-29T10:10:15.937+05:30</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="Awk" /><category scheme="http://www.blogger.com/atom/ns#" term="Linux Utilities" /><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="linux grep" /><title>Exact match with UNIX grep and awk</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;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;$&lt;/span&gt; cat file.txt&lt;br /&gt;1 172.17.4.1&lt;br /&gt;2 172.17.4.5&lt;br /&gt;3 172.17.4.8&lt;br /&gt;4 172.27.4.19&lt;br /&gt;5 172.24.4.12&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Now:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;$&lt;/span&gt; grep '172.17.4.1' file.txt&lt;br /&gt;1 172.17.4.1&lt;br /&gt;4 172.17.4.19&lt;br /&gt;5 172.17.4.12&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Are you trying to grep exact '172.17.4.1' ?&lt;br /&gt;&lt;br /&gt;From grep man pages:&lt;br /&gt;&lt;br /&gt;-w, --word-regexp&lt;br /&gt;&lt;br /&gt;Select only those lines containing matches that form whole words.  The test is that the matching substring must either be at the beginning  of the  line,  or  preceded  by  a non-word constituent character.  Similarly, it must be either at the end of the line or followed by a non-word constituent character.  Word-constituent characters are letters, digits, and the underscore.&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;$&lt;/span&gt; grep -w '172.17.4.1' file.txt&lt;br /&gt;1 172.17.4.1&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Another alternative:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;$&lt;/span&gt; grep '\&amp;lt;172.17.4.1\&amp;gt;' file.txt&lt;br /&gt;1 172.17.4.1&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Normal awk match:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;$&lt;/span&gt; awk '$2 ~ /172.17.4.1/ {print $1}' file.txt&lt;br /&gt;1&lt;br /&gt;4&lt;br /&gt;5&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;To make it exact match,&lt;br /&gt;From gawk man &lt;a href="http://www.fifi.org/cgi-bin/man2html/usr/share/man/man1/gawk.1.gz"&gt;pages&lt;/a&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;\y&lt;br /&gt;matches the empty string at either the beginning or the end of a word.&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;So,&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;$&lt;/span&gt; awk '$2 ~ /\y172.17.4.1\y/ {print $1}' file.txt&lt;br /&gt;1&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;For exact match in awk, you can use this though&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;$&lt;/span&gt; awk '$2=="172.17.4.1" {print $1}' file.txt&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;br /&gt;- &lt;a href="http://unstableme.blogspot.com/2009/03/highlight-match-with-color-in-grep.html"&gt;Highlight match with color in UNIX grep command&lt;/a&gt;&lt;br /&gt;- &lt;a href="http://unstableme.blogspot.com/2009/02/print-only-matched-string-not-line-in.html"&gt;Print only matched string not line using UNIX grep&lt;/a&gt;&lt;br /&gt;- &lt;a href="http://unstableme.blogspot.com/2009/10/grep-and-print-control-characters-in.html"&gt;Grep and print control characters in file - UNIX&lt;/a&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8796493175195266816-4815515324417661178?l=unstableme.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/59ZovKnld-lhflVez14N5vr1k4c/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/59ZovKnld-lhflVez14N5vr1k4c/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/59ZovKnld-lhflVez14N5vr1k4c/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/59ZovKnld-lhflVez14N5vr1k4c/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/bashscripting/~4/Osskj8cVb6c" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://unstableme.blogspot.com/feeds/4815515324417661178/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=8796493175195266816&amp;postID=4815515324417661178" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/8796493175195266816/posts/default/4815515324417661178?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/8796493175195266816/posts/default/4815515324417661178?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/bashscripting/~3/Osskj8cVb6c/exact-match-with-unix-grep-and-awk.html" title="Exact match with UNIX grep and awk" /><author><name>Jadu Saikia</name><uri>http://www.blogger.com/profile/00278385409494551967</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="28" height="32" src="http://4.bp.blogspot.com/_yqqceaP4wog/TR9u0hQQFNI/AAAAAAAACug/8mt6wpsc8Lk/S220/Jadu%2BKumar%2BSaikia.png" /></author><thr:total>0</thr:total><feedburner:origLink>http://unstableme.blogspot.com/2010/06/exact-match-with-unix-grep-and-awk.html</feedburner:origLink></entry><entry gd:etag="W/&quot;DkMDQ3syeip7ImA9WxFUFk8.&quot;"><id>tag:blogger.com,1999:blog-8796493175195266816.post-8957482258991306252</id><published>2010-06-27T12:13:00.004+05:30</published><updated>2010-06-27T13:11:12.592+05:30</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2010-06-27T13:11:12.592+05:30</app:edited><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="Sed" /><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="sed newbie" /><title>Insert text at top of file in UNIX</title><content type="html">&lt;span style="font-weight: bold; font-style: italic;"&gt;Purpose :&lt;/span&gt; Insert text at the top and bottom of a file in UNIX.&lt;br /&gt;&lt;br /&gt;I had a lot of .in extension files in my current working directory.&lt;br /&gt;&lt;br /&gt;One of them:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;$&lt;/span&gt; cat a.in&lt;br /&gt;k|1|4|rty|3.4|67|1&lt;br /&gt;k|2|4|rty|1.4|67|1&lt;br /&gt;k|3|4|rty|3.7|62|2&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;span style="font-weight: bold; font-style: italic;"&gt;Required:&lt;/span&gt; I had to insert two lines at the top of each files and a line at the bottom of each files.&lt;br /&gt;&lt;br /&gt;e.g. After modification above file should look like this:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;$&lt;/span&gt; cat a.in&lt;br /&gt;h1|t|4&lt;br /&gt;h2|r|0&lt;br /&gt;k|1|4|rty|3.4|67|1&lt;br /&gt;k|2|4|rty|1.4|67|1&lt;br /&gt;k|3|4|rty|3.7|62|2&lt;br /&gt;#End&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;span style="font-weight: bold; font-style: italic;"&gt;Solutions:&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Unix 'Sed' utility is suitable for adding/appending, changing or inserting lines to files. You can refer one of my earlier &lt;a href="http://unstableme.blogspot.com/2007/06/some-seds.html"&gt;post&lt;/a&gt; on this.&lt;br /&gt;&lt;br /&gt;The bash script using 'sed':&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;#!/bin/sh&lt;br /&gt;&lt;br /&gt;FILES=$(echo *.in)&lt;br /&gt;&lt;br /&gt;for file in $FILES&lt;br /&gt;    do&lt;br /&gt;        echo "Modifying file : $file"&lt;br /&gt;&lt;br /&gt;        sed '1i\&lt;br /&gt;h1|t|4' $file | sed '2i\&lt;br /&gt;h2|r|0' | sed '$a\&lt;br /&gt;#End' &amp;gt; $file.tmp&lt;br /&gt;&lt;br /&gt;        mv $file.tmp $file&lt;br /&gt;done&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;With newer version of 'sed', the use of temporary files in the above bash script can be avoided by using '-i' option. Please refer &lt;a href="http://unstableme.blogspot.com/2010/01/sed-save-changes-to-same-file.html"&gt;example&lt;/a&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;-i[SUFFIX], --in-place[=SUFFIX]&lt;br /&gt;edit files in place (makes backup if extension supplied)&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;A plain bash script without using 'sed' utility would be something like this:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;for file in $(ls *.in)&lt;br /&gt;    do&lt;br /&gt;        echo "Modifying file : $file"&lt;br /&gt;    echo "h1|t|4" &gt;&gt; $file.tmp&lt;br /&gt;    echo "h2|r|0" &gt;&gt; $file.tmp&lt;br /&gt;    cat $file &gt;&gt; $file.tmp&lt;br /&gt;    echo "#End" &gt;&gt; $file.tmp&lt;br /&gt;    mv $file.tmp $file&lt;br /&gt;done&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Awk way of inserting a line of text in any line number (say insert text 'first line' as line number 1 of file.txt)&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;$&lt;/span&gt; awk 'NR==1{print "first line"}1' file.txt&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold; font-style: italic;"&gt;A related post:&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;- &lt;a href="http://unstableme.blogspot.com/2008/10/add-text-in-middle-of-file-awk-bash.html"&gt;UNIX - add text in the middle of a file using awk in bash&lt;/a&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8796493175195266816-8957482258991306252?l=unstableme.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/OxHfkfpJfKeHYIRETj9RY33FP_4/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/OxHfkfpJfKeHYIRETj9RY33FP_4/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/OxHfkfpJfKeHYIRETj9RY33FP_4/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/OxHfkfpJfKeHYIRETj9RY33FP_4/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/bashscripting/~4/uHFd4zxPTsY" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://unstableme.blogspot.com/feeds/8957482258991306252/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=8796493175195266816&amp;postID=8957482258991306252" title="2 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/8796493175195266816/posts/default/8957482258991306252?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/8796493175195266816/posts/default/8957482258991306252?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/bashscripting/~3/uHFd4zxPTsY/insert-text-at-top-of-file-in-unix.html" title="Insert text at top of file in UNIX" /><author><name>Jadu Saikia</name><uri>http://www.blogger.com/profile/00278385409494551967</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="28" height="32" src="http://4.bp.blogspot.com/_yqqceaP4wog/TR9u0hQQFNI/AAAAAAAACug/8mt6wpsc8Lk/S220/Jadu%2BKumar%2BSaikia.png" /></author><thr:total>2</thr:total><feedburner:origLink>http://unstableme.blogspot.com/2010/06/insert-text-at-top-of-file-in-unix.html</feedburner:origLink></entry><entry gd:etag="W/&quot;DEUNSXsyeip7ImA9WxFSEEk.&quot;"><id>tag:blogger.com,1999:blog-8796493175195266816.post-7199904766414513027</id><published>2010-04-12T09:48:00.002+05:30</published><updated>2010-04-12T09:54:58.592+05:30</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2010-04-12T09:54:58.592+05:30</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="bash shell" /><category scheme="http://www.blogger.com/atom/ns#" term="bash shopt" /><category scheme="http://www.blogger.com/atom/ns#" term="bash parameter substitution" /><category scheme="http://www.blogger.com/atom/ns#" term="bash extglob" /><category scheme="http://www.blogger.com/atom/ns#" term="Awk" /><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="awk newbie" /><category scheme="http://www.blogger.com/atom/ns#" term="bash shell newbie" /><category scheme="http://www.blogger.com/atom/ns#" term="sed newbie" /><title>Remove leading zero from line - awk, sed, bash</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 ids.txt&lt;br /&gt;00009&lt;br /&gt;01902&lt;br /&gt;34390&lt;br /&gt;00190&lt;br /&gt;00001&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Required: Remove the leading zero's from each of the lines in the above file.&lt;br /&gt;&lt;br /&gt;Using sed:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;$&lt;/span&gt; sed 's/^[0]*//' ids.txt&lt;br /&gt;&lt;br /&gt;Output:&lt;br /&gt;9&lt;br /&gt;1902&lt;br /&gt;34390&lt;br /&gt;190&lt;br /&gt;1&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;So in vi ex mode, the command for the same will be:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;:1,$ s/^[0]*//&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Other alternatives:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;$&lt;/span&gt; awk '{printf "%d\n",$0}' ids.txt&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;$&lt;/span&gt; awk '{print $1 + 0}' ids.txt&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;$&lt;/span&gt; cat ids.txt  | bc&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Using bash parameter substitution:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;$&lt;/span&gt; shopt | grep extglob&lt;br /&gt;extglob         off&lt;br /&gt;&lt;br /&gt;# Set this option on&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;$&lt;/span&gt; shopt -s extglob&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;$&lt;/span&gt; i=000100&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;$&lt;/span&gt; echo ${i##+(0)} &lt;br /&gt;100&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;span style="font-weight: bold; font-style: italic;"&gt;extglob&lt;/span&gt; shell option in bash:&lt;br /&gt;If set, the extended &lt;a href="http://www.bash-hackers.org/wiki/doku.php/syntax/pattern"&gt; pattern matching&lt;/a&gt; features are enabled&lt;br /&gt;&lt;br /&gt;Now if you are thinking how we can add zero's at beginning of a bash variable, here is the way using printf:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;$&lt;/span&gt; printf "%010d\n" 00005&lt;br /&gt;0000000005&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;You might look at &lt;a href="http://unstableme.blogspot.com/2009/06/padding-zeros-in-file-name-using-bash.html"&gt;this&lt;/a&gt; post which shows how we can make some numbers equal width by padding the number with leading zeroes.&lt;br /&gt;&lt;br /&gt;Some related posts:&lt;br /&gt;&lt;a href="http://unstableme.blogspot.com/2008/12/replace-leading-zero-0-with-blanks.html"&gt;Replace leading zero's with blank using sed&lt;/a&gt;&lt;br /&gt;&lt;a href="http://unstableme.blogspot.com/2009/05/remove-leading-white-space-in-vi-editor.html"&gt;Remove white space in vi editor&lt;/a&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8796493175195266816-7199904766414513027?l=unstableme.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/Izq11ILpDKfarYWNPYjcu85gD8s/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/Izq11ILpDKfarYWNPYjcu85gD8s/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/Izq11ILpDKfarYWNPYjcu85gD8s/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/Izq11ILpDKfarYWNPYjcu85gD8s/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/bashscripting/~4/Ruq2CcQzDTA" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://unstableme.blogspot.com/feeds/7199904766414513027/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=8796493175195266816&amp;postID=7199904766414513027" title="11 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/8796493175195266816/posts/default/7199904766414513027?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/8796493175195266816/posts/default/7199904766414513027?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/bashscripting/~3/Ruq2CcQzDTA/remove-leading-zero-from-line-awk-sed.html" title="Remove leading zero from line - awk, sed, bash" /><author><name>Jadu Saikia</name><uri>http://www.blogger.com/profile/00278385409494551967</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="28" height="32" src="http://4.bp.blogspot.com/_yqqceaP4wog/TR9u0hQQFNI/AAAAAAAACug/8mt6wpsc8Lk/S220/Jadu%2BKumar%2BSaikia.png" /></author><thr:total>11</thr:total><feedburner:origLink>http://unstableme.blogspot.com/2010/04/remove-leading-zero-from-line-awk-sed.html</feedburner:origLink></entry><entry gd:etag="W/&quot;C0IAQnszcSp7ImA9WxFTFkw.&quot;"><id>tag:blogger.com,1999:blog-8796493175195266816.post-1343291764522963036</id><published>2010-04-07T08:54:00.004+05:30</published><updated>2010-04-07T09:09:03.589+05:30</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2010-04-07T09:09:03.589+05:30</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="bash parameter substitution" /><category scheme="http://www.blogger.com/atom/ns#" term="bash for" /><category scheme="http://www.blogger.com/atom/ns#" term="bash loop" /><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>Simple bash script to parse log file</title><content type="html">A simple bash script; can be useful for bash newbies.&lt;br /&gt;&lt;br /&gt;Input file 'log.txt' is a log file and is of the following format:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;$&lt;/span&gt; cat log.txt&lt;br /&gt;2010-04-06 08:06:01 INFO Start ....&lt;br /&gt;2010-04-06 08:06:02 INFO Consuming file trp.1270540317.0.in&lt;br /&gt;2010-04-06 08:06:02 INFO Consuming file trp.1270540326.2.in&lt;br /&gt;2010-04-06 08:06:03 INFO Consuming file trp.1270540341.6.in&lt;br /&gt;2010-04-06 08:06:03 INFO Consuming file trp.1270540367.0.in&lt;br /&gt;2010-04-06 08:06:04 INFO End ....&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Required: For each file (trp.&amp;lt;epoch&amp;gt;.&amp;lt;id&amp;gt;.in) entry in the above file, calculate the difference in seconds between this "epoch" time-stamp and the time the file is processed.&lt;br /&gt;e.g.&lt;br /&gt;for this entry:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;2010-04-06 08:06:02 INFO Consuming file trp.1270540317.0.in&lt;br /&gt;&lt;br /&gt;Calculate this:&lt;br /&gt;&lt;br /&gt;(2010-04-06 08:06:02)-1270540317&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;The bash script:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;#!/bin/sh&lt;br /&gt;&lt;br /&gt;QFILE=log.txt&lt;br /&gt;&lt;br /&gt;grep "INFO Consuming file" $QFILE | while read line&lt;br /&gt;    do&lt;br /&gt;        filename=$(echo "$line" | awk '{print $NF}')&lt;br /&gt;        PT=$(date +%s -u -d "${line%INFO*}")&lt;br /&gt;        AT=$(echo "$line" | awk '{print $(NF-2)}' FS=\.)&lt;br /&gt;        ((diff_sec=PT-AT))&lt;br /&gt;        echo "$filename $diff_sec"&lt;br /&gt;done&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;Output:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;$&lt;/span&gt; ./show-diff.sh&lt;br /&gt;trp.1270540317.0.in 845&lt;br /&gt;trp.1270540326.2.in 836&lt;br /&gt;trp.1270540341.6.in 822&lt;br /&gt;trp.1270540367.0.in 796&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Some related posts:&lt;br /&gt;&lt;ul&gt;&lt;li&gt;&lt;a href="http://unstableme.blogspot.com/2008/10/calculate-query-response-time-awk-in.html"&gt;Parse log file and calculate query response time using awk in bash&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href="http://unstableme.blogspot.com/2009/10/bash-while-loop-sum-issue-explained.html"&gt;Bash while loop sum issue explained&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href="http://unstableme.blogspot.com/2008/12/ways-of-writing-bash-for-loop.html"&gt;Ways of writing bash for loop&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href="http://unstableme.blogspot.com/2009/10/extract-sub-string-from-variable-in.html"&gt;Extract sub string from variable in Bash&lt;/a&gt;&lt;br /&gt;&lt;/li&gt;&lt;/ul&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8796493175195266816-1343291764522963036?l=unstableme.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/qYTzwPyLNF2qNsXCn455zxZ5rMs/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/qYTzwPyLNF2qNsXCn455zxZ5rMs/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/qYTzwPyLNF2qNsXCn455zxZ5rMs/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/qYTzwPyLNF2qNsXCn455zxZ5rMs/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/bashscripting/~4/nB0FtuS1--w" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://unstableme.blogspot.com/feeds/1343291764522963036/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=8796493175195266816&amp;postID=1343291764522963036" title="7 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/8796493175195266816/posts/default/1343291764522963036?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/8796493175195266816/posts/default/1343291764522963036?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/bashscripting/~3/nB0FtuS1--w/simple-bash-script-to-parse-log-file.html" title="Simple bash script to parse log file" /><author><name>Jadu Saikia</name><uri>http://www.blogger.com/profile/00278385409494551967</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="28" height="32" src="http://4.bp.blogspot.com/_yqqceaP4wog/TR9u0hQQFNI/AAAAAAAACug/8mt6wpsc8Lk/S220/Jadu%2BKumar%2BSaikia.png" /></author><thr:total>7</thr:total><feedburner:origLink>http://unstableme.blogspot.com/2010/04/simple-bash-script-to-parse-log-file.html</feedburner:origLink></entry><entry gd:etag="W/&quot;A0IBQH4yfip7ImA9WxFTFE4.&quot;"><id>tag:blogger.com,1999:blog-8796493175195266816.post-2595676620549833867</id><published>2010-04-05T09:00:00.007+05:30</published><updated>2010-04-05T09:22:31.096+05:30</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2010-04-05T09:22:31.096+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="ubuntu tips" /><category scheme="http://www.blogger.com/atom/ns#" term="VIM" /><title>Vi editor copy and paste block example</title><content type="html">"file.txt" is of the following format:&lt;br /&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://3.bp.blogspot.com/_yqqceaP4wog/S7laWGXrmcI/AAAAAAAACcA/-53SU1JXgIE/s1600/Vi+Block+paste+-+1.png"&gt;&lt;img style="margin: 0px auto 10px; display: block; text-align: center; cursor: pointer; width: 229px; height: 164px;" src="http://3.bp.blogspot.com/_yqqceaP4wog/S7laWGXrmcI/AAAAAAAACcA/-53SU1JXgIE/s400/Vi+Block+paste+-+1.png" alt="" id="BLOGGER_PHOTO_ID_5456491759249627586" border="0" /&gt;&lt;/a&gt;&lt;br /&gt;File "dummy.txt" contains the following lines:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;$&lt;/span&gt; cat dummy.txt&lt;br /&gt;item2&lt;br /&gt;item5&lt;br /&gt;item9&lt;br /&gt;item1&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;Required:&lt;/span&gt; In Vi, Copy the above lines from "dummy.txt"  and paste to "file.txt" in the following format:&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://1.bp.blogspot.com/_yqqceaP4wog/S7laOaKUpVI/AAAAAAAACb4/EWA-gPgIzqs/s1600/Vi+Block+paste+-+2.png"&gt;&lt;img style="margin: 0px auto 10px; display: block; text-align: center; cursor: pointer; width: 200px; height: 137px;" src="http://1.bp.blogspot.com/_yqqceaP4wog/S7laOaKUpVI/AAAAAAAACb4/EWA-gPgIzqs/s400/Vi+Block+paste+-+2.png" alt="" id="BLOGGER_PHOTO_ID_5456491627123352914" border="0" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;The solution:&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;In vim:&lt;br /&gt;&lt;br /&gt;&lt;ul&gt;&lt;li&gt;In "dummy.txt", press 'Ctrl v' to go to the "Visual Block" mode.&lt;/li&gt;&lt;li&gt;Select the required block from "dummy.txt" using the keyboard arrow keys and then press "y" for copy or "x" to cut the selected block to buffer.&lt;/li&gt;&lt;li&gt;In "file.txt" move cursor to the location you want the paste the copied block (in this example its Row 3 Column 23) and in command mode press P (capital P)&lt;/li&gt;&lt;li&gt;Done !&lt;/li&gt;&lt;/ul&gt;&lt;span style="font-weight: bold;"&gt;Related posts:&lt;/span&gt;&lt;br /&gt;&lt;ul&gt;&lt;li&gt;&lt;a href="http://unstableme.blogspot.com/2010/01/vim-copy-paste-indentation-issue.html"&gt;Vim copy paste indentation issue solution&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href="http://unstableme.blogspot.com/2009/04/pull-word-under-cursor-vim-tip.html"&gt;Vi editor tip : Pull word under cursor&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href="http://unstableme.blogspot.com/2008/10/start-new-shell-from-vi-editor-vim-tips.html"&gt;Start a new shell from vi editor&lt;/a&gt;&lt;/li&gt;&lt;/ul&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8796493175195266816-2595676620549833867?l=unstableme.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/hEfmoRFbxVZqSIowSEBBcvBcT1o/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/hEfmoRFbxVZqSIowSEBBcvBcT1o/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/hEfmoRFbxVZqSIowSEBBcvBcT1o/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/hEfmoRFbxVZqSIowSEBBcvBcT1o/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/bashscripting/~4/GY6hX894Nq8" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://unstableme.blogspot.com/feeds/2595676620549833867/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=8796493175195266816&amp;postID=2595676620549833867" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/8796493175195266816/posts/default/2595676620549833867?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/8796493175195266816/posts/default/2595676620549833867?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/bashscripting/~3/GY6hX894Nq8/vi-editor-copy-and-paste-block-example.html" title="Vi editor copy and paste block example" /><author><name>Jadu Saikia</name><uri>http://www.blogger.com/profile/00278385409494551967</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="28" height="32" src="http://4.bp.blogspot.com/_yqqceaP4wog/TR9u0hQQFNI/AAAAAAAACug/8mt6wpsc8Lk/S220/Jadu%2BKumar%2BSaikia.png" /></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://3.bp.blogspot.com/_yqqceaP4wog/S7laWGXrmcI/AAAAAAAACcA/-53SU1JXgIE/s72-c/Vi+Block+paste+-+1.png" height="72" width="72" /><thr:total>0</thr:total><feedburner:origLink>http://unstableme.blogspot.com/2010/04/vi-editor-copy-and-paste-block-example.html</feedburner:origLink></entry></feed>

