Remove a block of Text at regular intervals


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Remove a block of Text at regular intervals
# 15  
Old 11-03-2010
Question

Quote:
Originally Posted by Scrutinizer
It does exactly that when I use m=3. This should work on your platform, does it not?
I don't know if I am doing something wrong, but it doesn't work on my end.

Any suggestions to try and log what is going wrong?

---------- Post updated at 07:57 AM ---------- Previous update was at 07:54 AM ----------

Would it be possible to modify any of the suggestions so far such that frames 2,3,5,6,8,9,11,12, etc would be removed and the output file would have only frames 1,4,7,10,13,16,19,...etc?

This would remove 2 frames at a time instead of one.

Many many thanks to all of you for your help with this.Smilie
# 16  
Old 11-03-2010
Quote:
Originally Posted by marcozd
...
Would it be possible to modify any of the suggestions so far such that frames 2,3,5,6,8,9,11,12, etc would be removed and the output file would have only frames 1,4,7,10,13,16,19,...etc?
...
You could pass a comma-delimited list of frame numbers that should not be displayed. Frame numbers not in the list would be displayed by default.

Code:
$
$
$ cat -n frames1.pl
     1  #!perl -w
     2  print "Enter number of atoms: ";
     3  chomp($x = <STDIN>);
     4  print "Enter comma-delimited list of frame nos. that should be removed: ";
     5  chomp($y = <STDIN>);
     6  @noshow = split(",", $y);                         # store non-displaying frames in array @noshow
     7  $file = "frames";
     8  open (F, "frames") or die "Can't open $file: $!";
     9  while (<F>) {                                     # iterate through the records
    10    chomp;                                          # chomp newline
    11    if (/^\d+$/) {                                  # if the number is here all by itself
    12      $atoms = $_;                                  # then it is the number of atoms
    13      $iter = 1;                                    # initialize the iterator
    14      $show = 0;                                    # don't show/print the records just yet
    15    } elsif (/^Frame (\d+)$/) {                     # if this line has the Frame number
    16      if (grep(/^$1$/,@noshow) == 0) {              # that should be displayed
    17        print $atoms,"\n";                          # then print the number of atoms
    18        print $_,"\n";                              # and print the current line
    19        $show = 1;                                  # and set the flag so that the block could be printed
    20      }
    21    } elsif ($show and $iter <= $x) {               # if flag is set and we haven't reached the upper limit
    22      print $_,"\n";                                # then keep printing the record
    23      $iter++;                                      # and incrementing the iterator
    24    }
    25  }
    26  close (F) or die "Can't close $file: $!";         # clean up after ourselves
$
$ perl frames1.pl
Enter number of atoms: 14
Enter comma-delimited list of frame nos. that should be removed: 2,3,5
14
Frame 1
Ir 0.4482 -1.2980 -0.2902
P 1.8759 -2.1654 1.4038
P -1.2305 -0.8418 -1.9134
H -2.5605 -0.7775 -1.4067
H -1.3820 -1.8515 -2.9058
H -1.1987 0.3321 -2.7223
H 2.5359 -3.3920 1.1065
H 1.2161 -2.5072 2.6182
H 2.9669 -1.3899 1.8960
C 1.3685 0.2571 -0.5341
O 1.9671 1.2795 -0.7004
Cl -0.8142 -3.4101 0.0318
H -0.8380 -0.5636 2.1141
H -1.0869 -0.4380 2.8141
14
Frame 4
Ir 0.1111 -1.1111 -0.1111
P 1.2222 -2.2222 1.2222
P -1.3333 -0.3333 -1.3333
H -2.4444 0.4444 -1.4444
H -2.5555 -1.5555 -1.5555
H -1.6666 -0.6666 -2.6666
H 2.7777 -2.7777 1.7777
H 0.8888 -3.8888 2.8888
H 1.9999 -1.9999 2.9999
C 1.1010 -0.1010 -1.1010
O 2.1111 -0.1111 -1.1111
Cl 0.1212 -3.1212 -0.1212
H 0.1313 0.1313 0.1313
H -0.1414 -1.1414 0.1414
14
Frame 6
Ir 0.2929 -1.2929 -0.2929
P 1.3030 -2.3030 1.3030
P -1.3131 -0.3131 -1.3131
H -2.3232 0.3232 -1.3232
H -2.3333 -1.3333 -1.3333
H -1.3434 -0.3434 -2.3434
H 2.3535 -2.3535 1.3535
H 0.3636 -3.3636 2.3636
H 1.3737 -1.3737 2.3737
C 1.3838 -0.3838 -1.3838
O 2.3939 -0.3939 -1.3939
Cl 0.4040 -3.4040 -0.4040
H 0.4141 0.4141 0.4141
H -0.4242 -1.4242 0.4242
$
$

tyler_durden

---------- Post updated at 11:46 AM ---------- Previous update was at 11:13 AM ----------

In the script posted earlier, it might be a tad cumbersome to input a comma-delimited list of all frame numbers that should not be displayed.

If "S" means "Show" and "NS" means "(Do) Not Show", then you could input the smallest non-repeating display pattern to the script. And the script can keep on implementing the pattern for all the frames.

For example, if the input pattern is "S,NS,NS", then it means:

Code:
(1)  Show block 1.
(2)  Do not show block 2.
(3)  Do not show block 3.
(4)  Repeat the above pattern (i.e. "S,NS,NS") for each block of 3 frames that follows.

You could come up with more complex patterns for varying block counts.
For example, an input pattern of "S,NS,NS,NS,S" means -

Code:
(1)  Show block 1.
(2)  Do not show block 2.
(3)  Do not show block 3.
(4)  Do not show block 4.
(5)  Show block 5.
------
(6)  Show block 6.
(7)  Do not show block 7.
(8)  Do not show block 8.
(9)  Do not show block 9.
(10)  Show block 10.
------
and so on...

The boundary condition - "S" means "show all blocks" and "NS" means "do not show any block".

Here's the code -

Code:
$
$
$ cat -n frames2.pl
     1  #!perl -w
     2  print "Enter number of atoms: ";
     3  chomp($x = <STDIN>);
     4  print "Enter comma-delimited display pattern: ";
     5  chomp($y = <STDIN>);
     6  @action = split(",", $y);                         # store the "show/noshow" action in an array
     7  @acopy  = @action;                                # copy the array because we'll modify the original
     8  $file = "frames";
     9  open (F, "frames") or die "Can't open $file: $!";
    10  while (<F>) {                                     # iterate through the records
    11    chomp;                                          # chomp newline
    12    if (/^\d+$/) {                                  # if the number is here all by itself
    13      $atoms = $_;                                  # then it is the number of atoms
    14      $iter = 1;                                    # initialize the iterator
    15      $show = 0;                                    # don't show/print the records just yet
    16    } elsif (/^Frame (\d+)$/) {                     # if this line has the Frame number
    17      $act = shift @action;                         # then pick up an element from array @action
    18      if ($act eq "S") {                            # if this action is "Show"
    19        print $atoms,"\n";                          # then print the number of atoms
    20        print $_,"\n";                              # and print the current line
    21        $show = 1;                                  # and set the flag so that the block could be printed
    22      }
    23      if ($#action == -1) {                         # if the original array @action is empty
    24        @action = @acopy;                           # then replenish it
    25      }
    26    } elsif ($show and $iter <= $x) {               # if flag is set and we haven't reached the upper limit
    27      print $_,"\n";                                # then keep printing the record
    28      $iter++;                                      # and incrementing the iterator
    29    }
    30  }
    31  close (F) or die "Can't close $file: $!";         # clean up after ourselves
$
$
$ perl frames2.pl
Enter number of atoms: 14
Enter comma-delimited display pattern: S,NS,NS
14
Frame 1
Ir 0.4482 -1.2980 -0.2902
P 1.8759 -2.1654 1.4038
P -1.2305 -0.8418 -1.9134
H -2.5605 -0.7775 -1.4067
H -1.3820 -1.8515 -2.9058
H -1.1987 0.3321 -2.7223
H 2.5359 -3.3920 1.1065
H 1.2161 -2.5072 2.6182
H 2.9669 -1.3899 1.8960
C 1.3685 0.2571 -0.5341
O 1.9671 1.2795 -0.7004
Cl -0.8142 -3.4101 0.0318
H -0.8380 -0.5636 2.1141
H -1.0869 -0.4380 2.8141
14
Frame 4
Ir 0.1111 -1.1111 -0.1111
P 1.2222 -2.2222 1.2222
P -1.3333 -0.3333 -1.3333
H -2.4444 0.4444 -1.4444
H -2.5555 -1.5555 -1.5555
H -1.6666 -0.6666 -2.6666
H 2.7777 -2.7777 1.7777
H 0.8888 -3.8888 2.8888
H 1.9999 -1.9999 2.9999
C 1.1010 -0.1010 -1.1010
O 2.1111 -0.1111 -1.1111
Cl 0.1212 -3.1212 -0.1212
H 0.1313 0.1313 0.1313
H -0.1414 -1.1414 0.1414
$
$
$ perl frames2.pl
Enter number of atoms: 14
Enter comma-delimited display pattern: S,NS
14
Frame 1
Ir 0.4482 -1.2980 -0.2902
P 1.8759 -2.1654 1.4038
P -1.2305 -0.8418 -1.9134
H -2.5605 -0.7775 -1.4067
H -1.3820 -1.8515 -2.9058
H -1.1987 0.3321 -2.7223
H 2.5359 -3.3920 1.1065
H 1.2161 -2.5072 2.6182
H 2.9669 -1.3899 1.8960
C 1.3685 0.2571 -0.5341
O 1.9671 1.2795 -0.7004
Cl -0.8142 -3.4101 0.0318
H -0.8380 -0.5636 2.1141
H -1.0869 -0.4380 2.8141
14
Frame 3
Ir 0.2799 -1.1423 -0.0744
P 1.5830 -2.0634 1.6851
P -1.4956 -0.6824 -1.5757
H -2.5240 0.2145 -1.1716
H -2.2321 -1.8577 -1.8841
H -1.2106 -0.1808 -2.8791
H 2.8807 -2.5381 1.3446
H 0.9971 -3.2308 2.2364
H 1.8734 -1.3013 2.8541
C 1.7346 -0.5673 -1.1992
O 2.6174 -0.1436 -1.8621
Cl 0.2051 -3.5805 -0.9057
H 0.1695 0.2982 0.6158
H -0.9713 -1.5095 0.9427
14
Frame 5
Ir 0.1515 -1.1515 -0.1515
P 1.1616 -2.1616 1.1616
P -1.1717 -0.1717 -1.1717
H -2.1818 0.1818 -1.1818
H -2.1919 -1.1919 -1.1919
H -1.2020 -0.2020 -2.2020
H 2.2121 -2.2121 1.2121
H 0.2222 -3.2222 2.2222
H 1.2323 -1.2323 2.2323
C 1.2424 -0.2424 -1.2424
O 2.2525 -0.2525 -1.2525
Cl 0.2626 -3.2626 -0.2626
H 0.2727 0.2727 0.2727
H -0.2828 -1.2828 0.2828
$
$
$ perl frames2.pl
Enter number of atoms: 14
Enter comma-delimited display pattern: NS
$
$
$
$

HTH,
tyler_durden
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Bulk load testing in regular intervals

I need to write a script which can send files via sftp communication continously for half an hour or any given duration of time. I have already written a batch file to send multiple file via SFTP. but I need to know how can we set a duration of half an hour through shell script. Can we use sleep... (2 Replies)
Discussion started by: talk1234
2 Replies

2. Shell Programming and Scripting

perl regular expression to remove the special characters

I had a string in perl script as below. Tue Augáá7 03:54:12 2012 Now I need to replace the special character with space. After removing the special chaacters Tue Aug 7 03:54:12 2012 Could anyone please help me here for writing the regular expression? Thanks in advance.. Regards, GS (1 Reply)
Discussion started by: giridhar276
1 Replies

3. Programming

Selecting files in regular intervals from a folder

Hi, I need your expertise in selecting files from a folder. I have files named with convention: filename.i.j where j is an interger from 1 to 16, for each i which is an integer from 1 to 2000. I would like to select the files with i in regular interval of 50 like filename.1.j,... (2 Replies)
Discussion started by: rpd25
2 Replies

4. Shell Programming and Scripting

Filter or remove duplicate block of text without distinguishing marks or fields

Hello, Although I have found similar questions, I could not find advice that could help with our problem. The issue: We have several hundreds text files containing repeated blocks of text (I guess back at the time they were prepared like that to optmize printing). The block of texts... (13 Replies)
Discussion started by: samask
13 Replies

5. UNIX for Advanced & Expert Users

awk - remove block of text, multiple actions for 'if', inline edit

I'm having a couple of issues. I'm trying to edit a nagios config and remove a host definition if a certain "host_name" is found. My thought is I would find host definition block containing the host_name I'm looking for and output the line numbers for the first and last lines. Using set, I will... (9 Replies)
Discussion started by: mglenney
9 Replies

6. Shell Programming and Scripting

Need perl regular expression to remove the comment

I need a perl substitution to remove only the comment in the line . That line may have '#' with in double quotes .I used the following , s/(^.*\".+?#.+?\".+?)(#.*)/$1/g It works for , print " not a comment # not a comment " . "not a comment # not a comment" ; # It is a comment ... (3 Replies)
Discussion started by: karthigayan
3 Replies

7. Shell Programming and Scripting

Grouping data numbers in a text file into prescribed intervals and count

I have a text file that contains numbers (listed from the smallest to the largest). For ex. 34 817 1145 1645 1759 1761 3368 3529 4311 4681 5187 5193 5199 5417 5682 . . (5 Replies)
Discussion started by: Lucky Ali
5 Replies

8. Shell Programming and Scripting

Pls Help me out ... I want to check process status at regular intervals of time

I want to check process status at regular interval of time ... so i ha wirtten this BUT its not working when i placed this peace of code in .sh .. please help me out #!/bin/sh w = ps -ef|grep processname | wc - l echo $w if ; then Banner "Proceesname Problem" else Banner " Running... (5 Replies)
Discussion started by: srinivasvandana
5 Replies

9. Programming

performing a task at regular intervals

hi! i m tryin to write a program that will perform a specific tasks after fixed interval of time.say every 1 min. i jus donno how to go abt it.. which functions to use and so on... i wud like to add that i am dont want to use crontab over here. ny lead is appreciated. thanx. (2 Replies)
Discussion started by: mridula
2 Replies

10. Shell Programming and Scripting

mailing myself at regular intervals...

hi all, i wrote a script to mail myself using pine (modified) to keep remind of b'days. #!/bin/bash grep "`date +%D |awk -F/ '{print $2+1, $1+0}'`" dataFile >/home/username/mailme if test -s /home/username/mailme then pine -I '^X,y' -subject "Birthday Remainder" username... (4 Replies)
Discussion started by: timepassman
4 Replies
Login or Register to Ask a Question