Clubbing two files after every 12 lines in hp-ux system

1

I have two large files .one file contains 0-12 Hrs of data and other contain 13-23 hours of data . I want to merge it in a single file with 23-0 hours for each combination.

ex :
file1

abcdefg00
abcdefg01
abcdefg02
---------
---------
abcdefg12
pqrstuv00
---------
---------

file2 :

abcdefg13
abcdefg14
---------
---------
abcdefg23
pqrstuv13
---------
---------

Is there any way that i can merge like this . output should be like below

>     abcdefg00
>     abcdefg01
>     abcdefg02
>     ---------
>     ---------
>     abcdefg12
>     abcdefg13
>     abcdefg14
>     ---------
>     ---------
>     abcdefg23
>     pqrstuv00
>     ---------
>     ---------
>     pqrstuv13
>     ---------
>     ---------
>     pqrstuv23

Thanks in adavance

kiran

Posted 2016-03-11T16:20:07.413

Reputation: 11

Hi kiran, welcome on SuperUser. Register yourself and check if the answers work in your case. If not add some comment or [edit] your post. – Hastur – 2016-03-11T18:01:12.877

Answers

0

If you are asking to append the two files, one after the other, but to also reverse the order of the lines, you can use either tail -r or tac as in

cat file1 file2 | tail -r

or

cat file1 file2 | tac

The -r option may not be available on HP-UX tail. The command tac is part of coreutils if you need to add it.

Ira

Posted 2016-03-11T16:20:07.413

Reputation: 1

0

You can try to use sort

sort -n -k1 f1.txt f2.txt   > newfile

from man sort you can read that sort

Writes sorted concatenation of all FILE(s) to standard output.

You may need to select the column used to sort (-k1) or to select a numbering sort -n.

If your files are not strictly ordered you should do a script that reads 12 lines alternatively from the first and the second file with two file descriptors [1],[2].

It can result in something similar to this one

#!/bin/bash
while true
do
  for ((i=1;i<=12;i++)); do
    read -r f1 <&3 && echo "$f1" || exit 1
  done
  for ((i=1;i<=12;i++)); do
    read -r f2 <&4 && echo "$f2" || exit 2
  done
done 3<file1 4<file2

Until is able to read it writes else it exit with different error value if from the 1st or the 2nd cycle.

Hastur

Posted 2016-03-11T16:20:07.413

Reputation: 15 043