Skip to main content

Thread: Setting the next line of a file to something if the above is something


i trying variable set next line if line above matches criteria, know line above is, not line below.

so, example of contents of file:
code:
		<key>cfbundledisplayname</key>  		<string>dismount</string>
i know cfbundledisplayname is, not next line down (dismount).
next line (dismount), want set variable (not <string> , </string> in though)

have spent hour messing around code try , read right (i change variable names )
code:
plutil -convert xml1 /applications/preferences.app/info.plist  themefile=$(</applications/preferences.app/info.plist)  theme="none"    line in $themefile;      #trim off whitespaces      line2=${line%%<}  	line3=${line2#<key>}      line3=${line3%</key>}        #now see if key      if [ "$line2:0:5" == "<string>" ];         #got key line         #need trim off xml tags  	    line4=${line2#<string>}          line4=${line4%</string>}         theme=$line4;    	         elif [ "$line3" == "cfbundledisplayname" ];   #found true line, setting variable $command :)         echo "found it"   fi  done  echo "$theme"
but alas, don't know how works, code taken answer have received before different question, , changed stuff around try , working. succesfully reads cfbundledisplayname line, , knows found it, don't know how set next line variable "$theme".

thank

-brian

quote posted bcooperizcool view post
i trying variable set next line if line above matches criteria, know line above is, not line below.

so, example of contents of file:
code:
		<key>cfbundledisplayname</key>  		<string>dismount</string>
i know cfbundledisplayname is, not next line down (dismount).
next line (dismount), want set variable (not <string> , </string> in though)

have spent hour messing around code try , read right (i change variable names )
code:
plutil -convert xml1 /applications/preferences.app/info.plist  themefile=$(</applications/preferences.app/info.plist)  theme="none"    line in $themefile;      #trim off whitespaces      line2=${line%%<}  	line3=${line2#<key>}      line3=${line3%</key>}        #now see if key      if [ "$line2:0:5" == "<string>" ];         #got key line         #need trim off xml tags  	    line4=${line2#<string>}          line4=${line4%</string>}         theme=$line4;    	         elif [ "$line3" == "cfbundledisplayname" ];   #found true line, setting variable $command :)         echo "found it"   fi  done  echo "$theme"
but alas, don't know how works, code taken answer have received before different question, , changed stuff around try , working. succesfully reads cfbundledisplayname line, , knows found it, don't know how set next line variable "$theme".

thank

-brian
since input xml, , question has perfect formulation in terms of xml, suggest use xml routines. xpath, example:

code:
$ cat data.xml   <data>    <key>cfbundledisplayname</key>    <string>dismount</string>  </data>  $ xpath -q -e '//*[preceding-sibling::key]/text()' data.xml   dismount
if use xml lot, xpath worth learning.


Forum The Ubuntu Forum Community Ubuntu Specialised Support Development & Programming Programming Talk [SOLVED] Setting the next line of a file to something if the above is something


Ubuntu

Comments

Popular posts from this blog

How to change text Component easybook reloaded *newbee* - Joomla! Forum - community, help and support

After Effect warning: A problem occurred when processing OpenGL commands

Preconditions Failed. - Joomla! Forum - community, help and support