0

I have one large file that looks like:

Header1

Subheader1

l1 

l1'

l1''

Header2

Subheader2

l2 

l2'

l2''

Headern

Subheader n

ln

ln'

ln''

(in which n is ~25K)

I need to paste l1-l1'', l2-l2'' ... ln-ln'' into different files - say file1, file2, ... file n (at a certain line number within the files).

1

2 Answers 2

0

You don't specify the relation between Headers and lines. But if the relation is a constant number of lines, you can retrieve the lines numbers with grep then use sed to extract the lines.

For example, to get all lines after Subheader :

#!/bin/bash 

file_in=/tmp/test.txt
file_out=/tmp/file1.txt
LINES=`grep -n Subheader $file_in | cut -f1 -d:`
for line in $LINES; do
  echo $line
  line1=$(($line+1))
  sed -n ${line1}p $file_in >> $file_out
done

Edit : you can run the script above into a loop :

#!/bin/bash

n_max=100
file_in=/tmp/test.txt
file_out=/tmp/file

LINES=`grep -n Subheader $file_in | cut -f1 -d:`
for n in `seq 1 $n_max`; do
  echo "line $n"
  for line in $LINES; do
    line_n=$(($line+$n))
    sed -n ${line_n}p $file_in >> $file_out$n.txt
  done
done
exit 0
0

We can do this as follows:

perl -Mautodie -lne '
   if ( /Subheader/ ... /Header/ ) {
      open FILE, q[>>], q[file] . ++$a if /Subheader/;
      close FILE if /Header/;
      print FILE if !/Subheader/ && !/Header/;
   }
' large_file.log

This will create the mini-files file1, file2, etc.

Perl opens a new filehandle when it encounters a Subheader line. And it close that filehandle when it encounters the Header line. for all other lines, it prints to the filehandle.

You must log in to answer this question.

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