comparing and replacing from one file to another


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting comparing and replacing from one file to another
# 1  
Old 06-07-2006
Power comparing and replacing from one file to another

Hi

I have this requirement:

I have FileA with this content:
-----------------------------------
DOWNLOAD_REPOSITORY=E:/download_repository/
DOWNLOAD_PROCESS_LOG=E:/download_repository/process_logs/
DOWNLOAD_ARTICLE_FOLDER=article
DOWNLOAD_SUBSIDARY_FOLDER=subsidiary

-----------------------------------

I have FileB with this content:
-----------------------------------
DOWNLOAD_REPOSITORY=/opt/download_repository/
DOWNLOAD_PROCESS_LOG=/opt/download_repository/process_logs/
-----------------------------------

I have to change the values of FileA with the values of FileB without changing the other values of FileA.

So after the program FileA shd look like ths:
-----------------------------------
DOWNLOAD_REPOSITORY=/opt/download_repository/
DOWNLOAD_PROCESS_LOG=/opt/download_repository/process_logs/
DOWNLOAD_ARTICLE_FOLDER=article
DOWNLOAD_SUBSIDARY_FOLDER=subsidiary
-----------------------------------






Can you help ?

many thanks
naren
# 2  
Old 06-07-2006
Code:
$ cat fA
DOWNLOAD_REPOSITORY=E:/download_repository/
DOWNLOAD_PROCESS_LOG=E:/download_repository/process_logs/
DOWNLOAD_ARTICLE_FOLDER=article
DOWNLOAD_SUBSIDARY_FOLDER=subsidiary
$ cat fB
DOWNLOAD_REPOSITORY=/opt/download_repository/
DOWNLOAD_PROCESS_LOG=/opt/download_repository/process_logs/
$ cat replace.sh 
awk '
BEGIN {
   FS="="
   i=0
   COMMAND="cat fA"
}

   {
   ARR[i,0]=$1
   ARR[i,1]=$2
   i++
   }

END {
   while (( COMMAND | getline ) > 0)
      {
      f=0
      for (b=0;b<=i;b++)
         {
         if ( $1 == ARR[b,0])
            {
            print ARR[b,0]"="ARR[b,1]
            f=1
            break
            }
         }
       if ( f == 0 )
          print $0
       }
}'<fB
$ replace.sh 
DOWNLOAD_REPOSITORY=/opt/download_repository/
DOWNLOAD_PROCESS_LOG=/opt/download_repository/process_logs/
DOWNLOAD_ARTICLE_FOLDER=article
DOWNLOAD_SUBSIDARY_FOLDER=subsidiary


Last edited by Klashxx; 06-07-2006 at 01:07 PM..
# 3  
Old 06-07-2006
Hi Klashxx,

Many Thanks for the solution it worked fine

But i tried the method below

a.txt - fileA
b.txt -fileB
tmp1 - output result

Is the below script good way to do this



#!/usr/bin/sh

rm tmp tmp1
for i in `cat a.txt`
do
repl1=`echo $i | cut -d"=" -f1`
if grep $repl1 b.txt
then
repls=`echo $i | tr '=' '-' | tr '/' '#'`
echo $repls repln=`grep $repl1 b.txt| tr '=' '-'| tr '/' '#'`
echo $repln echo `cat a.txt | grep $repl1 | sed s/$repl1/$repls/g >> tmp `
echo `cat tmp | grep $repl1 | sed s/$repls/$repln/g | cut -d"=" -f1 | tr '-' "="
| tr '#' "/" >> tmp1 `
else
echo $i >> tmp1
fi

done
# 4  
Old 06-07-2006
what have u tried so far ? Smilie Smilie Smilie Smilie Smilie Smilie
Code:
#! /usr/bin/ksh

awk -F"=" '{print $1" "$2}' filea | while read iden temp
do
  found=0
  awk -F"=" '{print $1" "$2}' fileb | while read line val
  do
     if [ $iden = $line ]
     then
        echo $iden"="$val
        found=1
        break
     fi
  done
  if [ $found -eq 0 ]
  then
     echo $iden"="$temp
  fi
done

exit 0

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replacing 12 columns of one file by second file based on mapping in third file

i have a real data prod file with 80+ fields containing 1k -2k records. i have to extract say 12 columns out of this which are sensitive fields along with one primary key say SEQ_ID (like DOB,account no, name, SEQ_ID, govtid etc) in a lookup file. i have to replace these sensitive fields in... (11 Replies)
Discussion started by: megh12
11 Replies

2. UNIX for Dummies Questions & Answers

Comparing 2 column of a file with the lines in another file

Hi I have 2 files which are tab delimited. file1 chr1 5 10 P1 KR4 chr1 10 20 1H LA1 R0 TA1 file2 P1 G6 13.27 0.2425 P1 KR4 18.79 0.3060 P1 DND1 19.44 0.2833 N1 DH1 0.99 1.08 1H R0 0.9 NA LA1 R0 90 0.9 TA1 KR4 1.8 8.9 TA1 R0 9.7 99I want to check whether first 2 columns in file2 (column... (1 Reply)
Discussion started by: raj_k
1 Replies

3. Shell Programming and Scripting

Script for Comparing directories and file from a text file

Hello all, I need to write a script which has following requirement: Need to read the filenames from text file and then search for the above read files in the required directory and if match found backup them in a backup folder. And also need to compare and verify whether the files in the... (7 Replies)
Discussion started by: saurau
7 Replies

4. Shell Programming and Scripting

sed -Replacing file path within .txt file

Hi, I am trying to use sed to replace a file path within all the .lay (.txt) files in a folder. I feel that this should be easy but I can't get it to work no matter what i try. I'm using cygwin. For a .txt file containing the below line I want to replace this file path with a new one. ... (1 Reply)
Discussion started by: carlr
1 Replies

5. Shell Programming and Scripting

Remove duplicate lines from first file comparing second file

Hi, I have two files with below data:: file1:- 123|aaa|ppp 445|fff|yyy 999|ttt|jjj 555|hhh|hhh file2:- 445|fff|yyy 555|hhh|hhh The records present in file1, not present in file 2 should be writtent to the out put file. output:- 123|aaa|ppp 999|ttt|jjj Is there any one line... (3 Replies)
Discussion started by: gani_85
3 Replies

6. Shell Programming and Scripting

sed command for copying the contents of other file replacing it another file on specifc pattern

We have 2 file XML files - FILE1.XML and FILE2.xml - we need copy the contents of FILE1.XML and replace in FILE2.xml pattern "<assignedAttributeList></assignedAttributeList>" FILE1.XML 1. <itemList> 2. <item type="Manufactured"> 3. <resourceCode>431048</resourceCode> 4. ... (0 Replies)
Discussion started by: balrajg
0 Replies

7. Shell Programming and Scripting

Comparing rows in same file and writing the result in new file

Help needed... Can you tell me how to compare the last two couple entries in a file and print their result in new file..:confused: I have one file Check1.txt \abc1 12345 \abc2 12327 \abc1 12345 \abc2 12330 I want to compare the entries in Check1 and write to... (1 Reply)
Discussion started by: kichu
1 Replies

8. Shell Programming and Scripting

Comparing two files and replacing fields

I have two files with ids and email addresses. File 2 cotains a subset of the records in file 1. The key field is the first field containing the id. file 1: 123|myadr@abc.com 456|myadr2@abc.com 789|myadr3@abc.com file 2: 456|adr456@xyz.com Where the record appears in the second... (3 Replies)
Discussion started by: tltroy
3 Replies

9. Shell Programming and Scripting

Need help in comparing a file with log file: perl code

Dear Members, I need a perl code: 1. Which will open first file with two columns separated by tab: 37 Distribution and seasonal variation of trace metals 85 Seasonal variability of the mixed layer in the central Bay 99 Dynamics of transparent exopolymeric particles (TEP) 103 Bacterial... (0 Replies)
Discussion started by: srsahu75
0 Replies

10. Shell Programming and Scripting

comparing the name of the file

hi, i have to check the name of file in other directory which contains number of files to check for duplicte file name avaliable or not. thanks (2 Replies)
Discussion started by: ravi214u
2 Replies
Login or Register to Ask a Question