Script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Script
# 1  
Old 08-17-2012
Script

I need a perl script which will print the remaining part of the file once it see a particular pattern.here the pattern i need to search is HOLD

Code:
#!/usr/bin/perl
if($#ARGV+1==2)
{
open(FH1,"<$ARGV[0]")||die("File not found");
open(FH2,">$ARGV[1]");
my $i=0;


while($x=<FH1>)
{

if ($x =~ /HOLD/ ) {
    print "Is hold\n";

 while($y=<FH1>)
   {
   print  FH2"$x;\n"
   }

 }
}
}


close (FH1);
close (FH2);




but the file pointer is not incrementing to the next line so my output is not proper

input file is
Code:
,,26,OCC_CLK_USB3_COCCLKA_3,0.925,0.000,0,1
,,27,OCC_CLK_USB3_COCLKA_DIV2_1,2.938,0.000,0,2
,,28,OCC_CLK_USB3_COCLKA_DIV4_2,6.936,0.000,0,2
 ,,HOLD, Clock Group, WNS, TNS, No. Of Max Violating Paths,Logic level
,,1,async_default,0.080,0.000,0,2
,,2,clock_gating_default,0.021,0.000,0,5
,,3,ATE_CLK,-0.076,-0.076,1,2

expected outout
Code:
,,1,async_default,0.080,0.000,0,2
,,2,clock_gating_default,0.021,0.000,0,5
,,3,ATE_CLK,-0.076,-0.076,1,2


wrong output
Code:
,,HOLD, Clock Group, WNS, TNS, No. Of Max Violating Paths,Logic level
;
 ,,HOLD, Clock Group, WNS, TNS, No. Of Max Violating Paths,Logic level
;
 ,,HOLD, Clock Group, WNS, TNS, No. Of Max Violating Paths,Logic level

Please help me correct this

Last edited by Scott; 08-17-2012 at 02:58 AM.. Reason: Code tags
# 2  
Old 08-17-2012
Would'nt this suffice???
Code:
sed '1,/HOLD/d' file

This User Gave Thanks to balajesuri For This Post:
# 3  
Old 08-17-2012
hi bala,

please let me know which part of the script i should change...can u pls change it ..
# 4  
Old 08-17-2012
Change this:
Code:
while($y=<FH1>)
{
    print FH2"$x;\n"
}

to this:
Code:
while($y=<FH1>)
{
    print FH2"$y;\n"
}

This User Gave Thanks to Chubler_XL For This Post:
# 5  
Old 08-17-2012
hi chubler,
Thanks...I made a silly mistake...Smilie
# 6  
Old 08-17-2012
1. Once you open a file handle and start reading from it, there's no going back (unless you're programming in C or using some file handling modules of perl which I'm unaware of).
2. In your program, the problem is that FH1 is read again in a nested while loop, where the parent loop is itself reading from FH1.

Here's how I would do it, if I had to write one in 2 mins:
Code:
#! /usr/bin/perl -w
use strict;

open IN, "< $ARGV[0]";
open OUT, "> $ARGV[1]";

my $f = 0;

for (<IN>) {
    if (!/HOLD/ && $f == 0) {
        next;
    }
    elsif (/HOLD/ && $f == 0) {
        $f = 1;
    }
    else {
        print OUT;
    }
}

close OUT;
close IN;

Hope this helps.
# 7  
Old 08-17-2012
Quote:
Originally Posted by balajesuri
Would'nt this suffice???
Code:
sed '1,/HOLD/d' file

this too works thanks
Login or Register to Ask a Question

Previous Thread | Next Thread

5 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to block first bash script until second bash script script launches web server/site?

I'm new to utilities like socat and netcat and I'm not clear if they will do what I need. I have a "compileDeployStartWebServer.sh" script and a "StartBrowser.sh" script that are started by emacs/elisp at the same time in two different processes. I'm using Cygwin bash on Windows 10. My... (3 Replies)
Discussion started by: siegfried
3 Replies

2. Shell Programming and Scripting

Shell script works fine as a standalone script but not as part of a bigger script

Hello all, I am facing a weird issue while executing a code below - #!/bin/bash cd /wload/baot/home/baotasa0/sandboxes_finance/ext_ukba_bde/pset sh UKBA_publish.sh UKBA 28082015 3 if then echo "Param file conversion for all the areas are completed, please check in your home directory"... (2 Replies)
Discussion started by: ektubbe
2 Replies

3. UNIX for Dummies Questions & Answers

Calling a script from master script to get value from called script

I am trying to call a script(callingscript.sh) from a master script(masterscript.sh) to get string type value from calling script to master script. I have used scripts mentioned below. #masterscript.sh ./callingscript.sh echo $fileExist #callingscript.sh echo "The script is called"... (2 Replies)
Discussion started by: Raj Roy
2 Replies

4. Shell Programming and Scripting

Script will keep checking running status of another script and also restart called script at night

I am using blow script :-- #!/bin/bash FIND=$(ps -elf | grep "snmp_trap.sh" | grep -v grep) #check snmp_trap.sh is running or not if then # echo "process found" exit 0; else echo "process not found" exec /home/Ketan_r /snmp_trap.sh 2>&1 & disown -h ... (1 Reply)
Discussion started by: ketanraut
1 Replies

5. Shell Programming and Scripting

create a shell script that calls another script and and an awk script

Hi guys I have a shell script that executes sql statemets and sends the output to a file.the script takes in parameters executes sql and sends the result to an output file. #!/bin/sh echo " $2 $3 $4 $5 $6 $7 isql -w400 -U$2 -S$5 -P$3 << xxx use $4 go print"**Changes to the table... (0 Replies)
Discussion started by: magikminox
0 Replies
Login or Register to Ask a Question