I am trying to separate a large file with 4100 rows. Separating 6 rows of numbers as file and other lines need to follow as 4 rows of number as file.Please consider the below.

How can I write a bash script?


You could try with awk:

awk '{if (NR>6){if ((NR-6)%4==1){++c}} else {c=1}};{print >"file"c".las"}' infile

On a gnu setup and with a relatively recent version split which allows you to add a suffix you could also do something like:

{ printf %s\\n\\n; cat infile; } | split -d -l 4 --additional-suffix='.las' - file
( set ./*.las
sed '1d
3s/$/\\/' "$1" | sed -f - -i "$2"
rm -f "$1" )

that is, print two empty lines and then the file, splitting the combined output into pieces of four lines, inserting the first two lines from the original input via sed (after escaping backslashes - if any) into the second piece and finally removing the first piece. Though keep in mind this will zero-pad the chunk names so you'll end up with file0001.las etc


The following command will split as described presuming that the file is to be split every four lines. The output files will be named e. g. output00, output01, and so forth. You can then rename all output* files to tack the .las suffix to the end and, if desired, strip the leading zeroes.

split -l4 -d ./input_file output
  • Thanks for comments. I need to split first 6 lines and then every 5 lines. – shi Mar 14 '17 at 15:49
  • head -n1 input_file > output-01; tail -n+2 input_file | split -l5 -d output – DopeGhoti Mar 14 '17 at 16:13
  • error: split: cannot open ‘output’ for reading: No such file or directory – shi Mar 15 '17 at 21:21
csplit -s -k -f file. inoutfile '/^/+6' '/^/+4' '{*}'
  • this format does not work well. – shi Mar 14 '17 at 15:49
  • error: csplit: ‘%^3%’: match not found – shi Mar 15 '17 at 21:24
  • I have placed a new command. – user218374 Mar 16 '17 at 10:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.