deleting particular lines and moving a line up using perl/sed

Hi,

I need convert a dump file in the following format : (please note that line numbers are provided for easy look)

Original file:

 1  2007-10-2482.90 No trade 0 0.00 100000.00
 2  100000.00
 3  0.00
 4  HOLD
 5  2007-10-2589.75 Bought 1114 1114 100000.00 0.00
 6  100000.00
 7  -0.00
 8  STRONG  BUY
 9  2007-10-2692.35 No trade 1114 102896.94 0.00
10  102896.94
11  2.90
12  STRONG  BUY
13  2007-10-2991.20 No trade 1114 101615.60 0.00
14  101615.60
15  1.62
16  STRONG  BUY
17  2007-10-3090.05 No trade 1114 100334.26 0.00
18  100334.26
19  0.33
20  STRONG  BUY
21  2007-10-3188.45 No trade 1114 98551.53 0.00
22  98551.53
23  -1.45
24  STRONG  BUY
25  2007-11-0183.70 Sold 1114 0 0.00 93259.05
26  93259.05
27  -6.74
28  STRONG  SELL

Modifiled file :

1 2007-10-2482.90 No trade 0 0.00 100000.00 HOLD
2 2007-10-2589.75 Bought 1114 1114 100000.00 0.00 STRONG BUY
3 2007-10-2692.35 No trade 1114 102896.94 0.00 STRONG BUY
4 2007-10-2991.20 No trade 1114 101615.60 0.00 STRONG BUY
5 2007-10-3090.05 No trade 1114 100334.26 0.00 STRONG BUY
6 2007-10-3188.45 No trade 1114 98551.53 0.00 STRONG BUY
7 2007-11-0183.70 Sold 1114 0 0.00 93259.05 STRONG SELL

That is, every 4 line of the input file needs to converted into a single line - after deleting 2nd and 3rd line.

A perl script to do this would be great. (sed will also fulfill my purpose but perl way will be highly appreciated).

Awaiting for your help! :slight_smile:

awk '{ printf "%s", $0; getline;  getline; getline var; printf " %s\n", var }' filename

great! worked - just wondering if this can be donw in perl ...

yes it can be done ! :slight_smile:

#! /opt/third-party/bin/perl

my $skip = 2;

open(FILE, "<", "a2");

while(<FILE>) {
  chomp;
  if ( $skip < -1 ) {
    $skip = 1;
    print $_;
    next;
  }
  print $_ if( $skip == 2 );
  print "$_\n" if( $skip == -1 );
  $skip--;
}

close(FILE);

hi,

This one should be ok.

code:

awk '{
if (NR%4==1)
	str=$0
if (NR%4==0)
	print NR/4" "str" "$0
}' file

paste - - - - <x