Thread Subject: grep/regex---HELP

Subject: grep/regex---HELP

From: Muhammad Arsalan

Date: 24 Sep, 2009 08:35:03

Message: 1 of 8

Hi,
I need help regarding writing a script capable to do following;

o Accept file path of m.file
o scan all text on the left side of = in declarations
o Compare the scanned text with the text on right hand side of =
o create a new file containing the matched statements or declarations

Any suggestion please???

Thanks in advance for the support.

Arsalan

Subject: grep/regex---HELP

From: Sebastiaan

Date: 24 Sep, 2009 10:23:06

Message: 2 of 8

"Muhammad Arsalan" <arsalancheema@gmail.com> wrote in message <h9favn$1ql$1@fred.mathworks.com>...
> Hi,
> I need help regarding writing a script capable to do following;
>
> o Accept file path of m.file
> o scan all text on the left side of = in declarations
> o Compare the scanned text with the text on right hand side of =
> o create a new file containing the matched statements or declarations
>
> Any suggestion please???
>
> Thanks in advance for the support.
>
> Arsalan

Use AWK. Assuming your input looks like:
abc=abc
abc=cdbaaek

awk -F= '{if ($1==$2) {print $1"="$2}}' yourinput.m > youroutput.m

Sebastiaan

Subject: grep/regex---HELP

From: Sebastiaan

Date: 24 Sep, 2009 10:30:06

Message: 3 of 8

I was assuming you are running Linux, of which I am not sure.

In Matlab, I would open the input and output file with fopen, read it line by line with fgetl and compare it:
Z = regexp('abc=ab', '=', 'split');
if iscell(Z) && lenght(Z)==2
  if strcmp(Z{1},Z{2})
    % write to output
  end
end

Subject: grep/regex---HELP

From: Dan Pearce

Date: 24 Sep, 2009 10:30:06

Message: 4 of 8

> o Accept file path of m.file
Just define the file path as a string. Extension doesn't really matter as long as it is plain text.

> o scan all text on the left side of = in declarations
fopen to open the file
fgetl to get a line from the file. Use a while loop

> o Compare the scanned text with the text on right hand side of =
parts = regexp(tline,’=’,’split’) to split the string into left and right parts

> o Compare the scanned text with the text on right hand side of =
strcmp to compare the two strings.

> o create a new file containing the matched statements or declarations
fopen/fprintf/fclose to write the new file

Subject: grep/regex---HELP

From: Muhammad Arsalan

Date: 24 Sep, 2009 12:18:03

Message: 5 of 8

"Sebastiaan " <s.breedveld@erasmusmc.REMOVE.BOO.BOO.nl> wrote in message <h9fhne$7t9$1@fred.mathworks.com>...
> I was assuming you are running Linux, of which I am not sure.
>
> In Matlab, I would open the input and output file with fopen, read it line by line with fgetl and compare it:
> Z = regexp('abc=ab', '=', 'split');
> if iscell(Z) && lenght(Z)==2
> if strcmp(Z{1},Z{2})
> % write to output
> end
> end


Thanks for quick response, there is a new problem i am facing, in my m.script for which i am using your hints, there are some matrices and fgetl do not care about that it just read line, so how can i handle those matrices e.g

xyz = [ 10
           match
           50
           70]

i have to keep these matrices like this to improve readability and easy modifications.
Any help regarding this.

Thank you very much indeed

Subject: grep/regex---HELP

From: Sebastiaan

Date: 24 Sep, 2009 12:25:20

Message: 6 of 8

"Muhammad Arsalan" <arsalancheema@gmail.com> wrote in message <h9fo1r$slf$1@fred.mathworks.com>...
> "Sebastiaan " <s.breedveld@erasmusmc.REMOVE.BOO.BOO.nl> wrote in message <h9fhne$7t9$1@fred.mathworks.com>...
> > I was assuming you are running Linux, of which I am not sure.
> >
> > In Matlab, I would open the input and output file with fopen, read it line by line with fgetl and compare it:
> > Z = regexp('abc=ab', '=', 'split');
> > if iscell(Z) && lenght(Z)==2
> > if strcmp(Z{1},Z{2})
> > % write to output
> > end
> > end
>
>
> Thanks for quick response, there is a new problem i am facing, in my m.script for which i am using your hints, there are some matrices and fgetl do not care about that it just read line, so how can i handle those matrices e.g
>
> xyz = [ 10
> match
> 50
> 70]
>
> i have to keep these matrices like this to improve readability and easy modifications.
> Any help regarding this.

Then I do not understand what you want. You say:

o scan all text on the left side of = in declarations
o Compare the scanned text with the text on right hand side of =

but this fails in your example, since 'xyz' does not equal '[ 10; match; 50; 70]
And: you cannot have a string ('match') in a numercial matrix.

Do you maybe want to compare if your current value for xyz equals the rhs of your check file?

Subject: grep/regex---HELP

From: Muhammad Arsalan

Date: 24 Sep, 2009 12:57:01

Message: 7 of 8

"Sebastiaan " <s.breedveld@erasmusmc.REMOVE.BOO.BOO.nl> wrote in message <h9fofg$pgo$1@fred.mathworks.com>...
> "Muhammad Arsalan" <arsalancheema@gmail.com> wrote in message <h9fo1r$slf$1@fred.mathworks.com>...
> > "Sebastiaan " <s.breedveld@erasmusmc.REMOVE.BOO.BOO.nl> wrote in message <h9fhne$7t9$1@fred.mathworks.com>...
> > > I was assuming you are running Linux, of which I am not sure.
> > >
> > > In Matlab, I would open the input and output file with fopen, read it line by line with fgetl and compare it:
> > > Z = regexp('abc=ab', '=', 'split');
> > > if iscell(Z) && lenght(Z)==2
> > > if strcmp(Z{1},Z{2})
> > > % write to output
> > > end
> > > end
> >
> >
> > Thanks for quick response, there is a new problem i am facing, in my m.script for which i am using your hints, there are some matrices and fgetl do not care about that it just read line, so how can i handle those matrices e.g
> >
> > xyz = [ 10
> > match
> > 50
> > 70]
> >
> > i have to keep these matrices like this to improve readability and easy modifications.
> > Any help regarding this.
>
> Then I do not understand what you want. You say:
>
> o scan all text on the left side of = in declarations
> o Compare the scanned text with the text on right hand side of =
>
> but this fails in your example, since 'xyz' does not equal '[ 10; match; 50; 70]
> And: you cannot have a string ('match') in a numercial matrix.
>
> Do you maybe want to compare if your current value for xyz equals the rhs of your check file?


ok to be very precise i want to separate those declarations which are dependent on any other declaration in that same file e.g.

x= 10;
y= [0, 3, x, 15];
f= [2, 4, 6, 7];
.
.
z= [2 3 4
      x 7 9
      9 8 y]

i want to check if anything on the left hand side of "=" i.e. x,y,f,z come any where on the right hand side of "=" like in y and z, those declarations get printed in a separate file. This is a rather complex for me.
and i am using winXP MATLAB7.5.0(R2007b)

Subject: grep/regex---HELP

From: Sebastiaan

Date: 24 Sep, 2009 13:12:01

Message: 8 of 8

> ok to be very precise i want to separate those declarations which are dependent on any other declaration in that same file e.g.
>
> x= 10;
> y= [0, 3, x, 15];
> f= [2, 4, 6, 7];
> .
> .
> z= [2 3 4
> x 7 9
> 9 8 y]
>
> i want to check if anything on the left hand side of "=" i.e. x,y,f,z come any where on the right hand side of "=" like in y and z, those declarations get printed in a separate file. This is a rather complex for me.

So, your example output should be:
y = [0, 3, x, 15]; % since x is declared before
z = [2 3 4
       x 7 9
       9 8 y] % since x and y have been declared before


What about:
t = [0 w] % w is undefined ?

Seems that you have quite some coding to do. What I would do:
- open file
- read line-by-line
- use regexp to split = (Z{1} = x, Z{2} = 10;)
- record LHS in a cell array as declared variable (Declared{length(Declared)+1} = Z{1})
- use strfind/findstr to determine if any variable in Declared is in Z{2}.
- write stuff to output file if necessary

To handle multiline arrays, you need to keep track of the brackets:
t = fgetl(input);
while ~findstr(t, ']')
  t = strcat(t, fgetl(intput));
end

Now t contains the whole 'block'.

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Tag Activity for This Thread
Tag Applied By Date/Time
regex Muhammad Arsalan 24 Sep, 2009 04:39:03
grep Muhammad Arsalan 24 Sep, 2009 04:39:03
generating new ... Muhammad Arsalan 24 Sep, 2009 04:39:03
rssFeed for this Thread

Contact us at files@mathworks.com