LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 07-05-2011, 03:00 PM   #1
ge1
LQ Newbie
 
Registered: Jun 2011
Posts: 10

Rep: Reputation: Disabled
Question perl script to extract string


hii all.

i'm writing a perl script, by searching the net and understanding the functionality of it.

the requirement is that i should create a file by using a perl scipt which should contain something like this.
struct regread ge1[] {
{"string","offset","shift,"mask"},
{... },
{... },
}

so, the file from which i should read contains something like this
Code:
#define SPIRO_TX_EB_ADDR_LOW_OFFSET  0xc00

#define SPIRO_TX_EB_ADDR_LOW_SHIFT    	   0x00

#define SPIRO_TX_EB_ADDR_LOW_MASK    	   0xFFFFFFFF



#define SPIRO_TX_EB_ADDR_HIGH_OFFSET  0xc04

#define SPIRO_TX_EB_ADDR_HIGH_SHIFT    	   0x00

#define SPIRO_TX_EB_ADDR_HIGH_MASK    	   0xFFFFFFFF
so i have to 1.extract string
2.eliminate #define
3.extract string from (offset,shift,mask) and write that in the string
4.for offset the respective address should be in "offset" of the structure similarly for mask, and shift.

i came to a stage and stuck i need your help in extracting the spiro_tx_eb_addr_high and separating from (offset,mask,shift)
my code is
Code:
#!usr/bin/perl

open IN, "/home/ge1/Documents/c_files/gmac_dma.h" or die "Can not open in.txt for reading : $!";			#opening the file

while ( my $line = <IN> ) {		#openig each line in file
	#print "$line";		
my @list = split ' ' , $line;		#spliting the line on spaces,storing
#print "string:$list[1]\t\taddress:$list[2]\n";	#printing the wanted stuff
@string=$list[1]=~ m/OFFSET/g;
print "@string";

}

close IN or die "Can not close file : $!";
i hope you understood what i was expecting, if so please help. I'm not asking you to give me code, just tell how to do.
 
Old 07-05-2011, 07:14 PM   #2
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,362

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
A few tips

1. amend top of prog to
Code:
#!usr/bin/perl -w
use strict;
2. Good links
http://perldoc.perl.org/
http://www.perlmonks.org/?node=Tutorials

3. 3 arg open() is best practice
Code:
open(INFILE, "<", $file) or die "Unable to open $file: $!\n";
4. tidier version of while loop
Code:
while ( defined ( $rec = <IN_FILE> ) )
{
    chomp($rec);
    ($var1, $var2) = (split(/:/, $rec));
    print  "$var1, $var2\n";
}
note use of defined() & chomp().
For a small amt of vars, you can specify target vars directly; using an array is optional; your call.

5. I think I'd like to see an example of the desired output before going any further.
 
Old 07-06-2011, 12:33 AM   #3
samratk1230
LQ Newbie
 
Registered: Jul 2011
Posts: 1

Rep: Reputation: Disabled
Reply

Really nice.

Last edited by Tinkster; 07-06-2011 at 09:12 PM.
 
Old 07-07-2011, 09:45 AM   #4
ge1
LQ Newbie
 
Registered: Jun 2011
Posts: 10

Original Poster
Rep: Reputation: Disabled
Smile

sir, the output pattern is also mentioned in my above post.

the requirement is to read from a file, and create another file which should contain a structure.

the prototype of it is in my previous post.

thank you for answering.
 
Old 07-07-2011, 12:14 PM   #5
markush
Senior Member
 
Registered: Apr 2007
Location: Germany
Distribution: Slackware
Posts: 3,979

Rep: Reputation: Disabled
Hello ge1,

first of all you should use the "Report"-button at your post and ask a Moderator to move this thread to the Programming forum of LQ, http://www.linuxquestions.org/questions/programming-9/ because this is a programming-question.

To your program, the structure of your data will be fine represented by a "hash of hashes".
If you have a hash named %datahash it could look like
Code:
%datahash = ( SPIRO_TX_EB_ADDR_LOW => { offset => 0xc00, shift => 0x00, mask => 0xFFFFFFFF } 
... and so on ...
)
look here http://docstore.mik.ua/orelly/perl/prog3/ch09_04.htm

Markus
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Perl string question:extract lines from somefile dreamlemon Programming 1 06-19-2011 05:02 AM
Need Perl Script to extract last octent of an Argument pooppp Linux - Newbie 2 09-30-2008 08:13 AM
awk/perl script for extract data mruknown1 Programming 3 09-11-2007 09:19 AM
awk/perl script for extract data mruknown1 Linux - Newbie 2 09-11-2007 04:42 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 01:06 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration