Perl Script - Print Content of next line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Perl Script - Print Content of next line
# 1  
Old 10-19-2009
Perl Script - Print Content of next line

Good evening to you all perl experts

I need your help

I have a very simple script where Im trying to print a line from status.dat file.

The script will find the line containing "servicestatus", and I want to print the content of the next line.

For example, the file contains this text:

servicestatus {
hostname = Obelix

What i want to print is "hostname = Obelix"

Could you help?

Here goes the code. I want to replace "PRINT LINE AFTER THAT" with the necessary code to print the second line after "servicestatus."


Code:
#!/usr/bin/perl -w
 

 open my $logfile, 'status.dat' or die "I couldn't get at status.dat:  $!";
 

 for my $line (<$logfile>) {
      if $line =~ /^servicestatus/;
        "PRINT LINE AFTER THAT"
}

# 2  
Old 10-19-2009
Try:

Code:
#!/usr/bin/perl
use warnings;
use strict;

open (LOG,"<","status.dat") or die "I couldn't get at status.dat:  $!";

while (<LOG>) {
        chomp ($_);
        if ($_ =~ /^servicestatus/){
                my $nextline=<LOG>;
                print "$nextline";
        }
}

# 3  
Old 10-19-2009
You could try:
Code:
#!/usr/bin/perl -w
open LOGFILE, 'status.dat' or die "I couldn't get at status.dat:  $!";
while (<LOGFILE>) {
  print $_=<LOGFILE> if /^servicestatus/
}

# 4  
Old 10-19-2009
And yet another way:

Code:
$
$ cat t1
servicestatus {
hostname = Obelix
blah blah
}
servicestatus {
hostname = Asterix
blah blah
}
servicestatus {
hostname = Getafix
blah blah
}
$
$ perl -lne 'BEGIN{undef $/} while (/servicestatus {\n([^\n]+)\n/g){print $1}' t1
hostname = Obelix
hostname = Asterix
hostname = Getafix
$
$

tyler_durden
# 5  
Old 10-19-2009
That's almost like awk Smilie
Code:
awk '/^servicestatus/{getline;print}' infile

# 6  
Old 10-20-2009
It worked...thankx to you all...you guys rulle!

---------- Post updated at 06:09 AM ---------- Previous update was at 04:22 AM ----------

Btw, one more doubt...

Do you know how I can get the previous line instead of the next one?

Imagine that I have the following:

servicestatus {
hostname = Obelix
service name = test

The script will find the line containing "service name = test", and I want to print the content of the previous line "hostname = Obelix".

---------- Post updated at 08:10 AM ---------- Previous update was at 06:09 AM ----------

Ok...i got it

Heres one getting the previous line:

Code:
#!/usr/bin/perl -w

open (CHECKLOG, "status.dat");

@LINHAS = <CHECKLOG>;
@lnumber = @LINHAS;
$lnumber = @lnumber;

for ($i=0;$i < $lnumber; $i++){
        if($LINHAS[$i] =~ /^\tservice_description=PING/){
                print "Linhas: $LINHAS[$i-1]\n";
        }
}

Thkx again to you experts Smilie
# 7  
Old 10-20-2009
Code:
#!/usr/bin/perl -w
open LOGFILE, 'status.dat' or die "I couldn't get at status.dat:  $!";
while (<LOGFILE>) {
  print $prev if /^service name = test/;
  $prev=$_
}

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Perl to extract values and print at end of each line

In the below perl I am trying to extract and print the values AF1=, the GT value, and F or QUAL diveded by 33 (rounded to the nearest whole #). The GT value is at the end after the GT:PL so all the possibilities are read into a hash h, then depending on the value that is in the line the... (1 Reply)
Discussion started by: cmccabe
1 Replies

2. Shell Programming and Scripting

Break a line content and print as column

Hi, I have urls in my input file like this (1 Reply)
Discussion started by: tmonk1
1 Replies

3. Shell Programming and Scripting

Perl SCript to read file content (if else statemenet)

Hi All, I wanted to write a perl script to read the content in a file,the file content is either 0 (zero) OR 1. The idea is like this. If (content =1), then it will proceed to perform some step. and then update the file content to 0(zero) else if (content =0), it will update the content to... (11 Replies)
Discussion started by: hploh
11 Replies

4. Shell Programming and Scripting

Help to print the line that share exactly same with column one content

Input file : AAAG TC AACCCT AACCCT AACCCT AACCCT TCTG TCTG TCTG AC AC TCTG TCTG AC AC AC AC AC AGTG AC AGTG TCC Desired output file : AACCCT AACCCT AACCCT AACCCT AC AC I would like to print out the line that share exactly same as the first column data content. Column one data... (4 Replies)
Discussion started by: perl_beginner
4 Replies

5. Shell Programming and Scripting

Script to process file from a directory and grep the required content and print

Hi All, below script reads the perticular files from the directory. Am trying to fetch status and print them in the required format. It needs to read line and search for string "Passed/Failed" and print them under correct sub header. script : BASE_DIR=/tmp/test/REPORT/CollectReport #... (16 Replies)
Discussion started by: Optimus81
16 Replies

6. Shell Programming and Scripting

Break a line content and print as column

Hi, I have urls in my input file like this http://unix.com/abc/def http://unix.com/kil/min I want to use the / as separator and print the last content as another column like this http://unix.com/abc/def def http://unix.com/kil/min min I was using awk -F option and then joining the... (3 Replies)
Discussion started by: jacobs.smith
3 Replies

7. Shell Programming and Scripting

want to print the file content from the specific line

Hi All, I would like to print the content from the specific line of a file . For example... i have file abc.txt which has 100 lines of code ,from this file i would like to print the content from 20,19,18th line......like that Regards Srikanth (4 Replies)
Discussion started by: srikanthg
4 Replies

8. Shell Programming and Scripting

Perl question - How do I print contents of an array on a single line?

I have the following code: print @testarray; which returns: 8 8 8 9 How do I return the array like this: The output is: 8, 8, 8, 9 (5 Replies)
Discussion started by: streetfighter2
5 Replies

9. Shell Programming and Scripting

Perl :How to print the o/p of a Perl script on console and redirecting same in log file @ same time.

How can i print the output of a perl script on a unix console and redirect the same in a log file under same directory simultaneously ? Like in Shell script, we use tee, is there anything in Perl or any other option ? (2 Replies)
Discussion started by: butterfly20
2 Replies

10. Shell Programming and Scripting

Perl : Find a string and Print full line

Hi Need a perl script to read lines in a file, scan for a string named "APPLE" and write to different file the only lines containing the matched string. (5 Replies)
Discussion started by: PrasannaKS
5 Replies
Login or Register to Ask a Question