// API callback
related_results_labels_thumbs({"version":"1.0","encoding":"UTF-8","feed":{"xmlns":"http://www.w3.org/2005/Atom","xmlns$openSearch":"http://a9.com/-/spec/opensearchrss/1.0/","xmlns$blogger":"http://schemas.google.com/blogger/2008","xmlns$georss":"http://www.georss.org/georss","xmlns$gd":"http://schemas.google.com/g/2005","xmlns$thr":"http://purl.org/syndication/thread/1.0","id":{"$t":"tag:blogger.com,1999:blog-1255024703457423340"},"updated":{"$t":"2024-01-15T00:41:54.209+05:30"},"category":[{"term":"awk one liners"},{"term":"perl one liners"},{"term":"different methods"},{"term":"sed one liners"},{"term":"Different ways of"},{"term":"Shell Scripting"},{"term":"commands"},{"term":"Perl"},{"term":"shells"},{"term":"awk"},{"term":"Basics"},{"term":"sed"},{"term":"Administration"},{"term":"Filesystem"},{"term":"bash"},{"term":"utilities"},{"term":"Oracle"},{"term":"find"},{"term":"VI editor"},{"term":"awk tutorial"},{"term":"join"},{"term":"sed tutorial"},{"term":"sqlplus"},{"term":"shell script"},{"term":"vim"},{"term":"Productivity"},{"term":"awk parse CSV"},{"term":"file permissions"},{"term":"grep command"},{"term":"inodes"},{"term":"ksh"},{"term":"python"},{"term":"Inodes FAQ"},{"term":"Linux Interview Questions and answers"},{"term":"date"},{"term":"env variable"},{"term":"how to"},{"term":"parse CSV file"},{"term":"perl arrays"},{"term":"perl hash"},{"term":"python3"},{"term":"sed regular expression"},{"term":"typeset"},{"term":"C programming"},{"term":"FTP"},{"term":"GNU date"},{"term":"PATH variable"},{"term":"Soft Skill"},{"term":"associative array"},{"term":"awk date functions"},{"term":"awk sed perl grep substring"},{"term":"awk sum column values"},{"term":"awk time"},{"term":"convert uppercase"},{"term":"date formatting"},{"term":"line count"},{"term":"lines below pattern"},{"term":"linux"},{"term":"mail"},{"term":"mathematical"},{"term":"paste command"},{"term":"perl foreach"},{"term":"perl function"},{"term":"perl split string"},{"term":"profile"},{"term":"ps command"},{"term":"sed insert line"},{"term":"sed replace"},{"term":"shell script duplicates"},{"term":"soft link"},{"term":"sort remove duplicates"},{"term":"source"},{"term":"special variable IFS"},{"term":"split file"},{"term":"timestamps"},{"term":"unix linux process"},{"term":"zero padding"},{"term":"CDPATH"},{"term":"CGI"},{"term":"CPAN module"},{"term":"CPU utilization"},{"term":"IFS"},{"term":"Linux shell scripting interview questions"},{"term":"NSE"},{"term":"Oracle table csv"},{"term":"SUID"},{"term":"XML"},{"term":"__DATA__"},{"term":"__END__"},{"term":"alpha vantage"},{"term":"array"},{"term":"awk concatenate values"},{"term":"awk delimiter square brackets"},{"term":"awk first record group"},{"term":"awk insert column"},{"term":"awk insert line"},{"term":"awk join with delimiter"},{"term":"awk join without delimiter"},{"term":"awk match exact word"},{"term":"awk match pattern specific column"},{"term":"awk max value"},{"term":"awk min value"},{"term":"awk mktime"},{"term":"awk pass parameters"},{"term":"awk pattern matching"},{"term":"awk perl sed change delimiter"},{"term":"awk perl sed print trailer record"},{"term":"awk print lines before pattern"},{"term":"awk read files alternatively"},{"term":"awk sed perl cut extract characters"},{"term":"awk sed perl insert line before pattern"},{"term":"awk sed perl print lines below pattern"},{"term":"awk sed perl reverse file order"},{"term":"awk sed perl reverse string"},{"term":"awk sed perl right padding"},{"term":"awk sed print first few characters"},{"term":"awk sed swap lines in file"},{"term":"awk special variables"},{"term":"awk split file and insert header"},{"term":"awk split files every n line"},{"term":"awk split files on pattern"},{"term":"awk strftime"},{"term":"awk sum group"},{"term":"awk systime"},{"term":"awk unique values"},{"term":"awk varying length delimiter"},{"term":"awk word delimiter"},{"term":"bash arithmetic"},{"term":"bash substring"},{"term":"bc"},{"term":"bc command examples"},{"term":"best practice"},{"term":"case insensitive search"},{"term":"char"},{"term":"comm"},{"term":"comm command examples"},{"term":"command line arguments"},{"term":"compare files"},{"term":"concatenate lines"},{"term":"concatenate variables"},{"term":"convert ascii to hex"},{"term":"convert csv file to query"},{"term":"convert lowercase"},{"term":"convert text to query"},{"term":"cp"},{"term":"cron"},{"term":"ctags"},{"term":"cut"},{"term":"database"},{"term":"date command"},{"term":"date conversion"},{"term":"date functions"},{"term":"delete"},{"term":"delete character"},{"term":"delete control-M"},{"term":"delete line"},{"term":"df command"},{"term":"du command"},{"term":"environment"},{"term":"epoch time"},{"term":"eulers project"},{"term":"exclude directory"},{"term":"expr command"},{"term":"extern"},{"term":"filename"},{"term":"find files only current directory"},{"term":"find files specific directory"},{"term":"find module location"},{"term":"find xargs"},{"term":"find2perl"},{"term":"fold"},{"term":"foreach"},{"term":"gawk"},{"term":"getopts"},{"term":"group count"},{"term":"heredoc"},{"term":"inode changes"},{"term":"insert line beginning"},{"term":"insert line end"},{"term":"iterators"},{"term":"join method"},{"term":"ksh arrays examples"},{"term":"ksh substring"},{"term":"leave directory"},{"term":"length"},{"term":"line number"},{"term":"lines above pattern"},{"term":"link count"},{"term":"links"},{"term":"linux command line options"},{"term":"linux date command"},{"term":"ln command"},{"term":"map"},{"term":"multiple delimiters"},{"term":"multiple pattern search"},{"term":"multiple queries"},{"term":"negative pattern search"},{"term":"omit directory"},{"term":"ord"},{"term":"pandas"},{"term":"parse text file"},{"term":"pass awk variables to shell"},{"term":"paste command join lines"},{"term":"paste command merge files"},{"term":"paste command multiple files"},{"term":"paste command standard input"},{"term":"paste read file alternatively"},{"term":"pattern match"},{"term":"pattern matching"},{"term":"performance"},{"term":"perl DATA handler"},{"term":"perl alias"},{"term":"perl append string"},{"term":"perl builtin function"},{"term":"perl date"},{"term":"perl delete string"},{"term":"perl dumper"},{"term":"perl each"},{"term":"perl eval"},{"term":"perl file find"},{"term":"perl file handling"},{"term":"perl find files"},{"term":"perl hash examples"},{"term":"perl insert line"},{"term":"perl insert string"},{"term":"perl join"},{"term":"perl qr"},{"term":"perl regular expression"},{"term":"perl replace string"},{"term":"perl sed print lines before pattern"},{"term":"perl subtring"},{"term":"perl sum digits"},{"term":"preserve timestamp"},{"term":"prime numbers"},{"term":"print hash"},{"term":"print header"},{"term":"print nth line"},{"term":"print range of lines"},{"term":"print trailer record"},{"term":"ps TIME"},{"term":"push"},{"term":"python file writing"},{"term":"python iterator"},{"term":"python list"},{"term":"python read file"},{"term":"remove duplicates"},{"term":"remove leading zeros"},{"term":"replace characters"},{"term":"replace line"},{"term":"replace string"},{"term":"replace word"},{"term":"retrieve filename"},{"term":"reverse file"},{"term":"review"},{"term":"rlogin"},{"term":"sed delete blank lines"},{"term":"sed delete header trailer"},{"term":"sed delete line before after pattern"},{"term":"sed delete line ranges"},{"term":"sed delete lines ending with"},{"term":"sed delete not containing pattern"},{"term":"sed hold space"},{"term":"sed pattern space"},{"term":"sed print line"},{"term":"sed sum"},{"term":"set options"},{"term":"setuid"},{"term":"shell script UUC"},{"term":"shell script change delimiter"},{"term":"shell script read files alternatively"},{"term":"shell script reverse string"},{"term":"shell script sum"},{"term":"shell script swap lines in file"},{"term":"shell script to insert line before pattern"},{"term":"shell variables"},{"term":"shift"},{"term":"shift command"},{"term":"sort"},{"term":"sort and merge"},{"term":"sort file numerically"},{"term":"sort find duplicates"},{"term":"sort multiple fields"},{"term":"sort reverse"},{"term":"special variables"},{"term":"split"},{"term":"split command"},{"term":"split line"},{"term":"split multiple files"},{"term":"ssh"},{"term":"string length"},{"term":"string reverse"},{"term":"substitution"},{"term":"sum file contents"},{"term":"swap columns"},{"term":"swap lines"},{"term":"sys module"},{"term":"sys.path"},{"term":"table to csv"},{"term":"tac command"},{"term":"tags"},{"term":"time command"},{"term":"time difference"},{"term":"tr"},{"term":"umask"},{"term":"unix"},{"term":"unix find"},{"term":"unix shell scripting interview questions"},{"term":"unix sort examples"},{"term":"unix time"},{"term":"update"},{"term":"values"},{"term":"wantarray example"},{"term":"while loop"},{"term":"while loop IFS"},{"term":"while read multiple delimiters"},{"term":"word count"},{"term":"write method"},{"term":"writelines method"},{"term":"xargs"},{"term":"year 2038"},{"term":"yes command"}],"title":{"type":"text","$t":"The UNIX School"},"subtitle":{"type":"html","$t":"Instructions\/Tutorials on Unix\/Linux commands, C Programming, Unix Administration , Oracle, PL SQL, Perl, Productivity tips soft skills for newbies and professionals."},"link":[{"rel":"http://schemas.google.com/g/2005#feed","type":"application/atom+xml","href":"https:\/\/www.theunixschool.com\/feeds\/posts\/default"},{"rel":"self","type":"application/atom+xml","href":"https:\/\/www.blogger.com\/feeds\/1255024703457423340\/posts\/default\/-\/awk?alt=json-in-script\u0026max-results=5"},{"rel":"alternate","type":"text/html","href":"https:\/\/www.theunixschool.com\/search\/label\/awk"},{"rel":"hub","href":"http://pubsubhubbub.appspot.com/"},{"rel":"next","type":"application/atom+xml","href":"https:\/\/www.blogger.com\/feeds\/1255024703457423340\/posts\/default\/-\/awk\/-\/awk?alt=json-in-script\u0026start-index=6\u0026max-results=5"}],"author":[{"name":{"$t":"Guru Prasad"},"uri":{"$t":"http:\/\/www.blogger.com\/profile\/07898245472802947101"},"email":{"$t":"noreply@blogger.com"},"gd$image":{"rel":"http://schemas.google.com/g/2005#thumbnail","width":"16","height":"16","src":"https:\/\/img1.blogblog.com\/img\/b16-rounded.gif"}}],"generator":{"version":"7.00","uri":"http://www.blogger.com","$t":"Blogger"},"openSearch$totalResults":{"$t":"11"},"openSearch$startIndex":{"$t":"1"},"openSearch$itemsPerPage":{"$t":"5"},"entry":[{"id":{"$t":"tag:blogger.com,1999:blog-1255024703457423340.post-7568283897640781908"},"published":{"$t":"2017-01-11T11:28:00.000+05:30"},"updated":{"$t":"2017-01-11T11:28:21.325+05:30"},"category":[{"scheme":"http://www.blogger.com/atom/ns#","term":"awk"},{"scheme":"http://www.blogger.com/atom/ns#","term":"awk pattern matching"},{"scheme":"http://www.blogger.com/atom/ns#","term":"grep command"}],"title":{"type":"text","$t":"grep vs awk - Part 2"},"content":{"type":"html","$t":"In this article, we will see more awk alternatives for the frequently used grep commands. This is in continuation to the \u003Ca href=\"http:\/\/www.theunixschool.com\/2012\/09\/grep-vs-awk-examples-for-pattern-search.html\"\u003Egrep vs awk - Part 1\u003C\/a\u003E.\nLet us consider a sample file as shown below:\n\u003Cbr \/\u003E\n\u003Cpre class=\"gpr1\"\u003E$ cat file\nUnix\n\nLinux\nUniix\nSolaris\nAIX\nArchLinux\nUbuntu\n\u003C\/pre\u003E\n\u003Cb\u003E1. Search for a pattern present in a variable\n\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cpre class=\"gpr1\"\u003E$ x=\"Linux\"\n$ grep \"$x\" file\nLinux\n\u003C\/pre\u003E\nThe variable x contains the search pattern. Using grep, the variable can directly be used.\n\u003Cbr \/\u003E\n\u003Cpre class=\"gpr1\"\u003E$ awk -v var=\"$x\" '$0 ~ var' file\nLinux\n\u003C\/pre\u003E\nIn awk, the variable cannot be used directly. It needs to be passed to awk from shell using the -v option. More about the \u003Ca href=\"http:\/\/www.theunixschool.com\/2011\/09\/awk-passing-arguments-or-shell.html\"\u003Evariable passing to awk\u003C\/a\u003E.\n\u003Cbr \/\u003E\n\u003Ca name='more'\u003E\u003C\/a\u003E\u003Cb\u003E\u003Cbr \/\u003E\u003C\/b\u003E\n\u003Cb\u003E2. Search for lines beginning with a specific pattern:\n\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cpre class=\"gpr1\"\u003E$ grep '^S' file\nSolaris\n\u003C\/pre\u003E\nThe ^ symbol is used to search for lines which begin with. In this example, we are trying to search for lines which are beginning with is S.\n\u003Cbr \/\u003E\n\u003Cpre class=\"gpr1\"\u003E$ awk '\/^S\/' file\nSolaris\n\u003C\/pre\u003E\n\u003Cb\u003E3. Search for\u0026nbsp;\u003C\/b\u003E\u003Cb\u003Elines\u003C\/b\u003E\u003Cb\u003E\u0026nbsp;ending with a specific pattern:\n\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cpre class=\"gpr1\"\u003E$ grep 'x$' file\nUnix\nUniix\nLinux\nArchLinux\n\u003C\/pre\u003E\nThe $ symbol is used to search for matches which are ending with.\n\u003Cbr \/\u003E\n\u003Cpre class=\"gpr1\"\u003E$ awk '\/x$\/' file\nUnix\nUniix\nLinux\nArchLinux\n\u003C\/pre\u003E\n\u003Cb\u003E4. To search for a exact word in a file:\n\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cpre class=\"gpr1\"\u003E$ grep -w Linux file\nLinux\n\u003C\/pre\u003E\nThe option -w is used in grep for the specific word search. The pattern ArchLinux did not retutrn because 'Linux' is just part of the the word.\n\u003Cbr \/\u003E\n\u003Cpre class=\"gpr1\"\u003E$ awk '{for(i=1;i\u0026lt;=NF;i++)if($i==\"Linux\"){print;}}' file\nLinux\n\u003C\/pre\u003E\nIn awk, we need to loop over the columns and compare \u0026nbsp;the value against every column.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E5. To get count of lines matching pattern:\n\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cpre class=\"gpr1\"\u003E$ grep -c ix file\n2\n\u003C\/pre\u003E\nThe -c option of grep gives the count of lines matching the pattern. The \"ix\" pattern is matched in 2 lines.\n\u003Cbr \/\u003E\n\u003Cpre class=\"gpr1\"\u003E$ awk '\/ix\/{x++}END{print x}' file\n2\n\u003C\/pre\u003E\nIn awk, whenever the pattern \"ix\" is matched, a variable x is incremented. At the end of the file processing, x contains the count of lines matching the pattern.\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E6. Search for a pattern which exactly matches the whole line:\n\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cpre class=\"gpr1\"\u003E$ grep -x Linux file\nLinux\n\u003C\/pre\u003E\ngrep has the -x option for matching exact matches.\n\u003Cbr \/\u003E\n\u003Cpre class=\"gpr1\"\u003E$ awk '\/^Linux$\/' file\nLinux\n\u003C\/pre\u003E\nIn awk, we use the ^ (beginning with) and $(ending with) meta characters to search for the exact match.\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E7. Search for non-empty lines or lines containing atleast one character:\n\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cpre class=\"gpr1\"\u003E$ grep . file\nUnix\nLinux\nUniix\nSolaris\nAIX\nArchLinux\nUbuntu\n\u003C\/pre\u003E\nThe . matches any character. Since empty line does not have any, it does not match.\n\u003Cbr \/\u003E\n\u003Cpre class=\"gpr1\"\u003E$ awk NF file\nUnix\nLinux\nUniix\nSolaris\nAIX\nArchLinux\nUbuntu\n\u003C\/pre\u003E\nNF indicates number of fields. Since an empty line will have NF as 0, it does not get matched.\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E8. Extract part of the line instead of the entire line:\n\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cpre class=\"gpr1\"\u003E$ grep -o \"..$\" file\nix\nux\nix\nis\nIX\ntu\n\u003C\/pre\u003E\nBy default, grep prints the entire line which matches the pattern. Using -o option only a part of string can be extracted. .. extracts 2 characters, ..$ extracts 2 characters from the end of the line.\n\u003Cbr \/\u003E\n\u003Cpre class=\"gpr1\"\u003E$ awk 'NF{print substr($0,length-1);}' file\nix\nux\nix\nis\nIX\ntu\n\u003C\/pre\u003E\nawk uses the sub-string function(substr) to do the extraction.\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nLet us consider another sample file with 2 columns in it:\n\u003Cbr \/\u003E\n\u003Cpre class=\"gpr1\"\u003E$ cat file\nUnix 2\nLinux 3\nUniix 4\nSolaris 5\n\u003C\/pre\u003E\n\u003Cb\u003E9. Extract the 2nd column from the lines containing a specific pattern :\n\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cpre class=\"gpr1\"\u003E$ grep Uni file | cut -d\" \" -f2\n2\n4\n\u003C\/pre\u003E\ngrep extracts the lines containing the pattern, and cut extracts only the 2nd column.\n\u003Cbr \/\u003E\n\u003Cpre class=\"gpr1\"\u003E$ awk '\/Uni\/{print $2}' file\n2\n4\n\u003C\/pre\u003E\nIn awk, searching a pattern and printing a particular column all are done as part of the same command.\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E10. Extracting first 3 characters from lines containing a specific pattern:\n\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cpre class=\"gpr1\"\u003E$ grep x file | cut -c 1-3\nUni\nLin\nUni\n\u003C\/pre\u003E\ngrep extracts lines containing the pattern 'x' and cut cuts the 1st three columns.\n\u003Cbr \/\u003E\n\u003Cpre class=\"gpr1\"\u003E$ awk '\/x\/{print substr($0,1,3)}' file\nUni\nLin\nUni\n\u003C\/pre\u003E\n\u003Cbr \/\u003E\nTo know\u0026amp; learn \u0026nbsp;more about the \u003Ci\u003Eawk\u003C\/i\u003E, you can refer to the \u003Ca href=\"http:\/\/www.theunixschool.com\/p\/awk-sed.html\"\u003Eawk related articles\u003C\/a\u003E."},"link":[{"rel":"replies","type":"application/atom+xml","href":"https:\/\/www.theunixschool.com\/feeds\/7568283897640781908\/comments\/default","title":"Post Comments"},{"rel":"replies","type":"text/html","href":"https:\/\/www.theunixschool.com\/2017\/01\/grep-vs-awk-part-2.html#comment-form","title":"0 Comments"},{"rel":"edit","type":"application/atom+xml","href":"https:\/\/www.blogger.com\/feeds\/1255024703457423340\/posts\/default\/7568283897640781908"},{"rel":"self","type":"application/atom+xml","href":"https:\/\/www.blogger.com\/feeds\/1255024703457423340\/posts\/default\/7568283897640781908"},{"rel":"alternate","type":"text/html","href":"https:\/\/www.theunixschool.com\/2017\/01\/grep-vs-awk-part-2.html","title":"grep vs awk - Part 2"}],"author":[{"name":{"$t":"Guru Prasad"},"uri":{"$t":"http:\/\/www.blogger.com\/profile\/07898245472802947101"},"email":{"$t":"noreply@blogger.com"},"gd$image":{"rel":"http://schemas.google.com/g/2005#thumbnail","width":"16","height":"16","src":"https:\/\/img1.blogblog.com\/img\/b16-rounded.gif"}}],"thr$total":{"$t":"0"}},{"id":{"$t":"tag:blogger.com,1999:blog-1255024703457423340.post-7793589193128503600"},"published":{"$t":"2013-01-21T15:26:00.000+05:30"},"updated":{"$t":"2013-01-21T15:26:24.335+05:30"},"category":[{"scheme":"http://www.blogger.com/atom/ns#","term":"awk"},{"scheme":"http://www.blogger.com/atom/ns#","term":"awk date functions"},{"scheme":"http://www.blogger.com/atom/ns#","term":"awk mktime"},{"scheme":"http://www.blogger.com/atom/ns#","term":"awk strftime"},{"scheme":"http://www.blogger.com/atom/ns#","term":"awk systime"},{"scheme":"http://www.blogger.com/atom/ns#","term":"awk time"},{"scheme":"http://www.blogger.com/atom/ns#","term":"GNU date"}],"title":{"type":"text","$t":"gawk -  Date and time calculation functions"},"content":{"type":"html","$t":"\u003Ca href=\"http:\/\/www.theunixschool.com\/p\/awk-sed.html\" target=\"_blank\"\u003Egawk \u003C\/a\u003Ehas 3 functions to calculate date and time:\u003Cbr \/\u003E\n\u003Cul\u003E\n\u003Cli\u003Esystime\u003C\/li\u003E\n\u003Cli\u003Estrftime\u003C\/li\u003E\n\u003Cli\u003Emktime\u003C\/li\u003E\n\u003C\/ul\u003E\n\u0026nbsp; \u0026nbsp;Let us see in this article how to use these functions:\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cu\u003Esystime\u003C\/u\u003E\u003C\/b\u003E:\u003Cbr \/\u003E\n\u0026nbsp; \u0026nbsp;This function is equivalent to the Unix date (date\u0026nbsp;+%s) command. It gives the \u003Ca href=\"http:\/\/www.theunixschool.com\/2013\/01\/what-is-unix-time.html\" target=\"_blank\"\u003EUnix time\u003C\/a\u003E, total number of seconds elapsed since the epoch(01-01-1970 00:00:00).\u003Cbr \/\u003E\n\u003Cpre class=\"gpr1\"\u003E$ echo | awk '{print systime();}'\n1358146640\u003C\/pre\u003E\nNote: systime function does not take any arguments.\u003Cbr \/\u003E\n\u003Ca name='more'\u003E\u003C\/a\u003E\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cu\u003Estrftime\u003C\/u\u003E\u003C\/b\u003E:\u003Cbr \/\u003E\n\u0026nbsp; \u0026nbsp;A very common function used in gawk to format the systime into a calendar format. Using this function, from the systime, the year, month, date, hours, mins and seconds can be separated.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nSyntax:\u003Cbr \/\u003E\n\u0026nbsp; \u003Ci\u003E\u0026nbsp;strftime (\u0026lt;format specifiers\u0026gt;,unix time)\u003C\/i\u003E;\u003Cbr \/\u003E\n\u003Cb\u003E1. Printing current date time using strftime\u003C\/b\u003E:\u003Cbr \/\u003E\n\u003Cpre class=\"gpr1\"\u003E$\u0026nbsp;echo | awk '{print strftime(\"%d-%m-%y %H-%M-%S\",systime());}'\n14-01-13 12-37-45\u003C\/pre\u003E\n\u0026nbsp; \u0026nbsp;strftime takes format specifiers which are same as the format specifiers available with the date command. %d for date, %m for month number (1 to 12), %y for the 2 digit year number, %H for the hour in 24 hour format, %M for minutes and %S for seconds. In this way, strftime converts Unix time into a \u003Ca href=\"http:\/\/www.theunixschool.com\/2012\/05\/gnu-dates-and-strings-conversion-in.html\" target=\"_blank\"\u003Edate string\u003C\/a\u003E.\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cbr \/\u003E\u003C\/b\u003E\n\u003Cb\u003E2. Display current date time using strftime without systime\u003C\/b\u003E:\u003Cbr \/\u003E\n\u003Cpre class=\"gpr1\"\u003E$ echo | awk '{print strftime(\"%d-%m-%y %H-%M-%S\");}'\n14-01-13 12-38-08\u003C\/pre\u003E\n\u0026nbsp; \u0026nbsp;Both the arguments of strftime are optional. When the timestamp is not provided, it takes the systime by default.\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cbr \/\u003E\u003C\/b\u003E\n\u003Cb\u003E3. strftime with no arguments\u003C\/b\u003E:\u003Cbr \/\u003E\n\u003Cpre class=\"gpr1\"\u003E$ echo | awk '{print strftime();}'\nMon Jan 14 12:30:05 IST 2013\u003C\/pre\u003E\n\u0026nbsp; strftime without the format specifiers provides the output in the default output format as the Unix date command.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cu\u003Emktime\u003C\/u\u003E\u003C\/b\u003E:\u003Cbr \/\u003E\n\u0026nbsp; \u0026nbsp; mktime function converts any given date time string into a Unix time, which is of the systime format.\u003Cbr \/\u003E\nSyntax:\u003Cbr \/\u003E\n\u0026nbsp; \u003Ci\u003Emktime(date time string)\u003C\/i\u003E # where date time string is a string which contains atleast 6 components in the following order: YYYY MM DD HH MM SS\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cbr \/\u003E\u003C\/b\u003E\n\u003Cb\u003E1. Printing timestamp for a specific date time \u003C\/b\u003E:\u003Cbr \/\u003E\n\u003Cpre class=\"gpr1\"\u003E$ echo | awk '{print mktime(\"2012 12 21 0 0 0\");}'\n1356028200\u003C\/pre\u003E\n\u0026nbsp; \u0026nbsp; \u0026nbsp;This gives the Unix time for the date 21-Dec-12.\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cbr \/\u003E\u003C\/b\u003E\n\u003Cb\u003E2. \u0026nbsp;Using strftime with mktime\u003C\/b\u003E:\u003Cbr \/\u003E\n\u003Cpre class=\"gpr1\"\u003E$ echo | awk '{print strftime(\"%d-%m-%Y\",mktime(\"2012 12 21 0 0 0\"));}'\n21-12-2012\u003C\/pre\u003E\n\u0026nbsp; \u0026nbsp;The output of mktime can be validated by formatting the mktime output using the strftime function as above.\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cbr \/\u003E\u003C\/b\u003E\n\u003Cb\u003E3. Negative date in mktime\u003C\/b\u003E:\u003Cbr \/\u003E\n\u003Cpre class=\"gpr1\"\u003E$ echo | awk '{print strftime(\"%d-%m-%Y\",mktime(\"2012 12 -1 0 0 0\"));}'\n29-11-2012\u003C\/pre\u003E\n\u0026nbsp; mktime can take negative values as well. -1 in the date position indicates one day before the date specified which in this case leads to 29th Nov 2012.\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cbr \/\u003E\u003C\/b\u003E\n\u003Cb\u003E4. Negative hour value in mktime\u003C\/b\u003E:\u003Cbr \/\u003E\n\u003Cpre class=\"gpr1\"\u003E$ echo | awk '{print strftime(\"%d-%m-%Y %H-%M-%S\",mktime(\"2012 12 3 -2 0 0\"));}'\n02-12-2012 22-00-00\u003C\/pre\u003E\n\u0026nbsp; \u0026nbsp; -2 in the hours position indicates 2 hours before the specified date time which in this case leads to \"2-Dec-2012 22\" hours."},"link":[{"rel":"replies","type":"application/atom+xml","href":"https:\/\/www.theunixschool.com\/feeds\/7793589193128503600\/comments\/default","title":"Post Comments"},{"rel":"replies","type":"text/html","href":"https:\/\/www.theunixschool.com\/2013\/01\/gawk-date-and-time-calculation-functions.html#comment-form","title":"0 Comments"},{"rel":"edit","type":"application/atom+xml","href":"https:\/\/www.blogger.com\/feeds\/1255024703457423340\/posts\/default\/7793589193128503600"},{"rel":"self","type":"application/atom+xml","href":"https:\/\/www.blogger.com\/feeds\/1255024703457423340\/posts\/default\/7793589193128503600"},{"rel":"alternate","type":"text/html","href":"https:\/\/www.theunixschool.com\/2013\/01\/gawk-date-and-time-calculation-functions.html","title":"gawk -  Date and time calculation functions"}],"author":[{"name":{"$t":"Guru Prasad"},"uri":{"$t":"http:\/\/www.blogger.com\/profile\/07898245472802947101"},"email":{"$t":"noreply@blogger.com"},"gd$image":{"rel":"http://schemas.google.com/g/2005#thumbnail","width":"16","height":"16","src":"https:\/\/img1.blogblog.com\/img\/b16-rounded.gif"}}],"thr$total":{"$t":"0"}},{"id":{"$t":"tag:blogger.com,1999:blog-1255024703457423340.post-3305459963482280955"},"published":{"$t":"2012-11-26T12:52:00.000+05:30"},"updated":{"$t":"2012-11-26T12:52:03.332+05:30"},"category":[{"scheme":"http://www.blogger.com/atom/ns#","term":"awk"},{"scheme":"http://www.blogger.com/atom/ns#","term":"awk insert column"},{"scheme":"http://www.blogger.com/atom/ns#","term":"awk one liners"},{"scheme":"http://www.blogger.com/atom/ns#","term":"awk parse CSV"},{"scheme":"http://www.blogger.com/atom/ns#","term":"delete"},{"scheme":"http://www.blogger.com/atom/ns#","term":"join"},{"scheme":"http://www.blogger.com/atom/ns#","term":"linux"},{"scheme":"http://www.blogger.com/atom/ns#","term":"unix"},{"scheme":"http://www.blogger.com/atom/ns#","term":"update"}],"title":{"type":"text","$t":"awk - 10 examples to insert \/ remove \/ update fields of a CSV file"},"content":{"type":"html","$t":"How to manipulate a text \/ CSV file using \u003Ca href=\"http:\/\/www.theunixschool.com\/p\/awk-sed.html\" target=\"_blank\"\u003Eawk\/gawk\u003C\/a\u003E? How to insert\/add a column between columns, remove columns, or to update a particular column? Let us discuss in this article.\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nConsider a CSV file with the following contents:\n\u003Cbr \/\u003E\n\u003Cpre class=\"gpr1\"\u003E$ cat file\nUnix,10,A\nLinux,30,B\nSolaris,40,C\nFedora,20,D\nUbuntu,50,E\n\u003C\/pre\u003E\n\u003Cb\u003E1. To insert a new column (say serial number) before the 1st column\n\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cpre class=\"gpr1\"\u003E$ awk -F, '{$1=++i FS $1;}1' OFS=, file\n1,Unix,10,A\n2,Linux,30,B\n3,Solaris,40,C\n4,Fedora,20,D\n5,Ubuntu,50,E\n\u003C\/pre\u003E\n\u003Ci\u003E$1=++i FS $1\u003C\/i\u003E =\u0026gt; Space is used to concatenate columns in awk. This  expression concatenates a new field(++i) with the 1st field along with the delimiter(FS), and assigns it back to the 1st field($1). FS contains the file delimiter.\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Ca name='more'\u003E\u003C\/a\u003E\u003Cb\u003E2. To insert a new column after the last column\n\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cpre class=\"gpr1\"\u003E$ awk -F, '{$(NF+1)=++i;}1' OFS=, file\nUnix,10,A,1\nLinux,30,B,2\nSolaris,40,C,3\nFedora,20,D,4\nUbuntu,50,E,5\n\u003C\/pre\u003E\n$NF indicates the value of last column. Hence,by assigning something to $(NF+1), a new field is inserted at the end automatically.\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E3. Add 2 columns after the last column\u003C\/b\u003E:\n\u003Cbr \/\u003E\n\u003Cpre class=\"gpr1\"\u003E$ awk -F, '{$(NF+1)=++i FS \"X\";}1' OFS=, file\nUnix,10,A,1,X\nLinux,30,B,2,X\nSolaris,40,C,3,X\nFedora,20,D,4,X\nUbuntu,50,E,5,X\n\u003C\/pre\u003E\nThe explanation gives for the above 2 examples holds good here.\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E4. To insert a column before the 2nd last column\n\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cpre class=\"gpr1\"\u003E$ awk -F, '{$(NF-1)=++i FS $(NF-1);}1' OFS=, file\nUnix,1,10,A\nLinux,2,30,B\nSolaris,3,40,C\nFedora,4,20,D\nUbuntu,5,50,E\n\u003C\/pre\u003E\nNF-1 points to the 2nd last column. Hence, by concatenating the serial number in the beginning of NF-1 ends up in inserting a column before the 2nd last.\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E5. Update 2nd column  by adding 10 to the variable\u003C\/b\u003E:\n\u003Cbr \/\u003E\n\u003Cpre class=\"gpr1\"\u003E$ awk -F, '{$2+=10;}1' OFS=, file\nUnix,20,A\nLinux,40,B\nSolaris,50,C\nFedora,30,D\nUbuntu,60,E\n\u003C\/pre\u003E\n\u0026nbsp; \u0026nbsp;$2 is incremented by 10.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E6.Convert a specific column(1st column) to uppercase in the CSV file\u003C\/b\u003E:\n\u003Cbr \/\u003E\n\u003Cpre class=\"gpr1\"\u003E$ awk -F, '{$1=toupper($1)}1' OFS=, file\nUNIX,10,A\nLINUX,30,B\nSOLARIS,40,C\nFEDORA,20,D\nUBUNTU,50,E\n\u003C\/pre\u003E\nUsing the toupper function of the awk, the 1st column is converted from \u003Ca href=\"http:\/\/www.theunixschool.com\/2012\/03\/different-ways-to-capitalize-contents.html\" target=\"_blank\"\u003Elowercase to uppercase\u003C\/a\u003E.\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E7. Extract only first 3 characters of a specific column(1st column)\u003C\/b\u003E:\n\u003Cbr \/\u003E\n\u003Cpre class=\"gpr1\"\u003E$ awk -F, '{$1=substr($1,0,3)}1' OFS=, file\nUni,10,A\nLin,30,B\nSol,40,C\nFed,20,D\nUbu,50,E\n\u003C\/pre\u003E\nUsing the substr function of awk, a \u003Ca href=\"http:\/\/www.theunixschool.com\/2012\/05\/different-ways-to-print-first-few.html\" target=\"_blank\"\u003Esubstring \u003C\/a\u003Eof only the first few characters can be retrieved.\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E8.Empty the value in the 2nd column\u003C\/b\u003E:\n\u003Cbr \/\u003E\n\u003Cpre class=\"gpr1\"\u003E$ awk -F, '{$2=\"\";}1' OFS=, file\nUnix,,A\nLinux,,B\nSolaris,,C\nFedora,,D\nUbuntu,,E\n\u003C\/pre\u003E\nSet the variable of 2nd column($2) to blank(\"\"). Now, when the line is printed, $2 will be blank.\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E9. Remove\/Delete the 2nd column from the CSV file\u003C\/b\u003E:\n\u003Cbr \/\u003E\n\u003Cpre class=\"gpr1\"\u003E$ awk -F, '{for(i=1;i\u0026lt;=NF;i++)if(i!=x)f=f?f FS $i:$i;print f;f=\"\"}' x=2 file\nUnix,A\nLinux,B\nSolaris,C\nFedora,D\nUbuntu,E\n\u003C\/pre\u003E\nBy just emptying a particular column, the column stays as is with empty value. To remove a column, all the subsequent columns from that position, needs to be advanced one position ahead. The for loop loops on all the fields. Using the \u003Ca href=\"http:\/\/www.theunixschool.com\/2012\/06\/awk-10-examples-to-group-data-in-csv-or.html\" target=\"_blank\"\u003Eternary operator\u003C\/a\u003E,\u0026nbsp;every column is concatenated to the variable \u0026nbsp;\"f\" provided it is not 2nd column using the FS as delimiter. At the end, the variable \"f\" is printed which contains the updated record. The column to be removed is passed through the awk variable \"x\" and hence just be setting the appropriate number in x, any specific column can be removed.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E10. Join 3rd column with 2nd colmn using ':' and remove the 3rd column\u003C\/b\u003E:\n\u003Cbr \/\u003E\n\u003Cpre class=\"gpr1\"\u003E$ awk -F, '{$2=$2\":\"$x;for(i=1;i\u0026lt;=NF;i++)if(i!=x)f=f?f FS $i:$i;print f;f=\"\"}' x=3 file\nUnix,10:A\nLinux,30:B\nSolaris,40:C\nFedora,20:D\nUbuntu,50:E\n\u003C\/pre\u003E\nAlmost same as last example expcept that first the 3rd column($3) is concatenated with 2nd column($2) and then removed.\n"},"link":[{"rel":"replies","type":"application/atom+xml","href":"https:\/\/www.theunixschool.com\/feeds\/3305459963482280955\/comments\/default","title":"Post Comments"},{"rel":"replies","type":"text/html","href":"https:\/\/www.theunixschool.com\/2012\/11\/awk-examples-insert-remove-update-fields.html#comment-form","title":"46 Comments"},{"rel":"edit","type":"application/atom+xml","href":"https:\/\/www.blogger.com\/feeds\/1255024703457423340\/posts\/default\/3305459963482280955"},{"rel":"self","type":"application/atom+xml","href":"https:\/\/www.blogger.com\/feeds\/1255024703457423340\/posts\/default\/3305459963482280955"},{"rel":"alternate","type":"text/html","href":"https:\/\/www.theunixschool.com\/2012\/11\/awk-examples-insert-remove-update-fields.html","title":"awk - 10 examples to insert \/ remove \/ update fields of a CSV file"}],"author":[{"name":{"$t":"Guru Prasad"},"uri":{"$t":"http:\/\/www.blogger.com\/profile\/07898245472802947101"},"email":{"$t":"noreply@blogger.com"},"gd$image":{"rel":"http://schemas.google.com/g/2005#thumbnail","width":"16","height":"16","src":"https:\/\/img1.blogblog.com\/img\/b16-rounded.gif"}}],"thr$total":{"$t":"46"}},{"id":{"$t":"tag:blogger.com,1999:blog-1255024703457423340.post-5324409490705411523"},"published":{"$t":"2012-08-06T17:00:00.000+05:30"},"updated":{"$t":"2012-08-28T13:19:43.500+05:30"},"category":[{"scheme":"http://www.blogger.com/atom/ns#","term":"awk"},{"scheme":"http://www.blogger.com/atom/ns#","term":"awk pass parameters"},{"scheme":"http://www.blogger.com/atom/ns#","term":"different methods"},{"scheme":"http://www.blogger.com/atom/ns#","term":"pass awk variables to shell"}],"title":{"type":"text","$t":"awk - Passing awk variables to shell"},"content":{"type":"html","$t":"In one of our earlier articles, we discussed how to \u003Ca href=\"http:\/\/www.theunixschool.com\/2011\/09\/awk-passing-arguments-or-shell.html\" target=\"_blank\"\u003Eaccess or pass shell variables to awk\u003C\/a\u003E. In this, we will see how to access the awk variables in shell? Or How to access awk variables as shell variables ? \u0026nbsp;Let us see the \u003Ca href=\"http:\/\/www.theunixschool.com\/p\/different-methods.html\" target=\"_blank\"\u003Edifferent ways\u003C\/a\u003E in which we can achieve this.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u0026nbsp;Let us consider a file with the sample contents as below:\n\n\u003Cbr \/\u003E\n\u003Cpre class=\"gpr1\"\u003E$ cat file\nLinux 20\nSolaris 30\nHPUX 40\n\u003C\/pre\u003E\n\u003Cb\u003E1. Access the value of the entry \"Solaris\" in a shell variable\u003C\/b\u003E, say x:\n\n\u003Cbr \/\u003E\n\u003Cpre class=\"gpr1\"\u003E$ x=`awk '\/Solaris\/{a=$2;print a}' file`\n$ echo $x\n30\n\u003C\/pre\u003E\nThis approach is fine as long as we want to access only one value. What if we have to access multiple values in shell?\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Ca name='more'\u003E\u003C\/a\u003E\u003Cb\u003E\u0026nbsp;2. Access the value of \"Solaris\" in x, and \"Linux\" in y\u003C\/b\u003E:\n\n\u003Cbr \/\u003E\n\u003Cpre class=\"gpr1\"\u003E$ z=`awk '{if($1==\"Solaris\")print \"x=\"$2;if($1==\"Linux\")print \"y=\"$2}' file`\u003C\/pre\u003E\n\u003Cpre class=\"gpr1\"\u003E$ echo \"$z\"\ny=20\nx=30\u003C\/pre\u003E\n\u003Cpre class=\"gpr1\"\u003E$ eval $z\u003C\/pre\u003E\n\u003Cpre class=\"gpr1\"\u003E$ echo $x\n30\n$ echo $y\n20\n\u003C\/pre\u003E\n\u0026nbsp; \u0026nbsp;awk sets the value of \"x\" and \"y\" awk variables and prints which is collected in the shell variable \"z\". The eval command evaluates the variable meaning it executes the commands present in the variable. As a result, \"x=30\" and \"y=20\" gets executed, and they become shell variables x and y with appropriate values.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E3. Same using the sourcing method\u003C\/b\u003E:\n\n\u003Cbr \/\u003E\n\u003Cpre class=\"gpr1\"\u003E$ awk '{if($1==\"Solaris\")print \"x=\"$2;if($1==\"Linux\")print \"y=\"$2}' file \u0026gt; f1\u003C\/pre\u003E\n\u003Cpre class=\"gpr1\"\u003E$ source f1\u003C\/pre\u003E\n\u003Cpre class=\"gpr1\"\u003E$ echo $x\n30\n$ echo $y\n20\n\u003C\/pre\u003E\nHere, instead of collecting the output of awk command in a variable, it is re-directed to a temporary file. The file is then sourced or in other words executed in the same shell. As a result, \"x\" and \"y\" become shell variables.\u003Cbr \/\u003E\nNote: Depending on the shell being used, the appropriate way of\u0026nbsp;\u003Ca href=\"http:\/\/www.theunixschool.com\/2012\/04\/what-is-sourcing-file.html\" target=\"_blank\"\u003Esourcing \u003C\/a\u003Ehas to be done. The \"source\" command is used here since the default shell is bash.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E"},"link":[{"rel":"replies","type":"application/atom+xml","href":"https:\/\/www.theunixschool.com\/feeds\/5324409490705411523\/comments\/default","title":"Post Comments"},{"rel":"replies","type":"text/html","href":"https:\/\/www.theunixschool.com\/2012\/08\/awk-passing-awk-variables-to-shell.html#comment-form","title":"2 Comments"},{"rel":"edit","type":"application/atom+xml","href":"https:\/\/www.blogger.com\/feeds\/1255024703457423340\/posts\/default\/5324409490705411523"},{"rel":"self","type":"application/atom+xml","href":"https:\/\/www.blogger.com\/feeds\/1255024703457423340\/posts\/default\/5324409490705411523"},{"rel":"alternate","type":"text/html","href":"https:\/\/www.theunixschool.com\/2012\/08\/awk-passing-awk-variables-to-shell.html","title":"awk - Passing awk variables to shell"}],"author":[{"name":{"$t":"Guru Prasad"},"uri":{"$t":"http:\/\/www.blogger.com\/profile\/07898245472802947101"},"email":{"$t":"noreply@blogger.com"},"gd$image":{"rel":"http://schemas.google.com/g/2005#thumbnail","width":"16","height":"16","src":"https:\/\/img1.blogblog.com\/img\/b16-rounded.gif"}}],"thr$total":{"$t":"2"}},{"id":{"$t":"tag:blogger.com,1999:blog-1255024703457423340.post-823002010606524484"},"published":{"$t":"2012-07-30T17:41:00.000+05:30"},"updated":{"$t":"2012-08-28T13:23:32.882+05:30"},"category":[{"scheme":"http://www.blogger.com/atom/ns#","term":"awk"},{"scheme":"http://www.blogger.com/atom/ns#","term":"awk delimiter square brackets"},{"scheme":"http://www.blogger.com/atom/ns#","term":"awk parse CSV"},{"scheme":"http://www.blogger.com/atom/ns#","term":"awk tutorial"},{"scheme":"http://www.blogger.com/atom/ns#","term":"awk varying length delimiter"},{"scheme":"http://www.blogger.com/atom/ns#","term":"awk word delimiter"},{"scheme":"http://www.blogger.com/atom/ns#","term":"multiple delimiters"}],"title":{"type":"text","$t":"awk - 10 examples to read files with multiple delimiters"},"content":{"type":"html","$t":"\u0026nbsp;In this article of \u003Ca href=\"http:\/\/www.theunixschool.com\/p\/awk-sed.html\" target=\"_blank\"\u003Eawk series\u003C\/a\u003E, we will see how to use awk to read or parse text or CSV files containing multiple delimiters or repeating delimiters. Also, we will discuss about some peculiar delimiters and how to handle them using awk.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nLet us consider a sample file. This colon separated file contains item, purchase year and a set of prices separated by a semicolon. \u003Cbr \/\u003E\n\u003Cpre class=\"gpr1\" style=\"border: 2px groove; margin: 20px; overflow: auto; padding: 6px;\"\u003E$ cat file\nItem1:2010:10;20;30\nItem2:2012:12;29;19\nItem3:2014:15;50;61\n\u003C\/pre\u003E\n\u003Cbr \/\u003E\n\u003Ca name='more'\u003E\u003C\/a\u003E\u003Cb\u003E1. To print the 3rd column\u003C\/b\u003E which contains the prices:\u003Cbr \/\u003E\n\u003Cpre class=\"gpr1\" style=\"border: 2px groove; margin: 20px; overflow: auto; padding: 6px;\"\u003E$ awk -F: '{print $3}' file\n10;20;30\n12;29;19\n15;50;61\n\u003C\/pre\u003E\n\u0026nbsp; This is straight forward. By specifying colon(:) in the option with -F, the 3rd column can be retrieved using the $3 variable.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E2. To print the 1st component of $3 alone\u003C\/b\u003E:\u003Cbr \/\u003E\n\u003Cpre class=\"gpr1\" style=\"border: 2px groove; margin: 20px; overflow: auto; padding: 6px;\"\u003E$ awk -F '[:;]' '{print $4}' file\n20\n29\n50\n\u003C\/pre\u003E\n\u003Cdiv style=\"text-align: justify;\"\u003E\n\u0026nbsp; \u0026nbsp; What did we do here? Specified multiple delimiters, one is : and other is ; . \u0026nbsp;How awk parses the file? Its simple. First, it looks at the delimiters which is colon(:) and semi-colon(;). This means, while reading the line, as and when the delimiter : or ; is encountered, \u0026nbsp;store the part read in $1. Continue further. Again on encountering one of the delimiters, store the read part in $2. And this continues till the end of the line is reached. \u0026nbsp;In this way, $4 contained the first part of the price component above.\u003C\/div\u003E\n\u003Cdiv style=\"text-align: justify;\"\u003E\nNote: \u0026nbsp; \u0026nbsp;Always keep in mind. While specifying multiple delimiters, it has to be specified inside square brackets( [;:] ).\u003C\/div\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E3. To sum the individual components of the 3rd column and print it\u003C\/b\u003E:\u003Cbr \/\u003E\n\u003Cpre class=\"gpr1\" style=\"border: 2px groove; margin: 20px; overflow: auto; padding: 6px;\"\u003E$ awk -F '[;:]' '{$3=$3+$4+$5;print $1,$2,$3}' OFS=: file\nItem1:2010:60\nItem2:2012:60\nItem3:2014:126\n\u003C\/pre\u003E\n\u003Cdiv style=\"text-align: justify;\"\u003E\n\u0026nbsp; \u0026nbsp; \u0026nbsp; The individual components of the price($3) column are available in $3, $4 and $5. Simply, sum them up and store in $3, and print all the variables. OFS (output field separator) is used to specify the delimiter while printing the output.\u003C\/div\u003E\n\u003Cdiv style=\"text-align: justify;\"\u003E\nNote: If we do not use the OFS, awk will print the fields using the default output delimiter which is space.\u003C\/div\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E4. Un-group or re-group every record depending on the price column:\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cpre class=\"gpr1\" style=\"border: 2px groove; margin: 20px; overflow: auto; padding: 6px;\"\u003E$ awk -F '[;:]' '{for(i=3;i\u0026lt;=5;i++){print $1,$2,$i;}}' OFS=\":\" file\nItem1:2010:10\nItem1:2010:20\nItem1:2010:30\nItem2:2012:12\nItem2:2012:29\nItem2:2012:19\nItem3:2014:15\nItem3:2014:50\nItem3:2014:61\n\u003C\/pre\u003E\n\u0026nbsp; \u0026nbsp; \u0026nbsp;The requirement here is: \u0026nbsp;New records have to be created for every component of the price column. Simply, a loop is run on from columns 3 to 5, and every time a record is framed using the price component.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E5-6. Read file in which the delimiter is square brackets\u003C\/b\u003E:\u003Cbr \/\u003E\n\u003Cpre class=\"gpr1\" style=\"border: 2px groove; margin: 20px; overflow: auto; padding: 6px;\"\u003E$ cat file\n123;abc[202];124\n125;abc[203];124\n127;abc[204];124\n\u003C\/pre\u003E\n\u0026nbsp; \u003Cb\u003E5. \u0026nbsp;To print the value present within the brackets\u003C\/b\u003E:\u003Cbr \/\u003E\n\u003Cpre class=\"gpr1\" style=\"border: 2px groove; margin: 20px; overflow: auto; padding: 6px;\"\u003E$ awk -F '[][]' '{print $2}' file\n202\n203\n204\n\u003C\/pre\u003E\n\u003Cdiv style=\"text-align: justify;\"\u003E\n\u0026nbsp; \u0026nbsp; \u0026nbsp;At the first sight, the delimiter used in the above command might be confusing. Its simple. 2 delimiters are to be used in this case: One is [ and the other is \u0026nbsp;]. Since the delimiters itself is square brackets which is to be placed within the square brackets, it looks tricky at the first instance.\u003C\/div\u003E\n\u003Cdiv style=\"text-align: justify;\"\u003E\n\u003Cbr \/\u003E\u003C\/div\u003E\n\u003Cdiv style=\"text-align: justify;\"\u003E\nNote: If square brackets are delimiters, it should be put in this way only, meaning first ] followed by [. Using the delimiter like -F '[[]]' will give a different interpretation altogether.\u003C\/div\u003E\n\u003Cbr \/\u003E\n\u0026nbsp; \u003Cb\u003E6. \u0026nbsp;To print the first value, the value within brackets, and the last value:\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cpre class=\"gpr1\" style=\"border: 2px groove; margin: 20px; overflow: auto; padding: 6px;\"\u003E$ awk -F '[][;]' '{print $1,$3,$5}' OFS=\";\" file\n123;202;124\n125;203;124\n127;204;124\n\u003C\/pre\u003E\n\u0026nbsp; \u0026nbsp; \u0026nbsp;3 delimiters are used in this case with semi-colon also included.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E7-8. Read or parse a file containing a series of delimiters\u003C\/b\u003E:\u003Cbr \/\u003E\n\u003Cpre class=\"gpr1\" style=\"border: 2px groove; margin: 20px; overflow: auto; padding: 6px;\"\u003E$ cat file\n123;;;202;;;203\n124;;;213;;;203\n125;;;222;;;203\n\u003C\/pre\u003E\n\u0026nbsp; \u0026nbsp; \u0026nbsp; The above file contains a series of 3 semi-colons between every 2 values.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u0026nbsp; \u0026nbsp;\u0026nbsp;\u003Cb\u003E7. Using the multiple delimiter method\u003C\/b\u003E:\u003Cbr \/\u003E\n\u003Cpre class=\"gpr1\" style=\"border: 2px groove; margin: 20px; overflow: auto; padding: 6px;\"\u003E$ awk -F'[;;;]' '{print $2}' file\n\n\n\n\u003C\/pre\u003E\n\u003Cdiv style=\"text-align: justify;\"\u003E\n\u0026nbsp; \u0026nbsp; Blank output !!! The above delimiter, though specified as 3 colons is as good as one delimiter which is a semi-colon(;) since they are all the same. Due to this, $2 will be the value between the first and the second semi-colon which in our case is blank and hence no output.\u003C\/div\u003E\n\u003Cbr \/\u003E\n\u0026nbsp; \u0026nbsp; \u0026nbsp;\u003Cb\u003E8. Using the delimiter without square brackets:\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cpre class=\"gpr1\" style=\"border: 2px groove; margin: 20px; overflow: auto; padding: 6px;\"\u003E$ awk -F';;;' '{print $2}' file\n202\n213\n222\n\u003C\/pre\u003E\n\u0026nbsp; \u0026nbsp; \u0026nbsp;The expected output !!! \u0026nbsp;No square brackets is used and we got the output which we wanted.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cdiv style=\"text-align: justify;\"\u003E\n\u003Cb\u003EDifference between using square brackets and not using it \u003C\/b\u003E: When a set of delimiters are specified using square brackets, it means an OR condition of the delimiters. For example, \u003Cb\u003E-F '[;:]'\u003C\/b\u003E means to separate the contents either on encountering ':' or ';'. However, when a set of delimiters are specified without using square brackets, awk looks at them literally to separate the contents. For example, \u003Cb\u003E-F ':;'\u003C\/b\u003E means to separate the contents only on encountering a colon followed by a semi-colon. Hence, in the last example, the file contents are separated only when a set of 3 continuous semi-colons are encountered.\u003C\/div\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E9.\u0026nbsp;Read or parse a file containing a series of delimiters of varying lengths\u003C\/b\u003E:\u003Cbr \/\u003E\n\u0026nbsp; \u0026nbsp; \u0026nbsp; In the below file, the 1st and 2nd column are separated \u0026nbsp;using 3 semi-colons, however the 2nd and 3rd are separated by 4 semi-colons\n\u003Cbr \/\u003E\n\u003Cpre class=\"gpr1\" style=\"border: 2px groove; margin: 20px; overflow: auto; padding: 6px;\"\u003E$ cat file\n123;;;202;;;;203\n124;;;213;;;;203\n125;;;222;;;;203\u003C\/pre\u003E\n\u003Cpre class=\"gpr1\" style=\"border: 2px groove; margin: 20px; overflow: auto; padding: 6px;\"\u003E$ awk -F';'+ '{print $2,$3}' file\n202 203\n213 203\n222 203\n\u003C\/pre\u003E\n\u003Cdiv style=\"text-align: justify;\"\u003E\n\u0026nbsp; \u0026nbsp; \u0026nbsp; The '+' is a regular expression. It indicates one or more of previous characters. \u003Cb\u003E';'+\u003C\/b\u003E indicates one or more semi-colons, and hence both the 3 semi-colons and 4 semi-colons get matched.\u003C\/div\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E10. \u0026nbsp;Using a word as a delimiter\u003C\/b\u003E:\u003Cbr \/\u003E\n\u003Cpre class=\"gpr1\" style=\"border: 2px groove; margin: 20px; overflow: auto; padding: 6px;\"\u003E$ cat file\n123Unix203\n124Unix203\n125Unix203\n\u003C\/pre\u003E\n\u0026nbsp; \u0026nbsp; \u0026nbsp;\u003Cb\u003ERetrieve the numbers before and after the word \"Unix\"\u003C\/b\u003E :\u003Cbr \/\u003E\n\u003Cpre class=\"gpr1\" style=\"border: 2px groove; margin: 20px; overflow: auto; padding: 6px;\"\u003E$ awk -F'Unix' '{print $1, $2}' file\n123 203\n124 203\n125 203\n\u003C\/pre\u003E\n\u003Cdiv style=\"text-align: justify;\"\u003E\n\u0026nbsp; \u0026nbsp; \u0026nbsp;In this case, we use the word \"Unix\" as the delimiter. And hence $1 and $2 contained the appropriate values . Keep in mind, it is not just the special characters which can be used as delimiters. Even alphabets, words can also be used as delimiters.\u003C\/div\u003E\n\u003Cbr \/\u003E\nP.S: We will discuss about the awk split command on how to use it in these types of multiple delimited files."},"link":[{"rel":"replies","type":"application/atom+xml","href":"https:\/\/www.theunixschool.com\/feeds\/823002010606524484\/comments\/default","title":"Post Comments"},{"rel":"replies","type":"text/html","href":"https:\/\/www.theunixschool.com\/2012\/07\/awk-10-examples-to-read-files-with.html#comment-form","title":"11 Comments"},{"rel":"edit","type":"application/atom+xml","href":"https:\/\/www.blogger.com\/feeds\/1255024703457423340\/posts\/default\/823002010606524484"},{"rel":"self","type":"application/atom+xml","href":"https:\/\/www.blogger.com\/feeds\/1255024703457423340\/posts\/default\/823002010606524484"},{"rel":"alternate","type":"text/html","href":"https:\/\/www.theunixschool.com\/2012\/07\/awk-10-examples-to-read-files-with.html","title":"awk - 10 examples to read files with multiple delimiters"}],"author":[{"name":{"$t":"Guru Prasad"},"uri":{"$t":"http:\/\/www.blogger.com\/profile\/07898245472802947101"},"email":{"$t":"noreply@blogger.com"},"gd$image":{"rel":"http://schemas.google.com/g/2005#thumbnail","width":"16","height":"16","src":"https:\/\/img1.blogblog.com\/img\/b16-rounded.gif"}}],"thr$total":{"$t":"11"}}]}});