Read file and change a 0 to a 1 in output


 
Thread Tools Search this Thread
Homework and Emergencies Emergency UNIX and Linux Support Read file and change a 0 to a 1 in output
# 1  
Old 09-30-2009
Read file and change a 0 to a 1 in output

Code:
<key>ExcludeSimpleHostnames</key>
                <integer>0</integer>
                <key>FTPPassive</key>

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.

I got this output code from running:
Code:
grep -iA2  excludesimplehostnames /Library/Preferences/SystemConfiguration/preferences.plist

# 2  
Old 09-30-2009
check syntax on '&&' in if condition, otherwise, should work
Code:
PREVLINE=""
CURRLINE=""
while read line
do
    PREVLINE=$CURRLINE
    CURRLINE=$line
    CONTAINSKEY=`echo $PREVLINE|grep -c ExcludeSimpleHostnames`
    CONTAINSINT=`echo $CURRLINE|grep -c "<integer>0<"`
    if [ $CONTAINSKEY -eq 1 && $CONTAINSINT -eq 1 ]
    then
         echo $line|sed 's/0/1/' >> outputfile
    else
         echo $line >> outputfile
    fi
done < infile


Last edited by varontron; 09-30-2009 at 02:28 PM..
# 3  
Old 09-30-2009
Here's another solution, and this assumes you DO NOT want the other lines in the file printed. It uses awk's "range" operator.
Code:
awk '/<key>ExcludeSimpleHostnames<\/key>/,/<key>/ { sub(/>0</,">1<"); print; }'

Here's another way that prints out everything else:
Code:
awk 's { sub(/>0</,">1<");s=0; } /<key>ExcludeSimpleHostnames</ { s=1;} { print; }'

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.
# 4  
Old 09-30-2009
Write line to config file

Code:
<key>Proxies</key>
            <dict>
                <key>ExceptionsList</key>
                <array>
                    <string>*.local</string>
                    <string>169.254/16</string>
                </array>
                <key>ExcludeSimpleHostnames</key>
                <integer>1</integer>
                <key>FTPPassive</key>
                <integer>1</integer>
                <key>HTTPEnable</key>
                <integer>1</integer>
                <key>HTTPPort</key>
                <integer>8080</integer>
                <key>HTTPProxy</key>
                <string>192.168.171.4</string

I need to write
Code:
<key>ExcludeSimpleHostnames</key>
                <integer>1</integer>

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
Code:
<key>ExcludeSimpleHostnames</key>
                <integer>1</integer>

code inserted into all those sections as shown in the first example.

---------- Post updated at 04:30 PM ---------- Previous update was at 04:23 PM ----------

This is very urgent.. I'm sure it is some use of awk, the simpler the better.. I just need to insert two lines:

<key>ExcludeSimpleHostnames</key>
<integer>1</integer>

to be inserted the next line after </array> I suppose could work.
# 5  
Old 09-30-2009
Code:
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.

Last edited by varontron; 09-30-2009 at 05:54 PM.. Reason: caveat
# 6  
Old 10-01-2009
glev,

When you say "inserted", is it correct that you mean, "create a new file B with all the lines from A except changing those that I indicated" ? If that's the case, you simply need my second awk script from above. https://www.unix.com/emergency-unix-l...#post302357772
# 7  
Old 10-01-2009
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Read a lis, find items in a file from the list, change each item

Hello, I have some tab delimited text data, file: final_temp1 aname val NAME;r'(1,) 3.28584 r'(2,)<tab> NAME;r'(3,) 6.13003 NAME;r'(4,) 4.18037 r'(5,)<tab> You can see that the data is incomplete in some cases. There is a trailing tab after the first column for each incomplete row. I... (2 Replies)
Discussion started by: LMHmedchem
2 Replies

2. Shell Programming and Scripting

Change the naming convention of the output file

Hi Currently we have nmon running on our Red hat Linux server. The ouput file is now coming with the naming convention as "servername_160321_0010.nmon". The output file naming convention has to be changed as "nmon_servername_daily_2016.03.21_00.00.00" How can we do it ? Any suggestions... (10 Replies)
Discussion started by: newtoaixos
10 Replies

3. Shell Programming and Scripting

How to read each line from input file, assign variables, and echo to output file?

I've got a file that looks like this (spaces before first entries intentional): 12345650-000005000GL140227 ANNUAL HELC FEE EN 22345650-000005000GL140227 ANNUAL HELC FEE EN 32345650-000005000GL140227 ANNUAL HELC FEE EN I want to read through the file line by line,... (6 Replies)
Discussion started by: Scottie1954
6 Replies

4. Shell Programming and Scripting

Bash Script Help...search then read from file: change text and pipe back...

Hello, I am trying to make a bash script that can pull data from a file and then change one part of said data. I want to search by username and pull the full line. That way there is a way to replace just one part of that line then return it back to the file. My Data is stored like: ... (1 Reply)
Discussion started by: serverfull
1 Replies

5. Shell Programming and Scripting

Read file and for each line replace two variables, add strings and save output in another file

Hi All, I have a file, let's call it "info.tmp" that contains data like this .. ABC123456 PCX333445 BCD789833 I need to read "info.tmp" and for each line add strings in a way that the final output is put /logs/ua/dummy.trigger 'AAA00001.FTP.XXX.BLA03A01.xxxxxx(+1)' where XXX... (5 Replies)
Discussion started by: Andy_ARG
5 Replies

6. UNIX for Dummies Questions & Answers

How to read entire output from a file?

Hello- I am trying to view a file which is quite large. However, whenever I do 'cat (file name)' it shows me just the half.. I am using Putty to access my server. Also, is it possible to edit a file from a unix system on a 'Gedit for Windows" text editor? Thanks (7 Replies)
Discussion started by: DallasT
7 Replies

7. Shell Programming and Scripting

How to read file and only output certain content

Hi - I have a file containing data like :- cn=tommy,cn=users,c=uk passwordexpirydate=20100530130623z cn=jane,cn=users,c=uk passwordexpirydate=20100423140734z cn=michael,cn=users,c=uk passwordexpirydate=20100331020044z I want to end up with a file that looks like:-... (6 Replies)
Discussion started by: sniper57
6 Replies

8. Shell Programming and Scripting

Change file output format

I have a file which has following contents usmtnz-dinfsi19 62 61 18400 18800 99.7 usmtnz-dinfsi19 62 61 18400 18800 99.7 i want the o/p to be like date (7 Replies)
Discussion started by: fugitive
7 Replies

9. Shell Programming and Scripting

Change output if file is empty

I'm very new to writing scripts, so here is my problem...I have the following code already written (in perl) system "rm file2"; open(FILE2, ">file2"); open(MYINPUTFILE, "file"); while(<MYINPUTFILE>) { my($line) = $_; chomp($line); print file2 "$line\n"; print... (2 Replies)
Discussion started by: ddrew78
2 Replies

10. Shell Programming and Scripting

read file and output message

hi, I have a baby.txt file with two type of message: xxxxxxxx is missing xxxxxxxxxxx is not missing xxxx is missing xxxxxxxx is not missing xxxxxxxx is not missing For the above, I need to read file and get all "xxxx is missing" and write into baby_missing.txt. If no message "xxxxx is... (12 Replies)
Discussion started by: happyv
12 Replies
Login or Register to Ask a Question