Need simple command that will change the 0 to a 1 in this file when I grep it, but only for this integer key directly after the ExcludeSimpleHostnames key.
The second one consists of 3 awk pattern/program pairs. The first is for when you are the target line, and s is non-zero one input line after the 2nd pattern/program, which set s to non-zero. The third pattern/program prints out every line.
To this section of this file like shown in the first example. There will be a lot more text in the file, but I am only concerned with the sections that are formatted like the one shown in the first example. They will always start with <key>Proxies</key>. There may be multiple <key>Proxies</key> sections, I will need the
INPROXIES=0
CONTAINSKEY=0
CONTAINSARRAYEND=0
while read line
do
# output the current line
echo $line >> output
# test if we're inside <key>Proxies
if [ $INPROXIES -eq 0 ]
then
# set flag only if in current line matches, next 'if' keeps it set
CONTAINSKEY=`echo $line|grep -c "<key>Proxies</key>"`
fi
# test if flag is set
if [ $CONTAINSKEY -eq 1 ]
then
# set flag
INPROXIES=1
CONTAINSARRAYEND=`echo $line|grep -c "<\array>"`
if [ $CONTAINSARRAYEND -eq 1 ]
then
# output new vals
echo " <key>ExcludeSimpleHostnames</key>" >> output
echo " <integer>1</integer>" >> output
# reset flags
INPROXIES=0
CONTAINSKEY=0
CONTAINSARRAYEND=0
fi
fi
done < infile
this code isn't tested, but it seems like it should do it.
I mean that I need it to check for the lines that I mentioned
<key>ExcludeSimpleHostnames</key>
<integer>1</integer>
In every section that starts with <key>Proxies</key>
and if it doesn't exist there to place it there the line after </array>
I want it to write this info to the file, not to create a new one.
This doesn't happen. A new file is ALWAYS written and the old file replaced. This is simply the method used by operating systems for "text" files of any sort.
The second problem is that you want to check "if it doesn't exist" and then place it there. Well, you can't really do that in one pass. The best solution is to Put it there and remove any existing ones.
If you want to use the integer value found in this proxy section, you're kind of screwed -- you have to either do backtracking or two passes or create a metastructure of all the data and write it out again to XML. That's because you want the ExcludeSimpleHostnames key to follow the array in the output, but the input, it might occur later.