Perl : Process a file in perl


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Perl : Process a file in perl
# 1  
Old 09-22-2009
Perl : Process a file in perl

I have a file with following data :

Code:
Qspace: COLOR: Queue: doColor
Qspace: COLOR: Queue order: fifo
Qspace: COLOR: Out of order: none
Qspace: COLOR: Retries: 5
Qspace: COLOR: Queue: setColor
Qspace: COLOR: Queue order: fifo
Qspace: COLOR: Out of order: none
Qspace: COLOR: Retries: 5
Qspace: COLOR
Qspace: MatchSpace: Queue: getColor
Qspace: MatchSpace: Queue order: fifo
Qspace: MatchSpace: Out of order: none
Qspace: MatchSpace: Retries: 10
Qspace: MatchSpace
:
:

I am looking for an output like,

Code:
Qspace: COLOR: Queue: doColor
Qspace: COLOR: Queue: doColor: Queue order: fifo
Qspace: COLOR: Queue: doColor: Out of order: none
Qspace: COLOR: Queue: doColor: Retries: 5
Qspace: COLOR: Queue: setColor
Qspace: COLOR: Queue: setColor: Queue order: fifo
Qspace: COLOR: Queue: setColor: Out of order: none
Qspace: COLOR: Queue: setColor: Retries: 5
Qspace: COLOR
Qspace: MatchSpace: Queue: getColor
Qspace: MatchSpace: Queue: getColor: Queue order: fifo
Qspace: MatchSpace: Queue: getColor: Out of order: none
Qspace: MatchSpace: Queue: getColor: Retries: 10
Qspace: MatchSpace

For each Qspace there is/are queue/s associated with it. I need to append name of each Qspace & Queue with every attribute.

Last edited by vgersh99; 09-22-2009 at 10:24 AM.. Reason: code tags, PLEASE!
# 2  
Old 09-22-2009
To keep the forums high quality for all users, please take the time to format your posts correctly.

First of all, use Code Tags when you post any code or data samples so others can easily read your code. You can easily do this by highlighting your code and then clicking on the # in the editing menu. (You can also type code tags [code] and [/code] by hand.)

Second, avoid adding color or different fonts and font size to your posts. Selective use of color to highlight a single word or phrase can be useful at times, but using color, in general, makes the forums harder to read, especially bright colors like red.

Third, be careful when you cut-and-paste, edit any odd characters and make sure all links are working property.

Thank You.

The UNIX and Linux Forums
# 3  
Old 09-22-2009
Looks awfully like a double-post of this
# 4  
Old 09-24-2009
Code:
open FH,"<a.txt";
while(<FH>){
  if(/Queue: ([^ \n]+)$/){
  print;
  $queue=$1;
  next;
}
  if(/Retries/){
  s/(COLOR|MatchSpace): /$1: Queue: $queue: /;
  print;
  undef $queue;
  next;
}
  if(/COLOR$/){
  print;
  next;
}
  s/(COLOR|MatchSpace): /$1: Queue: $queue: /;
  print;
}

# 5  
Old 09-29-2009
Thanks summer_cherryfot the code. I am looking for a more generic form of the code. COLOR & MatchSpace has been hardcoded to get the desired result. If you can give me more generic form of the code without hardcoding any values, it will be great. (Can consider those values are values in an array.)
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

PERL: In a perl-scripttTrying to execute another perl-script that SETS SOME VARIABLES !

I have reviewed many examples on-line about running another process (either PERL or shell command or a program), but do not find any usefull for my needs way. (Reviewed and not useful the system(), 'back ticks', exec() and open()) I would like to run another PERL-script from first one, not... (1 Reply)
Discussion started by: alex_5161
1 Replies

2. Shell Programming and Scripting

Parallel process in Perl

HI All, I have scenerio where I need to call sub modules through for loop for (i=0; i<8000 ;i++) { .. BLOCKA } BLOCKA { .. .. subroutine 1; subroutine 2; } I want this to be run in parallel process1 BLOCKA { (6 Replies)
Discussion started by: gvk25
6 Replies

3. Shell Programming and Scripting

Perl: How to read text from file and process $variable in that data too.

In the hello.htm have the sentenses: Hello $name How are you? The perl script: $name = "David"; open(HEADER,"hello.htm"); while(<HEADER>) { $html .= $_; } close(HEADER); print "$html";I making something about template. But it can't process the $name variable. (4 Replies)
Discussion started by: natong
4 Replies

4. Shell Programming and Scripting

Perl SFTP, to get, sort and process every file.

Hi All, I'm niks, and i'm a newbie here and newbie also in perl sorry, i'm just wondering how can i get the file from the other hostname using sftp? then after i get it i'm going to sort the file and process it one by one. sorry because i'm a newbie. Thanks, -niks (4 Replies)
Discussion started by: nikki1200
4 Replies

5. Shell Programming and Scripting

How to put FTP process as a background process/job in perl?

Hi, I am using net::ftp for transferring files now i am trying in the same Linux server as a result ftp is very fast but if the server is other location (remote) then the file transferred will be time consuming. So i want try putting FTP part as a background process. I am unaware how to do... (5 Replies)
Discussion started by: vanitham
5 Replies

6. Programming

Perl- How to catch the file in perl code?

Hi, plz see the below code , i have catch the file "Orders.20110714.out "file as a Orders*.out. but it giving me an error .it does not open the file. if the same thing i have done by code code-> ls Orders*.out then it gives me the output Orders.20110714.out i am trying apply the... (1 Reply)
Discussion started by: pspriyanka
1 Replies

7. 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

8. Shell Programming and Scripting

how to get process id in perl

Hey guys, how can i get process id of some particular command in perl? example: $cmd = "date; sleep 10; date&"; system($cmd); How can i get process id of system command? (2 Replies)
Discussion started by: RohitKJ
2 Replies

9. Shell Programming and Scripting

perl -write values in a file to @array in perl

Hi can anyone suggest me how to write a file containing values,... say 19 20 21 22 .. 40 to an array @array = (19, 20, ... 40) -- Thanks (27 Replies)
Discussion started by: meghana
27 Replies

10. Shell Programming and Scripting

Perl: Run perl script in the current process

I have a question regarding running perl in the current process. I shall demonstrate with an example. Look at this. sh-2.05b$ pwd /tmp sh-2.05b$ cat test.sh #! /bin/sh cd /etc sh-2.05b$ ./test.sh sh-2.05b$ pwd /tmp sh-2.05b$ . ./test.sh sh-2.05b$ pwd /etc sh-2.05b$ So... (10 Replies)
Discussion started by: vino
10 Replies
Login or Register to Ask a Question