// 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\/-\/split+file?alt=json-in-script\u0026max-results=5"},{"rel":"alternate","type":"text/html","href":"https:\/\/www.theunixschool.com\/search\/label\/split%20file"},{"rel":"hub","href":"http://pubsubhubbub.appspot.com/"}],"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":"2"},"openSearch$startIndex":{"$t":"1"},"openSearch$itemsPerPage":{"$t":"5"},"entry":[{"id":{"$t":"tag:blogger.com,1999:blog-1255024703457423340.post-6056938491932584444"},"published":{"$t":"2012-10-09T12:00:00.000+05:30"},"updated":{"$t":"2012-10-09T12:00:23.705+05:30"},"category":[{"scheme":"http://www.blogger.com/atom/ns#","term":"commands"},{"scheme":"http://www.blogger.com/atom/ns#","term":"split command"},{"scheme":"http://www.blogger.com/atom/ns#","term":"split file"},{"scheme":"http://www.blogger.com/atom/ns#","term":"split multiple files"}],"title":{"type":"text","$t":"10 examples of split command in Unix"},"content":{"type":"html","$t":"\u003Cdiv style=\"text-align: justify;\"\u003E\n\u003Cb\u003Esplit \u003C\/b\u003Ecommand in Unix is used to split a large file into smaller files. The splitting can be done on various criteria: on the basis of number of lines, or the number of output files or the byte count, etc. In an earlier article, we discussed how to \u003Ca href=\"http:\/\/www.theunixschool.com\/2012\/06\/awk-10-examples-to-split-file-into.html\" target=\"_blank\"\u003Esplit files into multiple files using awk\u003C\/a\u003E. In this, we will discuss it using the split command itself:\u003C\/div\u003E\n\u0026nbsp;Let us consider a file with the following content:\n\u003Cbr \/\u003E\n\u003Cpre class=\"gpr1\"\u003E$ cat file\nUnix\nLinux\nAIX\nSolaris\nHPUX\nUbuntu\nCygwin\n\u003C\/pre\u003E\n\u003Ca name='more'\u003E\u003C\/a\u003E\u003Cb\u003E1. Split a file :\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cpre class=\"gpr1\"\u003E$ split file\u003C\/pre\u003E\n\u0026nbsp; The split command splits the file into multiple files with 1000 lines into each output file by default. The output file generated in this case is:\u003Cbr \/\u003E\n\u003Cpre class=\"gpr1\"\u003E$ ls x*\nxaa\u003C\/pre\u003E\nSince the input file does not contain 1000 lines, all the contents are put into only one output file \"xaa\".\u0026nbsp;By default, the output files generated contains the prefix \"x\", and the suffix as \"aa\", \"ab\", \"ac\" and so on.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E2. Split file into multiple files with 3 lines each\u003C\/b\u003E:\n\u003Cbr \/\u003E\n\u003Cpre class=\"gpr1\"\u003E$ split -l 3 file\n\u003C\/pre\u003E\nThe option -l specifies the number of lines per output file. Since the input file contains 7 lines, the output files contain 3, 3 and 1 respectively.\nThe output files generated are:\n\u003Cbr \/\u003E\n\u003Cpre class=\"gpr1\"\u003E$ ls x*\nxaa  xab  xac\n$ cat xaa\nUnix\nLinux\nAIX\n\u003C\/pre\u003E\n\u0026nbsp; The file \"xab\" contains the 4th till 6th line, and the file \"xac\" contains the last line.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E3. Split file into multiple files with a user defined prefix\u003C\/b\u003E:\n\u003Cbr \/\u003E\n\u003Cpre class=\"gpr1\"\u003E$ split -l 3 file F\n$ ls F*\nFaa  Fab  Fac\n\u003C\/pre\u003E\nThe suffix, if provided, is the last argument of the split command. Since the suffix provided is \"F\", the files created are \"Faa\",\"Fab\", and so on.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E4. Split file into multiple files with a single character suffix\u003C\/b\u003E:\n\u003Cbr \/\u003E\n\u003Cpre class=\"gpr1\"\u003E$ split -l 3 -a 1 file F\n$ ls F*\nFa  Fb  Fc\n\u003C\/pre\u003E\nIn the above examples, the suffixes generated are \"aa\",\"ab\" and so on. If the number of output files to be created is huge, this makes sense. For our example, a single character suffix would suffice. The option -a of split allows to control the length of suffix. By providing the suffix length as 1, the files created are \"Fa\",\"Fb\", and so on.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E5. Split file into multiple files with a numeric suffix\u003C\/b\u003E:\n\u003Cbr \/\u003E\n\u003Cpre class=\"gpr1\"\u003E$ split -l 3 -d file F\n$ ls F*\nF00  F01  F02\n\u003C\/pre\u003E\nThe option -d of split enables a numeric suffix. With this, the files generated will be \"F00\", \"F01\", \"F02\", and so on.\n\nTo get the single digit numeric suffix:\n\u003Cbr \/\u003E\n\u003Cpre class=\"gpr1\"\u003E$ split -l 3 -a 1 -d file F\n$ ls F*\nF0  F1  F2\n\u003C\/pre\u003E\nBy enabling the option -a to 1, single digit numeric suffix is set.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E6. Split file into multiple files with 10 bytes per OUTPUT file:\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cpre class=\"gpr1\"\u003E$ split -b 10 -a 1 -d file F\n\u003C\/pre\u003E\nThe -b option of split divides the file on the basis of byte count. The byte count includes the new line character present at the end of the line as well.\n\u003Cbr \/\u003E\n\u003Cpre class=\"gpr1\"\u003E$ ls F*\nF0  F1  F2  F3  F4\n\u003C\/pre\u003E\n\u003Cpre class=\"gpr1\"\u003E$ cat F0\nUnix\nLinux\n\u003C\/pre\u003E\n\u003Cpre class=\"gpr1\"\u003E$ cat F1\n\nAIX\nSolar\n\u003C\/pre\u003E\nThe file F0 contains 10 characters 5 characters of first line (Unix + new line) and 5 characters of second line (Linux). The new line character of the 2nd line moved to the 2nd output file.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E7. Split file with Kilobytes or Megabytes of data per OUTPUT file\u003C\/b\u003E:\n\u003Cbr \/\u003E\n\u003Cpre class=\"gpr1\"\u003E$ split -b 1k file\n\u003C\/pre\u003E\nThis will split the file with 1 KB of data per OUTPUT file.  Similarly, to split the file with 1MB of data per OUTPUT file:\n\u003Cbr \/\u003E\n\u003Cpre class=\"gpr1\"\u003E$ split -b 1m file\n\u003C\/pre\u003E\nNote: The commands below use the option -n which is not available in all Unix flavors.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E8. Split a file into 2 files of equal length\u003C\/b\u003E:\n\u003Cbr \/\u003E\n\u003Cpre class=\"gpr1\"\u003E$ split -n 2 -a 1 -d file F\n\u003C\/pre\u003E\nAt times, the requirement can be to split a file equally into 2 files, unlike earlier case where the split is based on number of lines per output file. The n option of split does this. By specifying the \"-n 2\", the file is split equally into 2 files as shown below:\n\u003Cbr \/\u003E\n\u003Cpre class=\"gpr1\"\u003E$ ls F*\nF0  F1\n\u003C\/pre\u003E\n\u003Cpre class=\"gpr1\"\u003E$ cat F0\nUnix\nLinux\nAIX\nSolari\n\u003C\/pre\u003E\n\u003Cpre class=\"gpr1\"\u003E$ cat F1\ns\nHPUX\nUbuntu\nCygwin\n\u003C\/pre\u003E\nNote: -n divides the file into equal lengths on the basis of the byte count of the files. As shown above, since the file has 42 characters, it is divided into 21 characters each.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E9. Split file into 2 files with complete lines of output\u003C\/b\u003E:\n\u003Cbr \/\u003E\n\u003Cpre class=\"gpr1\"\u003E$ split -n l\/2 -a1 -d file F\n\u003C\/pre\u003E\nThe option \"-n l\/2\" enables to split on the basis of complete lines. And hence, the file F0 contains the complete 4th line \"Solaris\", and the rest goes to the 2nd file.\n\u003Cbr \/\u003E\n\u003Cpre class=\"gpr1\"\u003E$ ls F*\nF0  F1\n\u003C\/pre\u003E\n\u003Cpre class=\"gpr1\"\u003E$ cat F0\nUnix\nLinux\nAIX\nSolaris\n\u003C\/pre\u003E\n\u003Cb\u003E10. split command to display only a section of the file\u003C\/b\u003E:\n\u003Cbr \/\u003E\n\u003Cpre class=\"gpr1\"\u003E$ split -n 1\/4  file\nUnix\nLinux\n\u003C\/pre\u003E\nThe option \"-n 1\/4\" does not create any output files. It simply displays the file. 4 indicates to split the file into 4 equal parts or sections, and 1\/4 indicates to write to stdout the 1st of the 4 sections. In other words, it displays the 1st part in the terminal. \n  \n  Similarly, to display the 2nd of the 4 parts:\n\u003Cbr \/\u003E\n\u003Cpre class=\"gpr1\"\u003E$ split -n 2\/4 file\n\nAIX\nSolar\n\u003C\/pre\u003E\nNote: As seen above, the output does not contain complete lines. The split is done purely on the basis of equal byte count.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E\u0026nbsp;Split file with complete lines\u003C\/b\u003E:\n\u003Cbr \/\u003E\n\u003Cpre class=\"gpr1\"\u003E$ split -n l\/1\/4  file\nUnix\nLinux\n\u003C\/pre\u003E\n\u003Cpre class=\"gpr1\"\u003E$ split -n l\/2\/4  file\nAIX\nSolaris\n\u003C\/pre\u003E\n\u0026nbsp;By specifying the l option, the split is done at the completion of the line."},"link":[{"rel":"replies","type":"application/atom+xml","href":"https:\/\/www.theunixschool.com\/feeds\/6056938491932584444\/comments\/default","title":"Post Comments"},{"rel":"replies","type":"text/html","href":"https:\/\/www.theunixschool.com\/2012\/10\/10-examples-of-split-command-in-unix.html#comment-form","title":"2 Comments"},{"rel":"edit","type":"application/atom+xml","href":"https:\/\/www.blogger.com\/feeds\/1255024703457423340\/posts\/default\/6056938491932584444"},{"rel":"self","type":"application/atom+xml","href":"https:\/\/www.blogger.com\/feeds\/1255024703457423340\/posts\/default\/6056938491932584444"},{"rel":"alternate","type":"text/html","href":"https:\/\/www.theunixschool.com\/2012\/10\/10-examples-of-split-command-in-unix.html","title":"10 examples of split command in Unix"}],"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-8338870947509621309"},"published":{"$t":"2012-06-28T18:31:00.000+05:30"},"updated":{"$t":"2020-07-11T10:18:57.136+05:30"},"category":[{"scheme":"http://www.blogger.com/atom/ns#","term":"awk"},{"scheme":"http://www.blogger.com/atom/ns#","term":"awk one liners"},{"scheme":"http://www.blogger.com/atom/ns#","term":"awk split file and insert header"},{"scheme":"http://www.blogger.com/atom/ns#","term":"awk split files every n line"},{"scheme":"http://www.blogger.com/atom/ns#","term":"awk split files on pattern"},{"scheme":"http://www.blogger.com/atom/ns#","term":"split file"}],"title":{"type":"text","$t":"awk - 10 examples to split a file into multiple files"},"content":{"type":"html","$t":"\u0026nbsp;In this article of the \u003Ca href=\"http:\/\/unix-school.blogspot.com\/p\/awk-sed.html\" target=\"_blank\"\u003Eawk series\u003C\/a\u003E, we will see the different scenarios in which we need to split a file into multiple files using \u003Ca href=\"http:\/\/unix-school.blogspot.com\/search\/label\/awk\" target=\"_blank\"\u003Eawk\u003C\/a\u003E. The files can be split into multiple files either based on a condition, or based on a pattern or because the file is big and hence needs to split into smaller files.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cu style=\"background-color: white;\"\u003ESample File1:\u003C\/u\u003E\u003Cbr \/\u003E\nLet us consider a sample file with the following contents:\u003Cbr \/\u003E\n\u003Cpre class=\"gpr1\" style=\"border: 2px groove; margin: 20px; overflow: auto; padding: 6px;\"\u003E$ cat file1\nItem1,200\nItem2,500\nItem3,900\nItem2,800\nItem1,600\n\u003C\/pre\u003E\n\u003Ca name='more'\u003E\u003C\/a\u003E\u003Cb\u003E1. Split the file into 3 different files, one for each item\u003C\/b\u003E. i.e, All records pertaining to Item1 into a file, records of Item2 into another, etc.\u003Cbr \/\u003E\n\u003Cpre class=\"gpr1\" style=\"border: 2px groove; margin: 20px; overflow: auto; padding: 6px;\"\u003E$ awk -F, '{print \u0026gt; $1}' file1\n\u003C\/pre\u003E\n\u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp;The files generated by the above co\u003Cspan style=\"background-color: white;\"\u003Emmand are as \u0026nbsp;below:\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cpre class=\"gpr1\" style=\"border: 2px groove; margin: 20px; overflow: auto; padding: 6px;\"\u003E$ cat Item1\nItem1,200\nItem1,600\u003C\/pre\u003E\n\u003Cpre class=\"gpr1\" style=\"border: 2px groove; margin: 20px; overflow: auto; padding: 6px;\"\u003E$ cat Item3\nItem3,900\u003C\/pre\u003E\n\u003Cpre class=\"gpr1\" style=\"border: 2px groove; margin: 20px; overflow: auto; padding: 6px;\"\u003E$ cat Item2\nItem2,500\nItem2,800\n\u003C\/pre\u003E\n\u0026nbsp; \u0026nbsp;This looks so simple, right?\u0026nbsp;\u003Ci\u003Eprint\u003C\/i\u003E\u0026nbsp;prints the entire line, and the line is printed to a file whose name is $1, which is the first field. This means, the first record will get written to a file named 'Item1', and the second record to 'Item2', third to 'Item3', \u0026nbsp;4th goes to 'Item2', and so on.\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E2. Split the files by having an extension of .txt to the new file names\u003C\/b\u003E.\u003Cbr \/\u003E\n\u003Cpre class=\"gpr1\" style=\"border: 2px groove; margin: 20px; overflow: auto; padding: 6px;\"\u003E$ awk -F, '{print \u0026gt; $1\u003Cspan style=\"color: red;\"\u003E\".txt\"\u003C\/span\u003E}' file1\n\u003C\/pre\u003E\n\u0026nbsp; \u0026nbsp; The only change here from the above is concatenating the string \".txt\" to the $1 which is the first field. As a result, we get the extension to the file names. The files created are below:\u003Cbr \/\u003E\n\u003Cpre class=\"gpr1\" style=\"border: 2px groove; margin: 20px; overflow: auto; padding: 6px;\"\u003E$ ls *.txt\nItem2.txt  Item1.txt  Item3.txt\n\u003C\/pre\u003E\n\u003Cb\u003E3. Split the files by having only the value(the second field) in the individual files\u003C\/b\u003E, i.e, only 2nd field in the new files without the 1st field:\u003Cbr \/\u003E\n\u003Cpre class=\"gpr1\" style=\"border: 2px groove; margin: 20px; overflow: auto; padding: 6px;\"\u003E$ awk -F, '{print $2 \u0026gt; $1\".txt\"}' file1\n\u003C\/pre\u003E\n\u0026nbsp; \u0026nbsp; \u0026nbsp;The \u003Ci\u003Eprint\u003C\/i\u003E\u0026nbsp;command prints the entire record. Since we want only the second field to go to the output files, we do: \u003Ci\u003Eprint $2.\u003C\/i\u003E\u003Cbr \/\u003E\n\u003Cpre class=\"gpr1\" style=\"border: 2px groove; margin: 20px; overflow: auto; padding: 6px;\"\u003E$ cat Item1.txt\n200\n600\n\u003C\/pre\u003E\n\u003Cb\u003E4. Split the files so that all the items whose value is greater than 500 are in the file \"500G.txt\"\u003C\/b\u003E, and the rest in the \u0026nbsp;file \"500L.txt\".\u003Cbr \/\u003E\n\u003Cpre class=\"gpr1\" style=\"border: 2px groove; margin: 20px; overflow: auto; padding: 6px;\"\u003E$ awk -F, '{if($2\u0026lt;=500)print \u0026gt; \"500L.txt\";else print \u0026gt; \"500G.txt\"}' file1\n\u003C\/pre\u003E\n\u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u003Cspan style=\"background-color: white;\"\u003EThe output files created will be as below:\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cpre class=\"gpr1\" style=\"border: 2px groove; margin: 20px; overflow: auto; padding: 6px;\"\u003E$ cat 500L.txt\nItem1,200\nItem2,500\u003C\/pre\u003E\n\u003Cpre class=\"gpr1\" style=\"border: 2px groove; margin: 20px; overflow: auto; padding: 6px;\"\u003E$ cat 500G.txt\nItem3,900\nItem2,800\nItem1,600\n\u003C\/pre\u003E\n\u0026nbsp; \u0026nbsp; \u0026nbsp;\u003Cspan style=\"background-color: white;\"\u003ECheck the second field($2). If it is lesser or equal to 500, the record goes to \"500L.txt\", else to \"500G.txt\".\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cspan style=\"background-color: white;\"\u003E\u0026nbsp; Other way to achieve the same thing is using the ternary operator in awk:\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cpre class=\"gpr1\" style=\"border: 2px groove; margin: 20px; overflow: auto; padding: 6px;\"\u003E$ awk -F, '{x=($2\u0026lt;=500)?\"500L.txt\":\"500G.txt\"; print \u0026gt; x}' file1\n\u003C\/pre\u003E\n\u003Cdiv style=\"text-align: justify;\"\u003E\n\u0026nbsp; \u0026nbsp; \u0026nbsp; The condition for greater or lesser than 500 is checked and the appropriate file name is assigned to variable \u003Ci\u003Ex\u003C\/i\u003E. The record is then written to the file present in the variable x.\u003C\/div\u003E\n\u003Cbr \/\u003E\n\u003Cu\u003ESample File2:\u003C\/u\u003E\u003Cbr \/\u003E\nLet us consider another file with a different set of contents. This file has a pattern 'START' at frequent intervals.\u003Cbr \/\u003E\n\u003Cpre class=\"gpr1\" style=\"border: 2px groove; margin: 20px; overflow: auto; padding: 6px;\"\u003E$ cat file2\nSTART\nUnix\nLinux\nSTART\nSolaris\nAix\nSCO\n\u003C\/pre\u003E\n\u003Cb\u003E5.\u0026nbsp;\u003Cspan style=\"background-color: white;\"\u003ESplit the file into multiple files at every occurrence of the pattern START .\u003C\/span\u003E\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cpre class=\"gpr1\" style=\"border: 2px groove; margin: 20px; overflow: auto; padding: 6px;\"\u003E$ awk '\/START\/{x=\"F\"++i;}{print \u0026gt; x;}' file2\n\u003C\/pre\u003E\n\u003Cdiv style=\"text-align: justify;\"\u003E\n\u0026nbsp; \u0026nbsp; \u0026nbsp;This command contains 2 sets of curly braces: The control goes to the first set of braces only on encountering a line containing the pattern START. The second set will be encountered by every line since there is no condition and hence always true. \u003C\/div\u003E\n\u003Cdiv style=\"text-align: justify;\"\u003E\n\u003Cspan style=\"background-color: white;\"\u003E\u0026nbsp; \u0026nbsp;On encountering the pattern START, a new file name is created and stored. When the first START comes, x will contain \"F1\" and the control goes to the next set of braces and the record is written to F1, and the subsequent records go the file \"F1\" till the next START comes. On encountering next START, x will contain \"F2\" and the subsequent lines goes to \"F2\" till the next START, and it continues.\u003C\/span\u003E\u003C\/div\u003E\n\u003Cpre class=\"gpr1\" style=\"border: 2px groove; margin: 20px; overflow: auto; padding: 6px;\"\u003E$ cat F1\nSTART\nUnix\nLinux\nSolaris\u003C\/pre\u003E\n\u003Cpre class=\"gpr1\" style=\"border: 2px groove; margin: 20px; overflow: auto; padding: 6px;\"\u003E$ cat F2\nSTART\nAix\nSCO\n\u003C\/pre\u003E\n\u003Cb\u003E6. Split the file into multiple files at every occurrence of the pattern START. But the line containing the pattern\u003C\/b\u003E\u0026nbsp;should not be in the new files.\u003Cbr \/\u003E\n\u003Cpre class=\"gpr1\" style=\"border: 2px groove; margin: 20px; overflow: auto; padding: 6px;\"\u003E$ awk '\/START\/{x=\"F\"++i;\u003Cspan style=\"color: red;\"\u003Enext\u003C\/span\u003E}{print \u0026gt; x;}' file2\n\u003C\/pre\u003E\n\u003Cdiv style=\"text-align: justify;\"\u003E\n\u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp;The only difference in this from the above is the inclusion of the \u003Ci\u003Enext\u003C\/i\u003E\u0026nbsp;command. Due to the next command, the lines containing the START enters the first curly braces and then starts reading the next line immediately due to the \u003Ci\u003Enext\u003C\/i\u003E\u0026nbsp;command. As a result, the START lines does not get to the second curly braces and hence the START does not appear in the split files.\u003C\/div\u003E\n\u003Cpre class=\"gpr1\" style=\"border: 2px groove; margin: 20px; overflow: auto; padding: 6px;\"\u003E$ cat F1\nUnix\nLinux\u003C\/pre\u003E\n\u003Cpre class=\"gpr1\" style=\"border: 2px groove; margin: 20px; overflow: auto; padding: 6px;\"\u003E$ cat F2\nSolaris\nAix\nSCO\n\u003C\/pre\u003E\n\u003Cb\u003E7. Split the file by inserting a header record in every new file.\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cpre class=\"gpr1\" style=\"border: 2px groove; margin: 20px; overflow: auto; padding: 6px;\"\u003E$ awk '\/START\/{x=\"F\"++i;\u003Cspan style=\"color: red;\"\u003Eprint \"ANY HEADER\" \u0026gt; x\u003C\/span\u003E;next}{print \u0026gt; x;}' file2\n\u003C\/pre\u003E\n\u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp;The change here from the earlier one is this: Before the \u003Ci\u003Enext\u003C\/i\u003E\u0026nbsp;command, we write the header record into the file. This is the right place to write the header record since this is where the file is created first.\u003Cbr \/\u003E\n\u003Cpre class=\"gpr1\" style=\"border: 2px groove; margin: 20px; overflow: auto; padding: 6px;\"\u003E$ cat F1\nANY HEADER\nUnix\nLinux\n\u003C\/pre\u003E\n\u003Cpre class=\"gpr1\" style=\"border: 2px groove; margin: 20px; overflow: auto; padding: 6px;\"\u003E$ cat F2\nANY HEADER\nSolaris\nAix\nSCO\n\u003C\/pre\u003E\n\u003Cu\u003ESample File3:\u003C\/u\u003E\n\u003Cbr \/\u003E\nLet us consider a file with the sample contents:\u003Cbr \/\u003E\n\u003Cpre class=\"gpr1\" style=\"border: 2px groove; margin: 20px; overflow: auto; padding: 6px;\"\u003E$ cat file3\nUnix\nLinux\nSolaris\nAIX\nSCO\n\u003C\/pre\u003E\n\u003Cb\u003E8. Split the file into multiple files at every 3rd line \u003C\/b\u003E. i.e, First 3 lines into F1, next 3 lines into F2 and so on.\u003Cbr \/\u003E\n\u003Cpre class=\"gpr1\" style=\"border: 2px groove; margin: 20px; overflow: auto; padding: 6px;\"\u003E$ awk 'NR%3==1{x=\"F\"++i;}{print \u0026gt; x}'  file3\n\u003C\/pre\u003E\n\u003Cdiv style=\"text-align: justify;\"\u003E\n\u0026nbsp; \u0026nbsp; \u0026nbsp; In other words, this is nothing but splitting the file into equal parts. The condition does the trick here: NR%3==1 : NR is the line number of the current record. NR%3 will be equal to 1 for every 3rd line such as 1st, 4th, 7th and so on. And at every 3rd line, the file name is changed in the variable x, and hence the records are written to the appropriate files.\u003C\/div\u003E\n\u003Cpre class=\"gpr1\" style=\"border: 2px groove; margin: 20px; overflow: auto; padding: 6px;\"\u003E$ cat F1\nUnix\nLinux\nSolaris\u003C\/pre\u003E\n\u003Cpre class=\"gpr1\" style=\"border: 2px groove; margin: 20px; overflow: auto; padding: 6px;\"\u003E$ cat F2\nAix\nSCO\u0026nbsp;\u003C\/pre\u003E\n\u003Cspan style=\"background-color: white;\"\u003E\u003Cu\u003ESample File4:\u003C\/u\u003E\u003C\/span\u003E\u003Cbr \/\u003E\nLet us update the above file with a header and trailer:\u003Cbr \/\u003E\n\u003Cpre class=\"gpr1\" style=\"border: 2px groove; margin: 20px; overflow: auto; padding: 6px;\"\u003E$ cat file4\nHEADER\nUnix\nLinux\nSolaris\nAIX\nSCO\nTRAILER\u003Cspan style=\"font-family: 'Times New Roman';\"\u003E\u003Cspan style=\"white-space: normal;\"\u003E\n\u003C\/span\u003E\u003C\/span\u003E\u003C\/pre\u003E\n\u003Cspan style=\"background-color: white;\"\u003E\u003Cb\u003E9. Split the file at every 3rd line without the header and trailer in the new files.\u003C\/b\u003E\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cpre class=\"gpr1\" style=\"border: 2px groove; margin: 20px; overflow: auto; padding: 6px;\"\u003Esed '1d;$d;' file4 | awk 'NR%3==1{x=\"F\"++i;}{print \u0026gt; x}' \n\u003C\/pre\u003E\n\u003Cdiv style=\"text-align: justify;\"\u003E\n\u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp;The earlier command does the work for us, only thing is to pass to the above command without the header and trailer. sed does it for us. \u0026nbsp;'1d' is to delete the 1st line, '$d' to delete the last line.\u003C\/div\u003E\n\u003Cpre class=\"gpr1\" style=\"border: 2px groove; margin: 20px; overflow: auto; padding: 6px;\"\u003E$ cat F1\nUnix\nLinux\nSolaris\n\u003C\/pre\u003E\n\u003Cpre class=\"gpr1\" style=\"border: 2px groove; margin: 20px; overflow: auto; padding: 6px;\"\u003E$ cat F2\nAIX\nSCO\n\u003C\/pre\u003E\n\u003Cb\u003E10. \u0026nbsp;Split the file at every 3rd line, retaining the header and trailer in every file.\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cpre class=\"gpr1\" style=\"border: 2px groove; margin: 20px; overflow: auto; padding: 6px;\"\u003E$ awk 'BEGIN{getline f;}NR%3==2{x=\"F\"++i;a[i]=x;print f\u0026gt;x;}{print \u0026gt; x}END{for(j=1;j\u0026lt;i;j++)print\u0026gt; a[j];}\u003Ci;j++)print\u003E' file4\n\u003C\/i;j++)print\u003E\u003C\/pre\u003E\n\u003Cdiv style=\"text-align: justify;\"\u003E\n\u0026nbsp; \u0026nbsp; \u0026nbsp;This one is little tricky. Before the file is processed, the first line is read using \u003Ci\u003Egetline\u003C\/i\u003E\u0026nbsp;into the variable f. NR%3 is checked with 2 instead of 1 as in the earlier case because since the first line is a header, we need to split the files at 2nd, 5th, 8th lines, and so on. All the file names are stored in the array \"a\" for later processing.\u003C\/div\u003E\n\u003Cdiv style=\"text-align: justify;\"\u003E\n\u0026nbsp; \u0026nbsp; Without the END label, all the files will have the header record, but only the last file will have the trailer record. So, the END label is to precisely write the trailer record to all the files other than the last file.\u003C\/div\u003E\n\u003Cpre class=\"gpr1\" style=\"border: 2px groove; margin: 20px; overflow: auto; padding: 6px;\"\u003E$ cat F1\nHEADER\nUnix\nLinux\nSolaris\nTRAILER\u003C\/pre\u003E\n\u003Cpre class=\"gpr1\" style=\"border: 2px groove; margin: 20px; overflow: auto; padding: 6px;\"\u003E$ cat F2\nHEADER\nAix\nSCO\nTRAILER\n\u003C\/pre\u003E"},"link":[{"rel":"replies","type":"application/atom+xml","href":"https:\/\/www.theunixschool.com\/feeds\/8338870947509621309\/comments\/default","title":"Post Comments"},{"rel":"replies","type":"text/html","href":"https:\/\/www.theunixschool.com\/2012\/06\/awk-10-examples-to-split-file-into.html#comment-form","title":"18 Comments"},{"rel":"edit","type":"application/atom+xml","href":"https:\/\/www.blogger.com\/feeds\/1255024703457423340\/posts\/default\/8338870947509621309"},{"rel":"self","type":"application/atom+xml","href":"https:\/\/www.blogger.com\/feeds\/1255024703457423340\/posts\/default\/8338870947509621309"},{"rel":"alternate","type":"text/html","href":"https:\/\/www.theunixschool.com\/2012\/06\/awk-10-examples-to-split-file-into.html","title":"awk - 10 examples to split a file into multiple files"}],"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":"18"}}]}});