From: Janis Papanagnou on 19 Dec 2009 01:10 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 > > 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: Jack Shown on 19 Dec 2009 02:36 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. 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: mik3 on 19 Dec 2009 03:55 W=100 H=100 awk -vW=$W -vH=$H '{ for(i=1;i<=NF;i++) { if ($i~/width=\042/) { $i="width=\042"W"\042" } if ($i~/height=\042/){ $i="height=\042"H"\042" } } } {print} ' file
From: mop2 on 19 Dec 2009 04:46 On Sat, 19 Dec 2009 05:36:42 -0200, Jack Shown <jackshown(a)gmail.com> wrote: > 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. > > Jack $ bash -version|head -n1;sed --version|head -n1 GNU bash, version 4.0.35(1)-release (i686-pc-linux-gnu) GNU sed version 4.2.1 $ $ cat file #------ WIDTH=400 HEIGHT=20 sed ' s/ width="[0-9]\+"/ width="'$WIDTH'"/ s/ height="[0-9]\+"/ height="'$HEIGHT'"/ ' file return <zzzzz width="33" yyyyy height="555" wwwwww> #---------------- $ . ./file #------ WIDTH=400 HEIGHT=20 sed ' s/ width="[0-9]\+"/ width="'$WIDTH'"/ s/ height="[0-9]\+"/ height="'$HEIGHT'"/ ' file return <zzzzz width="400" yyyyy height="20" wwwwww> #---------------- $
From: Janis Papanagnou on 19 Dec 2009 07:09 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+7) 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. >
First
|
Prev
|
Next
|
Last
Pages: 1 2 3 4 Prev: What does 'S' mean in mode? And how to remove it? Next: configure MySQL on Leopard |