// 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+one+liners?alt=json-in-script\u0026max-results=5"},{"rel":"alternate","type":"text/html","href":"https:\/\/www.theunixschool.com\/search\/label\/awk%20one%20liners"},{"rel":"hub","href":"http://pubsubhubbub.appspot.com/"},{"rel":"next","type":"application/atom+xml","href":"https:\/\/www.blogger.com\/feeds\/1255024703457423340\/posts\/default\/-\/awk+one+liners\/-\/awk+one+liners?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":"39"},"openSearch$startIndex":{"$t":"1"},"openSearch$itemsPerPage":{"$t":"5"},"entry":[{"id":{"$t":"tag:blogger.com,1999:blog-1255024703457423340.post-2973515498960038714"},"published":{"$t":"2013-03-14T16:13:00.001+05:30"},"updated":{"$t":"2013-03-14T16:13:39.799+05:30"},"category":[{"scheme":"http://www.blogger.com/atom/ns#","term":"awk one liners"},{"scheme":"http://www.blogger.com/atom/ns#","term":"ksh"},{"scheme":"http://www.blogger.com/atom/ns#","term":"perl one liners"},{"scheme":"http://www.blogger.com/atom/ns#","term":"remove leading zeros"}],"title":{"type":"text","$t":"How to remove leading zeros in a string in Linux?"},"content":{"type":"html","$t":"\u003Cspan style=\"font-size: x-large;\"\u003EH\u003C\/span\u003Eow to remove leading zeros in a variable or a string?\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nLet us consider a variable \"x\" which has the below value :\u003Cbr \/\u003E\n\u003Cpre class=\"gpr1\"\u003E$ x=0010\n$ echo $x\n0010\u003C\/pre\u003E\n\u003Cb\u003E1. Using typeset command of ksh\u003C\/b\u003E:\u003Cbr \/\u003E\n\u003Cpre class=\"gpr1\"\u003E$ typeset -LZ x\n$ x=0010\n$ echo $x\n10\u003C\/pre\u003E\n\u0026nbsp; \u0026nbsp;The variable x is declared using the typeset command. The -Z option of typeset will strip the leading zeros present when used along with -L option.\u003Cbr \/\u003E\n\u003Ca name='more'\u003E\u003C\/a\u003E\u003Cbr \/\u003E\n\u003Cb\u003E2. Using sed command\u003C\/b\u003E:\u003Cbr \/\u003E\n\u003Cpre class=\"gpr1\"\u003E$ echo $x | sed 's\/^0*\/\/'\n10\u003C\/pre\u003E\n\u0026nbsp; \u0026nbsp; The expression '^0*' will search for a sequence of 0's in the beginning and delete them.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E3. Using awk\u003C\/b\u003E :\u003Cbr \/\u003E\n\u003Cpre class=\"gpr1\"\u003E$ echo $x | awk '{sub(\/^0*\/,\"\");}1'\n10\u003C\/pre\u003E\n\u0026nbsp; \u0026nbsp; Using the sub function of awk, explanation same as sed solution.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E4. awk with printf\u003C\/b\u003E:\u003Cbr \/\u003E\n\u003Cpre class=\"gpr1\"\u003E$ echo $x| awk '{printf \"%d\\n\",$0;}'\n10\u003C\/pre\u003E\n\u0026nbsp; \u0026nbsp;Using \u003Ci\u003Eprintf\u003C\/i\u003E, use the integer format specifier %d, the leading zeros get stripped off automatically.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E5. awk using int\u003C\/b\u003E:\u003Cbr \/\u003E\n\u003Cpre class=\"gpr1\"\u003E$ echo $x | awk '{$0=int($0)}1'\n10\u003C\/pre\u003E\n\u0026nbsp; \u0026nbsp; The \u003Ci\u003Eint \u003C\/i\u003Efunction converts a expression into a integer.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E6. Perl using regex\u003C\/b\u003E:\u003Cbr \/\u003E\n\u003Cpre class=\"gpr1\"\u003E$ echo $x | perl -pe 's\/^0*\/\/;'\n10\u003C\/pre\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E7. Perl with printf\u003C\/b\u003E:\u003Cbr \/\u003E\n\u003Cpre class=\"gpr1\"\u003E$ echo $x | perl -ne 'printf \"%d\\n\",$_;'\n10\u003C\/pre\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E8. Perl with int\u003C\/b\u003E:\u003Cbr \/\u003E\n\u003Cpre class=\"gpr1\"\u003E$ echo $x | perl -pe '$_=int;'\n10\u003C\/pre\u003E\n"},"link":[{"rel":"replies","type":"application/atom+xml","href":"https:\/\/www.theunixschool.com\/feeds\/2973515498960038714\/comments\/default","title":"Post Comments"},{"rel":"replies","type":"text/html","href":"https:\/\/www.theunixschool.com\/2013\/03\/how-to-remove-leading-zeros-in-string.html#comment-form","title":"7 Comments"},{"rel":"edit","type":"application/atom+xml","href":"https:\/\/www.blogger.com\/feeds\/1255024703457423340\/posts\/default\/2973515498960038714"},{"rel":"self","type":"application/atom+xml","href":"https:\/\/www.blogger.com\/feeds\/1255024703457423340\/posts\/default\/2973515498960038714"},{"rel":"alternate","type":"text/html","href":"https:\/\/www.theunixschool.com\/2013\/03\/how-to-remove-leading-zeros-in-string.html","title":"How to remove leading zeros in a string in Linux?"}],"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":"7"}},{"id":{"$t":"tag:blogger.com,1999:blog-1255024703457423340.post-5762911412582864329"},"published":{"$t":"2013-02-06T16:37:00.000+05:30"},"updated":{"$t":"2013-02-06T16:37:31.167+05:30"},"category":[{"scheme":"http://www.blogger.com/atom/ns#","term":"awk one liners"},{"scheme":"http://www.blogger.com/atom/ns#","term":"delete line"},{"scheme":"http://www.blogger.com/atom/ns#","term":"perl one liners"},{"scheme":"http://www.blogger.com/atom/ns#","term":"sed one liners"}],"title":{"type":"text","$t":"How to delete every nth line in a file in Linux?"},"content":{"type":"html","$t":"\u003Cspan style=\"font-size: x-large;\"\u003EH\u003C\/span\u003Eow to delete or remove every nth line in a file? The requirement is to remove every 3rd line in the file.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\nLet us consider a file with the below content.\u003Cbr \/\u003E\n\u003Cpre class=\"gpr1\"\u003E$ cat file\nAIX\nSolaris\nUnix\nLinux\nHPUX\u003C\/pre\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E1. awk solution\u003C\/b\u003E :\u003Cbr \/\u003E\n\u003Cpre class=\"gpr1\"\u003E$ awk 'NR%3' file\nAIX\nSolaris\nLinux\nHPUX\u003C\/pre\u003E\n\u0026nbsp; \u0026nbsp;\u003Ci\u003ENR%3\u003C\/i\u003E will be true for any line number which is not multiple of 3, and hence the line numbers which are multiple's of 3 does not get printed. \u003Cbr \/\u003E\nNote: \u003Ci\u003ENR%3\u003C\/i\u003E is same as \u003Ci\u003ENR%3!=0\u003C\/i\u003E\u003Cbr \/\u003E\n\u003Ca name='more'\u003E\u003C\/a\u003E\u003Cbr \/\u003E\n\u003Cb\u003E2. Perl\u003C\/b\u003E:\u003Cbr \/\u003E\n\u003Cpre class=\"gpr1\"\u003E$ perl -lne 'print if $.%3 ;' file\nAIX\nSolaris\nLinux\nHPUX\u003C\/pre\u003E\n\u0026nbsp; Same logic as awk solution. \u003Ci\u003E$.\u003C\/i\u003E in perl contains the line number of the file.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E3. sed\u003C\/b\u003E:\u003Cbr \/\u003E\n\u003Cpre class=\"gpr1\"\u003E$ sed 'n;n;d;' file\nAIX\nSolaris\nLinux\nHPUX\u003C\/pre\u003E\n\u0026nbsp; \u003Ci\u003En\u003C\/i\u003E commands prints the current line and reads the next line. Hence 2 consecutive \u003Ci\u003En\u003C\/i\u003E's result in 2 lines getting printed with the 3rd line in the pattern space. \u003Ci\u003Ed\u003C\/i\u003E command deletes the line(3rd line) which is present in the pattern space. And this continues till the end of the file.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E4. Bash Shell script\u003C\/b\u003E:\u003Cbr \/\u003E\n\u003Cpre class=\"gpr1\"\u003E$ x=0\n$ while read line\n\u0026gt; do\n\u0026gt; \u0026nbsp; ((x++))\n\u0026gt; \u0026nbsp; [ $x -eq 3 ] \u0026amp;\u0026amp; { x=0; continue; }\n\u0026gt; \u0026nbsp; echo $line\n\u0026gt; done \u0026lt; file\nAIX\nSolaris\nLinux\nHPUX\u003C\/pre\u003E\n\u0026nbsp; A simple logic of incrementing a variable by 1 after every line is processed. When the count becomes 3, the particular line is not printed and the counter is reset."},"link":[{"rel":"replies","type":"application/atom+xml","href":"https:\/\/www.theunixschool.com\/feeds\/5762911412582864329\/comments\/default","title":"Post Comments"},{"rel":"replies","type":"text/html","href":"https:\/\/www.theunixschool.com\/2013\/02\/how-to-delete-every-nth-line-in-file-in.html#comment-form","title":"0 Comments"},{"rel":"edit","type":"application/atom+xml","href":"https:\/\/www.blogger.com\/feeds\/1255024703457423340\/posts\/default\/5762911412582864329"},{"rel":"self","type":"application/atom+xml","href":"https:\/\/www.blogger.com\/feeds\/1255024703457423340\/posts\/default\/5762911412582864329"},{"rel":"alternate","type":"text/html","href":"https:\/\/www.theunixschool.com\/2013\/02\/how-to-delete-every-nth-line-in-file-in.html","title":"How to delete every nth line in a file in Linux?"}],"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-8245325870819168725"},"published":{"$t":"2012-12-12T15:39:00.000+05:30"},"updated":{"$t":"2012-12-12T15:39:47.168+05:30"},"category":[{"scheme":"http://www.blogger.com/atom/ns#","term":"awk one liners"},{"scheme":"http://www.blogger.com/atom/ns#","term":"Different ways of"},{"scheme":"http://www.blogger.com/atom/ns#","term":"perl one liners"},{"scheme":"http://www.blogger.com/atom/ns#","term":"print nth line"},{"scheme":"http://www.blogger.com/atom/ns#","term":"sed one liners"},{"scheme":"http://www.blogger.com/atom/ns#","term":"shell script"}],"title":{"type":"text","$t":"How to print every nth line in a file in Linux?"},"content":{"type":"html","$t":"\u003Cspan style=\"font-size: x-large;\"\u003EH\u003C\/span\u003Eow to print every nth line of a file? \u0026nbsp;The requirement is to print every 3rd line of the file.\u003Cbr \/\u003E\n\u0026nbsp; Let us consider a file with the following content.\u003Cbr \/\u003E\n\u003Cpre class=\"gpr1\"\u003E$ cat file\nAIX\nSolaris\nUnix\nLinux\nHPUX\nUbuntu\u003C\/pre\u003E\n\u003Cb\u003E1. Using awk \u003C\/b\u003E:\u003Cbr \/\u003E\n\u003Cpre class=\"gpr1\"\u003E$ awk '!(NR%3)' file\nUnix\nUbuntu\u003C\/pre\u003E\n\u0026nbsp; NR variable contains the line number of the file in awk. If NR modulus 3 is equal to 0, it indicates the line is a multiple of 3. And only those lines are printed which modulus is 0.\u003Cbr \/\u003E\n\u003Ca name='more'\u003E\u003C\/a\u003E\u003Cbr \/\u003E\n\u003Cb\u003E2. Using sed\u003C\/b\u003E:\u003Cbr \/\u003E\n\u003Cpre class=\"gpr1\"\u003E$ sed -n 'n;n;p;' file\nUnix\nUbuntu\u003C\/pre\u003E\n\u0026nbsp; n command in sed prints the current line(if -n is not present) and reads the next line in the buffer. Since -n switch is present, the n command does not print. Hence, after the 'n;n;', the 3rd line is present in the pattern space and it is printed using the p command. And this repeats till the end of the file, and hence every 3rd line gets printed.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E3. Using Perl\u003C\/b\u003E:\u003Cbr \/\u003E\n\u003Cpre class=\"gpr1\"\u003E$ perl -lne '$.%3 or print;' file\nUnix\nUbuntu\u003C\/pre\u003E\n\u0026nbsp; This is same as awk solution. The $. variable in Perl is equivalent to awk's NR. Only those lines are printed whose modulus is not equal to 0.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E4. Bash shell script\u003C\/b\u003E:\u003Cbr \/\u003E\n\u003Cpre class=\"gpr1\"\u003E#!\/usr\/bin\/bash\n\nx=0\nwhile read line\ndo\n \u0026nbsp; ((x++))\n \u0026nbsp; [ $x -eq 3 ] \u0026amp;\u0026amp; { echo $line ; x=0;}\ndone \u0026lt; file\u003C\/pre\u003E\n\u0026nbsp;This is a pure shell solution without any \u003Ca href=\"http:\/\/www.theunixschool.com\/2012\/03\/internal-vs-external-commands.html\" target=\"_blank\"\u003Eexternal command\u003C\/a\u003E. And hence for big files, this solution will be much better in \u003Ca href=\"http:\/\/www.theunixschool.com\/2012\/06\/10-tips-to-improve-performance-of-shell.html\" target=\"_blank\"\u003Eperformance\u003C\/a\u003E. Using the \u003Ca href=\"http:\/\/www.theunixschool.com\/2012\/05\/shell-read-text-or-csv-file-and-extract.html\" target=\"_blank\"\u003Ewhile loop\u003C\/a\u003E, every line of the file is read into the \"line\" variable. And internally, a counter 'x\" is incremented, and the line is printed only when 'x' reaches 3 after which the counter is reset to 0."},"link":[{"rel":"replies","type":"application/atom+xml","href":"https:\/\/www.theunixschool.com\/feeds\/8245325870819168725\/comments\/default","title":"Post Comments"},{"rel":"replies","type":"text/html","href":"https:\/\/www.theunixschool.com\/2012\/12\/how-to-print-every-nth-line-in-file-in.html#comment-form","title":"0 Comments"},{"rel":"edit","type":"application/atom+xml","href":"https:\/\/www.blogger.com\/feeds\/1255024703457423340\/posts\/default\/8245325870819168725"},{"rel":"self","type":"application/atom+xml","href":"https:\/\/www.blogger.com\/feeds\/1255024703457423340\/posts\/default\/8245325870819168725"},{"rel":"alternate","type":"text/html","href":"https:\/\/www.theunixschool.com\/2012\/12\/how-to-print-every-nth-line-in-file-in.html","title":"How to print every nth line in a file in Linux?"}],"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-2356716916387492460"},"published":{"$t":"2012-12-03T15:34:00.001+05:30"},"updated":{"$t":"2012-12-03T15:34:13.277+05:30"},"category":[{"scheme":"http://www.blogger.com/atom/ns#","term":"awk one liners"},{"scheme":"http://www.blogger.com/atom/ns#","term":"bash"},{"scheme":"http://www.blogger.com/atom/ns#","term":"perl one liners"},{"scheme":"http://www.blogger.com/atom/ns#","term":"sed one liners"},{"scheme":"http://www.blogger.com/atom/ns#","term":"shell script"}],"title":{"type":"text","$t":"How to remove the leading and trailing spaces in a file?"},"content":{"type":"html","$t":"\u003Cspan style=\"font-size: x-large;\"\u003EH\u003C\/span\u003Eow to remove \/ delete the leading and trailing spaces in a file? How to replace a group of spaces with a single space?\u003Cbr \/\u003E\n\u0026nbsp; \u0026nbsp; Let us consider a file with the below content:\u003Cbr \/\u003E\n\u003Cpre class=\"gpr1\"\u003E$ cat file\n Linux  25\nFedora   40\nSuse 36\n    CentOS 50\nLinuxMint 15\n\u003C\/pre\u003E\nUsing the -e option of cat, the trailing spaces can be noticed easily(the $ symbol indicates end of line)\u003Cbr \/\u003E\n\u003Cpre class=\"gpr1\"\u003E$ cat -e file\n Linux  25$\nFedora   40$\nSuse 36    $\n    CentOS 50$\nLinuxMint 15$\n\u003C\/pre\u003E\n\u003Ca name='more'\u003E\u003C\/a\u003ELet us see the different ways of how to remove these spaces:\u003Cbr \/\u003E\n\u003Cb\u003E1. awk command\u003C\/b\u003E:\u003Cbr \/\u003E\n\u003Cpre class=\"gpr1\"\u003E$ awk '$1=$1' file\nLinux 25\nFedora 40\nSuse 36\nCentOS 50\nLinuxMint 15\n\u003C\/pre\u003E\n\u003Ca href=\"http:\/\/www.theunixschool.com\/p\/awk-sed.html\" target=\"_blank\"\u003Eawk \u003C\/a\u003Ehas a property wherein just by editing a field, all the whitespaces get removed automatically. Nothing changes just by assigning $1 to $1 ('$1=$1' ) and at the same time, a dummy edit has happened which will remove the whitespaces.\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E2. sed command\u003C\/b\u003E:\n\u003Cbr \/\u003E\n\u003Cpre class=\"gpr1\"\u003E$ sed 's\/^ *\/\/;s\/ *$\/\/;s\/  *\/ \/;' file\nLinux 25\nFedora 40\nSuse 36\nCentOS 50\nLinuxMint 15\n\u003C\/pre\u003E\nUsing multiple substitutions(3) in \u003Ca href=\"http:\/\/www.theunixschool.com\/p\/awk-sed.html\" target=\"_blank\"\u003Esed\u003C\/a\u003E, the spaces are removed. The 1st command removes the leading spaces, the second removes the trailing spaces and the last replaces a group of spaces with a single space. The source file itself can be updated by using the -i option of sed.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E3. Perl solution\u003C\/b\u003E:\n\u003Cbr \/\u003E\n\u003Cpre class=\"gpr1\"\u003E$ perl -plne 's\/^\\s*\/\/;s\/\\s*$\/\/;s\/\\s+\/ \/;' file\nLinux 25\nFedora 40\nSuse 36\nCentOS 50\nLinuxMint 15\n\u003C\/pre\u003E\nThis is almost same as the sed solution. Like sed, the source file itself can be updated by adding an -i option to the above command.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E4. Bash solution\u003C\/b\u003E:\n\u003Cbr \/\u003E\n\u003Cpre class=\"gpr1\"\u003E$ while read f1 f2\n\u0026gt; do\n\u0026gt;  echo $f1 $f2\n\u0026gt; done \u0026lt; file\nLinux 25\nFedora 40\nSuse 36\nCentOS 50\nLinuxMint 15\n\u003C\/pre\u003E\nUsing the \u003Ca href=\"http:\/\/www.theunixschool.com\/2012\/05\/shell-read-text-or-csv-file-and-extract.html\" target=\"_blank\"\u003Ewhile loop\u003C\/a\u003E, the 2 columns are read in variables f1 and f2. By just echoing the variables back, the spaces get removed automatically.\n"},"link":[{"rel":"replies","type":"application/atom+xml","href":"https:\/\/www.theunixschool.com\/feeds\/2356716916387492460\/comments\/default","title":"Post Comments"},{"rel":"replies","type":"text/html","href":"https:\/\/www.theunixschool.com\/2012\/12\/howto-remove-leading-trailing-spaces.html#comment-form","title":"1 Comments"},{"rel":"edit","type":"application/atom+xml","href":"https:\/\/www.blogger.com\/feeds\/1255024703457423340\/posts\/default\/2356716916387492460"},{"rel":"self","type":"application/atom+xml","href":"https:\/\/www.blogger.com\/feeds\/1255024703457423340\/posts\/default\/2356716916387492460"},{"rel":"alternate","type":"text/html","href":"https:\/\/www.theunixschool.com\/2012\/12\/howto-remove-leading-trailing-spaces.html","title":"How to remove the leading and trailing spaces in a 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":"1"}},{"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"}}]}});