From: Janis Papanagnou on
[Repost with correction of errors I made in the code... (no coffee yet).]

Janis Papanagnou wrote:
> Jack Shown wrote:
>> On Dec 18, 10:10 pm, Janis Papanagnou <janis_papanag...(a)hotmail.com>
>> wrote:
>>> Jack Shown wrote:
>>>> On Dec 18, 9:29 pm, Janis Papanagnou <janis_papanag...(a)hotmail.com>
>>>> wrote:
>>>>> Jack Shown wrote:
>>>>>> I have files that contain strings such as: width="???" height="???"
>>>>>> where the question marks represent positive integers of either two or
>>>>>> three digits. Similar strings do exist but only once in each file
>>>>>> does
>>>>>> " width=" and " height=" exist. If I don't know where this string
>>>>>> exists for sure, is there anyway to obtain those values and replace
>>>>>> them based on their aspect ratio? I don't need the aspect ratio
>>>>>> conversion math, of course, just the extraction and the replacement.
>>>>>> I'm using bash but perl or python would work too of course. Thx.
>>>>> Based on Barry's solution path try...
>>>>> sed -e "s/ width=[0-9]\+/ width=$WIDTH/" \
>>>>> -e "s/ height=[0-9]\+/ height=$HEIGHT/" < infile > outfile
>>>>> But your "based on their aspect ratio" sounds like you want some
>>>>> calculations based on the previous values involved? If so, please
>>>>> clarify your demands.
>>>>> Janis
>>>> No, the aspect ratio does not affect the substitution as Barry rightly
>>>> chose numbers such as 80x24.
>>>> I think your attempt is almost there but it misses the quote (") on
>>>> each side of the integer value thus the substitution is not
>>>> performed. I tried it with \" on each side but didn't get a
>>>> substitution either.
>>> Oh, sorry for my inattentiveness WRT the quotes.
>>>
>>> Adding \" around the pattern and around the substitution works for me,
>>> though...
>>>
>>> sed -e "s/ width=\"[0-9]\+\"/ width=\"$WIDTH\"/" \
>>> -e "s/ height=\"[0-9]\+\"/ height=\"$HEIGHT\"/" < infile >
>>> outfile
>>>
>>> Mind to check this again?
>>>
>>> Janis
>>
>> Hi Janis, I think that's exactly what I tried (as I mentioned in my
>> previous post) but I gave yours a whirl anyway and it still did not
>> substitute. I put what you wrote into a small shell script and ran it
>> with bash -x to see it execute and the command output looks like I
>> think it should:
>>
>> + sed -e 's/ width="[0-9]\+"/ width="256"/' -e 's/ height="[0-9]\
>> +"/ height="192"/'
>
> Hmm.. - that's strange. Some random thoughts...
> Could it be that "width=" and "height=" is not prefixed by a blank, maybe
> by a tab, or on the begin of the line? (Then change the regexp.)
> Are you running a sed that doesn't support \+ in regexps? (Then replace
> the respective regexp parts by [0-9][0-9]* .)
>
> Otherwise I can just advise to use another tool; in awk
>

awk -v width="$WIDTH" -v height="$HEIGHT" '
match($0,/ width="[0-9]+"/) {
$0 = substr($0,1,RSTART+7) width substr($0,RSTART+RLENGTH-1)
}
match($0,/ height="[0-9]+"/) {
$0 = substr($0,1,RSTART+8) height substr($0,RSTART+RLENGTH-1)
}
{ print $0 }
'

> (Note: in perl this construct is simpler where AFAIK you can substitute
> with
> substring specifications and backreferences inside the substitution
> operator.
> But since I'm not experienced in perl I abstain from providing more than
> just
> a hint.)
>
> Janis
>
>>
>> I appreciate your efforts.
>>
>> Jack
>>
>>
>>
>>
>>>> The input is what one would obtain from Google Maps when, for example,
>>>> getting directions from one place to another. If you then click
>>>> "Link" in the top right corner, you obtain the code that is used to
>>>> reproduce a similar map on your site. It is that code that is my
>>>> input file. However, I cannot know what values someone may choose.
>>>> I think 425 and 350 are the default but can be overridden by
>>>> "Customize and preview embedded map" which you see below Google's
>>>> provided html.
>>
From: W. James on
Jack Shown wrote:

> I have files that contain strings such as: width="???" height="???"
> where the question marks represent positive integers of either two or
> three digits. Similar strings do exist but only once in each file does
> " width=" and " height=" exist. If I don't know where this string
> exists for sure, is there anyway to obtain those values and replace
> them based on their aspect ratio? I don't need the aspect ratio
> conversion math, of course, just the extraction and the replacement.
> I'm using bash but perl or python would work too of course. Thx.


Using Ruby:

# Remember filename (given on command-line).
file = ARGV.first
# Read entire file.
text = gets(nil)
# Make a change. "\b" indicates word-boundary.
text[ /\bwidth="(\d{2,3})"/, 1 ] = "640"
# Make another change.
text[ /\bheight="(\d{2,3})"/, 1 ] = "480"
# Write the changed text to original file.
File.open(file, "w"){|f| f.print text }


--

From: Barry Margolin on
In article
<d0efec58-7d56-4322-a35b-5dc832a77632(a)x5g2000prf.googlegroups.com>,
Jack Shown <jackshown(a)gmail.com> wrote:

> On Dec 18, 10:10�pm, Janis Papanagnou <janis_papanag...(a)hotmail.com>
> wrote:
> > Jack Shown wrote:
> > > On Dec 18, 9:29 pm, Janis Papanagnou <janis_papanag...(a)hotmail.com>
> > > wrote:
> > >> Jack Shown wrote:
> > >>> I have files that contain strings such as: �width="???" height="???"
> > >>> where the question marks represent positive integers of either two or
> > >>> three digits. Similar strings do exist but only once in each file does
> > >>> " width=" and " height=" exist. If I don't know where this string
> > >>> exists for sure, is there anyway to obtain those values and replace
> > >>> them based on their aspect ratio? �I don't need the aspect ratio
> > >>> conversion math, of course, just the extraction and the replacement.
> > >>> I'm using bash but perl or python would work too of course. �Thx.
> > >> Based on Barry's solution path try...
> >
> > >> sed -e "s/ width=[0-9]\+/ width=$WIDTH/" \
> > >> � � �-e "s/ height=[0-9]\+/ height=$HEIGHT/" �< infile �> outfile
> >
> > >> But your "based on their aspect ratio" sounds like you want some
> > >> calculations based on the previous values involved? If so, please
> > >> clarify your demands.
> >
> > >> Janis
> >
> > > No, the aspect ratio does not affect the substitution as Barry rightly
> > > chose numbers such as 80x24.
> >
> > > I think your attempt is almost there but it misses the quote (") on
> > > each side of the integer value thus the substitution is not
> > > performed. �I tried it with \" on each side but didn't get a
> > > substitution either.
> >
> > Oh, sorry for my inattentiveness WRT the quotes.
> >
> > Adding \" around the pattern and around the substitution works for me,
> > though...
> >
> > sed -e "s/ width=\"[0-9]\+\"/ width=\"$WIDTH\"/" \
> > � � �-e "s/ height=\"[0-9]\+\"/ height=\"$HEIGHT\"/" �< infile �> outfile
> >
> > Mind to check this again?
> >
> > Janis
>
> Hi Janis, I think that's exactly what I tried (as I mentioned in my
> previous post) but I gave yours a whirl anyway and it still did not
> substitute. I put what you wrote into a small shell script and ran it
> with bash -x to see it execute and the command output looks like I
> think it should:
>
> + sed -e 's/ width="[0-9]\+"/ width="256"/' -e 's/ height="[0-9]\
> +"/ height="192"/'
>
> I appreciate your efforts.

If neither my nor Janis's code works, I suspect you didn't describe the
input file accurately. Can you post a short example of the input?

--
Barry Margolin, barmar(a)alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
*** PLEASE don't copy me on replies, I'll read them in the group ***
From: mop2 on
On Sat, 19 Dec 2009 20:37:26 -0200, Barry Margolin <barmar(a)alum.mit.edu> wrote:

> In article
> <d0efec58-7d56-4322-a35b-5dc832a77632(a)x5g2000prf.googlegroups.com>,
> Jack Shown <jackshown(a)gmail.com> wrote:
>
>> On Dec 18, 10:10 pm, Janis Papanagnou <janis_papanag...(a)hotmail.com>
>> wrote:
>> > Jack Shown wrote:
>> > > On Dec 18, 9:29 pm, Janis Papanagnou <janis_papanag...(a)hotmail.com>
>> > > wrote:
>> > >> Jack Shown wrote:
>> > >>> I have files that contain strings such as: width="???" height="???"
>> > >>> where the question marks represent positive integers of either two or
>> > >>> three digits. Similar strings do exist but only once in each file does
>> > >>> " width=" and " height=" exist. If I don't know where this string
>> > >>> exists for sure, is there anyway to obtain those values and replace
>> > >>> them based on their aspect ratio? I don't need the aspect ratio
>> > >>> conversion math, of course, just the extraction and the replacement.
>> > >>> I'm using bash but perl or python would work too of course. Thx.
>> > >> Based on Barry's solution path try...
>> >
>> > >> sed -e "s/ width=[0-9]\+/ width=$WIDTH/" \
>> > >> -e "s/ height=[0-9]\+/ height=$HEIGHT/" < infile > outfile
>> >
>> > >> But your "based on their aspect ratio" sounds like you want some
>> > >> calculations based on the previous values involved? If so, please
>> > >> clarify your demands.
>> >
>> > >> Janis
>> >
>> > > No, the aspect ratio does not affect the substitution as Barry rightly
>> > > chose numbers such as 80x24.
>> >
>> > > I think your attempt is almost there but it misses the quote (") on
>> > > each side of the integer value thus the substitution is not
>> > > performed. I tried it with \" on each side but didn't get a
>> > > substitution either.
>> >
>> > Oh, sorry for my inattentiveness WRT the quotes.
>> >
>> > Adding \" around the pattern and around the substitution works for me,
>> > though...
>> >
>> > sed -e "s/ width=\"[0-9]\+\"/ width=\"$WIDTH\"/" \
>> > -e "s/ height=\"[0-9]\+\"/ height=\"$HEIGHT\"/" < infile > outfile
>> >
>> > Mind to check this again?
>> >
>> > Janis
>>
>> Hi Janis, I think that's exactly what I tried (as I mentioned in my
>> previous post) but I gave yours a whirl anyway and it still did not
>> substitute. I put what you wrote into a small shell script and ran it
>> with bash -x to see it execute and the command output looks like I
>> think it should:
>>
>> + sed -e 's/ width="[0-9]\+"/ width="256"/' -e 's/ height="[0-9]\
>> +"/ height="192"/'
>>
>> I appreciate your efforts.
>
> If neither my nor Janis's code works, I suspect you didn't describe the
> input file accurately. Can you post a short example of the input?
>


Yes, if the question is not solved, that is important. An easy way to check
each char can be:

$ tr '<>' '\n' <file|grep 'height="[1-9]'|od -An -tc -tx1
z z z z z w i d t h = " 3 3 "
7a 7a 7a 7a 7a 20 77 69 64 74 68 3d 22 33 33 22
y y y y y h e i g h t = " 5
20 79 79 79 79 79 20 68 65 69 67 68 74 3d 22 35
5 5 " w w w w w w \n
35 35 22 20 77 77 77 77 77 77 0a
$

In my "file", already posted, the prefix for width and height is
space, 20 hex. If it is not the case of the OP, the sed command
will not work, as already said by others.

The command od can be replaced by xxd, if available:

$ tr '<>' '\n' <file|grep 'height="[1-9]'|xxd
0000000: 7a7a 7a7a 7a20 7769 6474 683d 2233 3322 zzzzz width="33"
0000010: 2079 7979 7979 2068 6569 6768 743d 2235 yyyyy height="5
0000020: 3535 2220 7777 7777 7777 0a 55" wwwwww.
$

With font fixed (mono spaced) in the terminal, the result is more clear.
From: Jack Shown on
On Dec 19, 4:20 am, Janis Papanagnou <janis_papanag...(a)hotmail.com>
wrote:
> [Repost with correction of errors I made in the code... (no coffee yet).]
>
>
>
>
>
> Janis Papanagnou wrote:
> > Jack Shown wrote:
> >> On Dec 18, 10:10 pm, Janis Papanagnou <janis_papanag...(a)hotmail.com>
> >> wrote:
> >>> Jack Shown wrote:
> >>>> On Dec 18, 9:29 pm, Janis Papanagnou <janis_papanag...(a)hotmail.com>
> >>>> wrote:
> >>>>> Jack Shown wrote:
> >>>>>> I have files that contain strings such as:  width="???" height="???"
> >>>>>> where the question marks represent positive integers of either two or
> >>>>>> three digits. Similar strings do exist but only once in each file
> >>>>>> does
> >>>>>> " width=" and " height=" exist. If I don't know where this string
> >>>>>> exists for sure, is there anyway to obtain those values and replace
> >>>>>> them based on their aspect ratio?  I don't need the aspect ratio
> >>>>>> conversion math, of course, just the extraction and the replacement.
> >>>>>> I'm using bash but perl or python would work too of course.  Thx..
> >>>>> Based on Barry's solution path try...
> >>>>> sed -e "s/ width=[0-9]\+/ width=$WIDTH/" \
> >>>>>      -e "s/ height=[0-9]\+/ height=$HEIGHT/"  < infile  > outfile
> >>>>> But your "based on their aspect ratio" sounds like you want some
> >>>>> calculations based on the previous values involved? If so, please
> >>>>> clarify your demands.
> >>>>> Janis
> >>>> No, the aspect ratio does not affect the substitution as Barry rightly
> >>>> chose numbers such as 80x24.
> >>>> I think your attempt is almost there but it misses the quote (") on
> >>>> each side of the integer value thus the substitution is not
> >>>> performed.  I tried it with \" on each side but didn't get a
> >>>> substitution either.
> >>> Oh, sorry for my inattentiveness WRT the quotes.
>
> >>> Adding \" around the pattern and around the substitution works for me,
> >>> though...
>
> >>> sed -e "s/ width=\"[0-9]\+\"/ width=\"$WIDTH\"/" \
> >>>      -e "s/ height=\"[0-9]\+\"/ height=\"$HEIGHT\"/"  < infile  >
> >>> outfile
>
> >>> Mind to check this again?
>
> >>> Janis
>
> >> Hi Janis,  I think that's exactly what I tried (as I mentioned in my
> >> previous post) but I gave yours a whirl anyway and it still did not
> >> substitute.  I put what you wrote into a small shell script and ran it
> >> with bash -x to see it execute and the command output looks like I
> >> think it should:
>
> >>    + sed -e 's/ width="[0-9]\+"/ width="256"/' -e 's/ height="[0-9]\
> >> +"/ height="192"/'
>
> > Hmm.. - that's strange. Some random thoughts...
> > Could it be that "width=" and "height=" is not prefixed by a blank, maybe
> > by a tab, or on the begin of the line? (Then change the regexp.)
> > Are you running a sed that doesn't support \+ in regexps? (Then replace
> > the respective regexp parts by  [0-9][0-9]* .)
>
> > Otherwise I can just advise to use another tool; in awk
>
> awk -v width="$WIDTH" -v height="$HEIGHT" '
>    match($0,/ width="[0-9]+"/) {
>      $0 = substr($0,1,RSTART+7) width substr($0,RSTART+RLENGTH-1)
>    }
>    match($0,/ height="[0-9]+"/) {
>      $0 = substr($0,1,RSTART+8) height substr($0,RSTART+RLENGTH-1)
>    }
>    { print $0 }
> '


That worked! Thanks for being so persistent and patient!


> > (Note: in perl this construct is simpler where AFAIK you can substitute
> > with
> > substring specifications and backreferences inside the substitution
> > operator.
> > But since I'm not experienced in perl I abstain from providing more than
> > just
> > a hint.)
>
> > Janis
>
> >> I appreciate your efforts.
>
> >> Jack
>
> >>>> The input is what one would obtain from Google Maps when, for example,
> >>>> getting directions from one place to another.  If you then click
> >>>> "Link" in the top right corner, you obtain the code that is used to
> >>>> reproduce a similar map on your site.  It is that code that is my
> >>>> input file.  However, I cannot know what  values someone may choose.
> >>>> I think 425 and 350 are the default but can be overridden by
> >>>> "Customize and preview embedded map" which you see below Google's
> >>>> provided html.