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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting deleting particular lines and moving a line up using perl/sed
# 1  
Old 11-22-2007
Java 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! Smilie
# 2  
Old 11-22-2007
Code:
awk '{ printf "%s", $0; getline;  getline; getline var; printf " %s\n", var }' filename

# 3  
Old 11-22-2007
great! worked - just wondering if this can be donw in perl ...
# 4  
Old 11-22-2007
Quote:
Originally Posted by sabyasm
great! worked - just wondering if this can be donw in perl ...
yes it can be done ! Smilie

Code:
#! /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);

# 5  
Old 11-22-2007
awk

hi,

This one should be ok.

code:

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

# 6  
Old 12-12-2007
paste - - - - <x
btech_raju
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Sed: how to merge two lines moving matched pattern to end of previous line

hello everyone, im new here, and also programming with awk, sed and grep commands on linux. In my text i have many lines with this config: 1 1 4 3 1 1 2 5 2 2 1 1 1 3 1 2 1 3 1 1 1 2 2 2 5 2 4 1 3 2 1 1 4 1 2 1 1 1 3 2 1 1 5 4 1 3 1 1... (3 Replies)
Discussion started by: satir
3 Replies

2. Shell Programming and Scripting

Replacing lines matching a multi-line pattern (sed/perl/awk)

Dear Unix Forums, I am hoping you can help me with a pattern matching problem. What am I trying to do? I want to replace multiple lines of a text file (that match a multi-line pattern) with a single line of text. These patterns can span several lines and do not always have the same number of... (10 Replies)
Discussion started by: thefang
10 Replies

3. UNIX for Dummies Questions & Answers

Moving lines of file to command line

I have a file in which each line is the name of another file. Is there a way to serve them to the command line? For example, if the file contains file1.txt file2.txt file3.txt ... file9.txt is there a way to insert them in the command as a batch? If I ran a command like grep... (4 Replies)
Discussion started by: wbport
4 Replies

4. UNIX for Dummies Questions & Answers

Deleting last 3 lines from a file via sed

Hi Anybody can help me to delete the last 3 lines from a text file via sed under SunOS 5.8? Thanks Aldar (4 Replies)
Discussion started by: aldar
4 Replies

5. Shell Programming and Scripting

sed: deleting 5 lines after a specified pattern

As an example (just an example, this could apply to any block of text) say I have this: architecture x86_64 cputype CPU_TYPE_X86_64 cpusubtype CPU_SUBTYPE_X86_64_ALL offset 4096 size 2972420 align 2^12 (4096) architecture ppc64 cputype CPU_TYPE_POWERPC64 cpusubtype... (3 Replies)
Discussion started by: pcwiz
3 Replies

6. Shell Programming and Scripting

Deleting lines using Sed

Hi All, Please can anyone help me as am deleting a line in a file with the below script: sed '/"$value"/d' redirects.virgin-atlantic.com.conf > olist where $value is a variable where the pattern to be matched is stored. I am not getting any error also but the line containing the pattern... (2 Replies)
Discussion started by: Shazin
2 Replies

7. Shell Programming and Scripting

Help deleting lines with SED.

I take the /etc/passwd file and print it out, but I only want the lines that end with sh. I have cat /etc/passwd | sed '/sh/!d' Which prints out all lines that have sh somewhere in it. So I added $, which I thought matches the ends on lines, but its not working, like for example I have have... (5 Replies)
Discussion started by: Bandit390
5 Replies

8. Shell Programming and Scripting

deleting lines after pattern using sed

I have seen there are many sed posts but still it is quite difficult to apply other post to my own problem How can I delete all lines in a file from 2 lines after this pattern *End_fine_coreg:_NORMAL to the end of file? Cheers (2 Replies)
Discussion started by: larne
2 Replies

9. Shell Programming and Scripting

Deleting lines above a certain line

Hi, I have a file that gets automatically generated and it would look something like sakjsd adssad {{word}} sddsasd dsdsasa . . . So basically what I want to do is just keep the stuff below the {{word}} marker. The marker includes the brackets. Is there any command to delete the... (3 Replies)
Discussion started by: eltinator
3 Replies

10. Shell Programming and Scripting

Deleting Multiple Lines with sed

I am trying to use sed to delete multiple lines in a file. The problem is that I need to search for a certain line and then once found delete it plus the next 4 lines. For instance if I had a file that consisted of the following lines: #Data1.start ( (Database= data1) (Name = IPC)... (1 Reply)
Discussion started by: rambo15
1 Replies
Login or Register to Ask a Question