-2

I have a two files, file1 and file2.

In file1 contents are:

ABC_DEC_EDC=ON
WER_QSD_RCS=ON

file2 contents are:

TRD_OIY_REC=ON
YUH_PON_UYT=ON
WER_QSD_RCS=OFF

I have to check line by line in file2.

First

if ABC_DEC_EDC=ON is not present in file2 then add to file2.

Second

In first file SAX_IUY_TRE=OFF is there with OFF but in file2 SAX_IUY_TRE=ON with ON; In this case I just want to update as with the file only OFF.

Example: SAX_IUY_TRE=OFF

All updates new updates happen in file2 only.

Output should be:

ABC_DEC_EDC=ON
WER_QSD_RCS=ON
    WER_RTC_YTC=ON
WER_QSD_RCS=OFF
  • output should be : 1)ABC_DEC_EDC=ON 2)WER_QSD_RCS=ON 3)ZXC_POY_YTR=OFF 4)ZXC_OPI_GHF=ON 5)SAX_IUY_TRE=OFF 6)TRD_OIY_REC=ON 7)YUH_PON_UYT=ON 8)RTF_CDE_QWE=OFF 9)TYF_QWE_UTC=OFF 10)WER_RTC_YTC=ON 11)WER_QSD_RCS=OFF – suman Oct 18 '17 at 11:34
  • format your question and show your awk --version (first line) – RomanPerekhrest Oct 18 '17 at 11:35
  • Does your line including lines like 1), 2) or those are just for clarification? What about the numbers in output? And what happened for spaces in line3,5 in file1 and 7 infile2? – αғsнιη Oct 18 '17 at 12:26
  • Numbers are given in a file1 and file2 for just clarification only . Same as in output as well for clarification only. Please ignore the spaces . Its typo – suman Oct 18 '17 at 12:41
  • last question/clarification: What about line WER_QSD_RCS=OFF? Where it's same in both files but different value ON/OFF? and what about if this line ABC_DEC_EDC=OFF exist in file2 with OFF status? Basically you want take ON/OFF from file1 if it's match .....= part in file2? right? – αғsнιη Oct 18 '17 at 13:11
0

sh implementation.

#!/bin/sh

while read -r LINE
do
    setting=$(echo $LINE | sed 's/=.*//')
    switch=$(echo $LINE | sed 's/.*=//')

    found=$(grep "$setting" file2)
    if [ -z "$found" ]
    then
        echo $LINE >> file2
    else
        if [ "$switch" != "$(echo $found | sed 's/.*=//')" ]
        then
            sed -i "s/\($setting\).*/\1=$switch/" file2
        fi
    fi
done < file1

The script will flip the value of the settings found on both files that do not match.

  • I Tried this.Output is TRD_OIY_REC=ON YUH_PON_UYT=ON RTF_CDE_QWE=OFF TYF_QWE_UTC=OFF WER_RTC_YTC=ON SAX_IUY_TRE=ON WER_QSD_RCS=OFF TRD_OIY_REC=ON YUH_PON_UYT=ON RTF_CDE_QWE=OFF TYF_QWE_UTC=OFF WER_RTC_YTC=ON SAX_IUY_TRE=ON WER_QSD_RCS=OFF TRD_OIY_REC=ON YUH_PON_UYT=ON RTF_CDE_QWE=OFF TYF_QWE_UTC=OFF WER_RTC_YTC=ON SAX_IUY_TRE=ON WER_QSD_RCS=OFF TRD_OIY_REC=ON YUH_PON_UYT=ON RTF_CDE_QWE=OFF TYF_QWE_UTC=OFF WER_RTC_YTC=ON SAX_IUY_TRE=ON WER_QSD_RCS=OFF TRD_OIY_REC=ON YUH_PON_UYT=ON RTF_CDE_QWE=OFF TYF_QWE_UTC=OFF WER_RTC_YTC=ON SAX_IUY_TRE=ON WER_QSD_RCS=OFF – suman Oct 20 '17 at 6:19
  • #!/bin/sh file1="/tmp/output1.txt" file2="/tmp/livefile1.txt" while read LINE do setting=$(echo $LINE | sed 's/=.*//') switch=$(echo $LINE | sed 's/.*=//') found=$(grep "$setting" $file2) if [ -z "$found" ] then echo $LINE >> $file2 else if [ "$switch" != "$(echo $found | sed 's/.*=//')" ] then sed "s/($setting).*/\1=$switch/" $file2 fi fi done < $file1 – suman Oct 20 '17 at 6:20
  • Output is : just printing the file2 content – suman Oct 20 '17 at 6:22
  • That's because you removed the -i parameter from the sed command. – cesarv Oct 20 '17 at 6:34
0

It can be made in bash.

#! /bin/bash
file1="/tmp/output1.txt"
file2="/tmp/livefile1.txt"
cat $file1 | while read LINE; do
    KEY=${LINE%=*}
    CURRENT=$(grep $KEY= $file2)
    if [ -z "$CURRENT" ]; then  # if $CURRENT is empty
        echo NOT found $KEY in $file2, add it
        echo $LINE >> $file2
    else
        if [ "$LINE" != "$CURRENT" ]; then
            echo Found $KEY in $file2 and state has changed
            sed -i -e "s/^$KEY=.*\$/$LINE/" $file2
        fi
    fi
done

And run the command:

./update.bash

Output to screen:

NOT found ABC_DEC_EDC in /tmp/livefile1.txt, add it
Found WER_QSD_RCS in /tmp/livefile1.txt and state has changed
NOT found ZXC_POY_YTR in /tmp/livefile1.txt, add it
NOT found ZXC_OPI_GHF in /tmp/livefile1.txt, add it
Found SAX_IUY_TRE in /tmp/livefile1.txt and state has changed

The script read all lines from $file1. KEY is set to the part before = which is the key. Then grep search for the key in $file2 and add if not found. If the key is found, then it checked if the state has changed, and update it with sed if it has changed.

Edit: Changed to look more like the questionnaire will use it.

  • REPLY%=* what is this and how it works ? I tried to execute this and script never ended after 10 mins also .#!/bin/bash file1="/tmp/output1.txt" file2="/tmp/livefile1.txt" while read; do KEY=${REPLY%=*} if ! grep -q $KEY $file2; then # NOT found $KEY in file2, add it echo $REPLY >> $file2 else # found $KEY in file2, has state changed? if ! grep -q $REPLY $file2; then # State has changed sed -i -e "s/^$KEY=.*\$/$REPLY/" $file2 fi fi done – suman Oct 20 '17 at 6:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.