2

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?

1

3 Answers 3

1

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
s/\\/&&/g
2c\
1i\\
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

1

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .