Adding header once every 5 lines

Hi,

I need a help in creating a report file.
The input file is like this

1 A
2 B
3 V
4 X
5 m
6 O
7 X
8 p
9 a
10 X

There is a header which i have to print & save the result as a output file.
The header has multiple lines on is like say:
New New
S.No Name
(XX) (Y)
But i want the report file to be

New New
S.No Name
(XX) (Y)
1 A
2 B
3 V
4 X
5 m

---page break or a blank line----
New New
S.No Name
(XX) (Y)
6 O
7 X
8 p
9 a
10 X

Can this be possible using a SHELL SCRIPT?
I tried AWK & SED. but it helps inserting a single header, but i could find option to do this.
any pointers on this will be of great assistance.

Thanks

Something like this?

awk -v str="\nNew\tNew\nS.No\tName\n(XX)\t(Y)\n" 'BEGIN{printf str} {if(NR%5==0){print $1"\t"$2;i=1}else{printf i==1?str$1"\t"$2"\n":$1"\t"$2"\n";i=0}}' infile
1 Like

pravin27

But i get this error when doing so

The error context is
BEGIN{printf str} {if(NR%5==0){print $1"\t"$2;i=1}else{printf >>> i== <<<
awk: 0602-502 The statement cannot be correctly parsed. The source line is 1.

can yu plz help?

I think you have missed something. Please see below my o/p

# cat format
1 A
2 B
3 V
4 X
5 m
6 O
7 X
8 p
9 a
10 X
#awk -v str="\nNew\tNew\nS.No\tName\n(XX)\t(Y)\n" 'BEGIN{printf str} {if(NR%5==0){print $1"\t"$2;i=1}else{printf i==1?str$1"\t"$2"\n":$1"\t"$2"\n";i=0}}' format

O/P

New     New
S.No    Name
(XX)    (Y)
1       A
2       B
3       V
4       X
5       m

New     New
S.No    Name
(XX)    (Y)
6       O
7       X
8       p
9       a
10      X

through sed..

sed '1~5s/.*/\nNew New\nS.No Name\n(XX) (Y)\n&/' inputfile > outfile

Hi michaelrozar17,
Cluld you please explain me the SED code ?

Pravin27,

Thanks your awk works fine on LINUX box.
But i pity myself, the same thing doesnt work on AIX.... :frowning: :frowning:
It throws this error

Syntax Error The source line is 1.
The error context is
BEGIN{printf str} {if(NR%5==0){print $1"\t"$2;i=1}else{printf >>> i== <<<
awk: 0602-502 The statement cannot be correctly parsed. The source line is 1.

when i execute

 awk -v str="\nNew\tNew\nS.No\tName\n(XX)\t(Y)\n" 'BEGIN{printf str} {if(NR%5==0){print $1"\t"$2;i=1}else{printf i==1?str$1"\t"$2"\n":$1"\t"$2"\n";i=0}}' file1

nt sure why it fails on AIX :frowning: :frowning:

The only trick here is with the operator ~. 1~5, it means match every 5th line starting from line 1.Similarly 0~3 - matches every 3rd line starting from line 0. Rest of the thing i guess you are familiar..

1 Like

Note: ~ is GNU sed only.

Some other seds:

sed 's/^/\nNew New\nS.No Name\n(XX) (Y)\n&/;n;n;n;n' infile

---------- Post updated at 14:43 ---------- Previous update was at 14:41 ----------

awk '!(NR%5-1){print h}1' h="
New New
S.No Name
(XX) (Y)" infile

@Scrutinizer,
thanks for your inputs.
but the sed is not able to take the \n charecters properly...

nNew NewnS.No Namen(XX) (Y)nn1  A
2  B

Can yu help with some othr option n sed?
Thanks,

Just replace i==1?str$1
with (i==1)?str$1

1 Like

Thanks a ton ChublerXL
Your idea worked like magic..

\n in the replacement text is also a non-portable (non-posix) extension ;). A backslash followed by a newline is the standardized method of including a literal newline in that section of a substitution command (\n is allowed in the regular expression).

Regards,
Alister

All,
I tried one option today.

cat sam.awk
  BEGIN{ printf "TEST" }
  {
   for(i=0;i<5;i++)
   { REPNUM=i; printf "REP: %d\n", REPNUM;
     LINE1="TEST THE PRINT value of REP: $REPNUM"
      printf " %s\n", ENVIRON["LINE1"]
   }
   }

I executed

# nawk -f sam.awk

& the output is :

TEST
REP: 0
REP: 1
REP: 2
REP: 3
REP: 4

But I am not able to print the LINE1...
Not getting a clue y??? :frowning:
Can you please help me?

Works fine for me:

$ LINE1="Testing this script" ; export LINE1
$  echo | nawk -f sam.awk
TESTREP: 0
 Testing this script
REP: 1
 Testing this script
REP: 2
 Testing this script
REP: 3
 Testing this script
REP: 4
 Testing this script

You're right :wink: that is why I wrote "some other seds"