need to print the missing lines in 1 file from 1 file via script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting need to print the missing lines in 1 file from 1 file via script
# 1  
Old 06-02-2010
Question need to print the missing lines in 1 file from 1 file via script

Hi,

I am writting a script which will take all the files present in 2 directories. (the 2 directories have the same files with same name but some different content)
i need 2 compare 2 files and then copy the contents of file1 to file2. the contents which are missing in file1 and nothing from file 1 shud be deleted.

see below:-

Code:
File 1 contents :-                               File 2 Contents:-
DE_DAS
 
m3=DE_DS|OMER01|3                                   m3=DE_DS|OMER01|3  
m3=DE_DS|OMER02|4                                   m3=DE_DS|OMER02|4
m3=DE_DS|OMER03|5                                   m3=DE_DS|OMER03|5
                                                                m3=DE_DS|OMER06|7
                                                                m3=DE_D1S|OMER02|25

and the result of the file shud be like this :-

Code:
File 1 contains after the operation is done:- 
DE_DAS
m3=DE_DS|OMER01|3 
m3=DE_DS|OMER02|4
m3=DE_DS|OMER03|5
m3=DE_DS|OMER03|5
m3=DE_DS|OMER06|7
m3=DE_D1S|OMER02|25

Can anyone please help me with this. kinda need it urgently...

Last edited by dazdseg; 06-02-2010 at 06:35 AM..
# 2  
Old 06-02-2010
You can use similar to following :

Code:
 
#!/usr/bin/bash
 
printf "ENTER THE FULL NAME OF FIRST DIRECTORY : "
read d1
echo
printf "ENTER THE FULL NAME OF SECOND DIRECTORY : "
read d2
echo
ls -ld $d1 $d2 >/dev/null  2>&1 || echo "PLEASE CHECK... FULL PATH NAME OF EITHER DIRECTORY 1 OR DIRECTORY 2 IS NOT PROVIDED..."
ls -ld $d1 $d2 >/dev/null  2>&1 || echo "PLEASE RERUN THE SCRIPT...."
echo
ls -ld $d1 $d2 >/dev/null  2>&1 || exit 2
for i in `find $d1/* -prune -type f|xargs`
do
for j in `find $d2/* -prune -type f|xargs`
do
A=`basename $i`
B=`basename $j`
if [ "$A" == "$B" ] ; then
echo "$d1/out.txt"
echo $d1 $d2 $i $j
(comm $i $j|awk '{print $1,$2,$3}'|uniq) > $d1/out.txt
mv $d1/out.txt $i
fi
done
done

SmilieSmilie
This User Gave Thanks to Reboot For This Post:
# 3  
Old 06-02-2010
Can you post the file contents separately?
In your output there is one m3=DE_DS|OMER03|5 too much, right?
# 4  
Old 06-02-2010
its not working..... errrr

i thought it wud work thats why i said thanks. bt its comparing in a wrong way and i m getting below error....

Code:
./do_it.ksh[14]: dead.txt: bad number
mv: d1/out.txt: cannot access: No such file or directory
./do_it.ksh[14]: dead.txt: bad number
mv: d1/out.txt: cannot access: No such file or directory
mv: d1/out.txt: cannot access: No such file or directory
./do_it.ksh[14]: do_it.ksh: bad number
mv: d1/out.txt: cannot access: No such file or directory
./do_it.ksh[14]: alive.txt: bad number
mv: d1/out.txt: cannot access: No such file or directory

---------- Post updated at 06:53 AM ---------- Previous update was at 06:15 AM ----------

In your output there is one m3=DE_DS|OMER03|5 too much, right?
yeh bt all the contents that r uniq in file and file 2
# 5  
Old 06-02-2010
Could you post the exect script which you are using...
Because its working for me...SmilieSmilie
# 6  
Old 06-02-2010
i have done a lil bit of modification. i am not checking this for different directories. i have pasted both the files in same directory and doing it ... see the below script. (i m pasing my script as u said.. sorry for late response)

Code:
#!/usr/bin/ksh

echo  "enter the full name of the  first file:"
read f1
echo  "enter the full name of the  second file:"
read f2

for i in `find $f1 -prune -type f| xargs`
do
for j in `find $f2 -prune -type f | xargs `
do
A=`basename $i`
B=`basename $j`
if [ "$A" -eq "$B" ]
then
(comm $i $j|awk '{print $1,$2}'|uniq) > out.txt
#(cmp $i $j | awk '{print $1, $2}' | uniq ) >log.txt
fi
mv out.txt dodo.txt
done
done

i even tried using it with cmp but cudnt go anywhere.. i am getting the below error.

Code:
./do_it.ksh[14]: CAM.cfg: bad number
mv: out.txt: cannot access: No such file or directory

i am also pasting both the files i am using ..

Code:
file1  - CAM.cfg
[DS] - DOSE
vmsa3=DS101_DS|OMER101|3
vmsa3=DSA101_DS|OMER201|9
vmsa3=DSA102_DS|OMER301|12
vmsa3=DSA102_DS|OMER401|11

[DS2] - DOSE
vmsa4=DSA2_DS|OMER01|2
vmsa4=DSA2_DS|OMER02|3

[DSA300] - DOSE
vmsa4=DSA300_DS|OMER01|3
vmsa4=DSA300_DS|OMER02|4
vmsa4=DSA300_DS|OMER03|5
vmsa4=DSA300_DS|OMER05|7

[VAL0] - VALID
vmsa4=VAL0_DS|OMER01|3
m3vmsa4=VAL00_DS|OMER02|4
m3vmsa4=VAL00_DS|OMER04|6

[INT00] - INT
pps02=INT01_DS|OMER101|3
pps02=INT01_DS|OMER401|9
pps02=INT01_DS|OMER501|11

[VT1] - MAP
vmsa2=VT10_DS|OMER102|4
vmsa2=VT11_DS|OMER202|6
vmsa2=VT12_DS|OMER302|8
vmsa2=VT12_DS|OMER402|9
vmsa2=VT13_DS|OMER501|11
vmsa2=VT14_DS|OMER601|12

attaching my 2nd file ........

the 2nd file is RAM1
Code:
vmsa4=VAL0_DS|LOG_DB|1
vmsa4=VAL0_DS|MIN_DB|2
vmsa4=VAL0_DS|OMER01|3
vmsa4=VAL0_DS|OMER02|4
vmsa4=VAL0_DS|OMER03|5
vmsa4=VAL0_DS|OMER04|6
vmsa4=VAL0_DS|OMER90|90
vmsa3=DSA10_DS|LOG|1
vmsa3=DSA10_DS|MIN_DB|2
vmsa3=DSA10_DS|OMER101|3
vmsa3=DSA10_DS|OMER201|9
vmsa3=DSA10_DS|OMER301|12
vmsa3=DSA1_DS|OMER401|11
vmsa3=DSA10_DS|OMER4401|90
vmsa4=DSA2_DS|LOG|1
vmsa4=DSA2_DS|OMER01|2
vmsa4=DSA2_DS|OMER01|3
vmsa4=DSA30_DS|LOG_DB|1
vmsa4=DSA30_DS|MIN_DB|2
vmsa4=DSA30_DS|OMER01|3
vmsa4=DSA30_DS|OMER02|4
vmsa4=DSA30_DS|OMER03|5
vmsa4=DSA30_DS|OMER04|6
vmsa4=DSA30_DS|OMER05|7
vmsa4=DSA30_DS|OMER90|90
pps02=INT0_DS|LOG|1
pps02=INT0_DS|MIN|2

please let me know if there is anything i am missing in execution...
# 7  
Old 06-03-2010
Following are the assumptions for the new script :
Code:
1) You are providing the absolute path of both the FILES.

Now modify your script as follow :

Code:
#!/usr/bin/ksh
echo  "enter the full name of the  first file:"
read f1
echo  "enter the full name of the  second file:"
read f2
comm $f1 $f2|awk '{print $1,$2,$3,$4}' > out.txt
mv out.txt dodo.txt

Please let us know if still problem...SmilieSmilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Script to search every file in a directory and print last few lines

Hi everyone, I need to write a script to search a directory, output the name of a file to an ouput file and print the last few lines of the files to the output file such that I would have something like this: FILE1: LINE LINE LINE FILE2: LINE LINE LINE FILE3: LINE LINE LINE... (2 Replies)
Discussion started by: mojoman
2 Replies

2. Shell Programming and Scripting

Print Missing Header Name from File.

HI Guys, I have file A.txt ID,L1,L2,L3,L4 1A,2a,33a,44b,55c 2A,10a,14a,15b,16c File B.txt ID L1 L4 L5 Output:- ID,L1,L4,L5 (2 Replies)
Discussion started by: pareshkp
2 Replies

3. UNIX for Dummies Questions & Answers

Find the missing file and print

Hi , i will be getting 24 files for one day with a formate like 20131028_01 - 20131028_24 kind of ,i am trying to write a shell script to count the number of files and if the count is not equal to 24 print the missing files names for A in `seq 0 23`; do ls *20131024_`printf "%02d" $A`*;... (3 Replies)
Discussion started by: vikatakavi
3 Replies

4. UNIX for Dummies Questions & Answers

Adding missing lines in file

Dear all, I have a file with two columns - the first column is increasing every 50, the second column is just count (e.g. 5). However, when count is zero, no line is present. Sample: How can I change the file so as to include lines with zero count? e.g. in the previous file to put... (4 Replies)
Discussion started by: TheTransporter
4 Replies

5. UNIX for Dummies Questions & Answers

Adding missing lines in file

Dear all, I have a file with two columns - the first column is increasing every 50, the second column is just count (e.g. 5). However, when count is zero, no line is present. Sample: 1950 7 2000 14 2050 7 2100 13 2150 10 2200 9 2250 7 2300 8 2350 7... (1 Reply)
Discussion started by: TheTransporter
1 Replies

6. Shell Programming and Scripting

File Comparison: Print Lines not present in another file

Hi, I have fileA.txt like this. B01B02 D0011718 B01B03 D0012540 B01B04 D0006145 B01B05 D0004815 B01B06 D0012069 B01B07 D0004064 B01B08 D0011988 B01B09 D0012071 B01B10 D0005596 B01B11 D0011351 B01B12 D0004814 B01C01 D0011804 I want to compare this against another file (fileB.txt)... (3 Replies)
Discussion started by: genehunter
3 Replies

7. UNIX for Dummies Questions & Answers

Compare 2 files print the lines of file 2 that contain a string from file 1

Hello I am a new unix user, and I have a work related task to compare 2 files and print all of the lines in file 2 that contain a string from file 1 Note: the fields are in different columns in the files. I suspect the is a good use for awk? Thanks for your time & help File 1 123 232 W343... (6 Replies)
Discussion started by: KevinRidley
6 Replies

8. Shell Programming and Scripting

join based on line number when one file is missing lines

I have a file that contains 87 lines, each with a set of coordinates (x & y). This file looks like: 1 200.3 -0.3 2 201.7 -0.32 ... 87 200.2 -0.314 I have another file which contains data that was taken at certain of these 87 positions. i.e.: 37 125 42 175 86 142 where the first... (1 Reply)
Discussion started by: jackiev
1 Replies

9. Shell Programming and Scripting

Strings from one file which exactly match to the 1st column of other file and then print lines.

Hi, I have two files. 1st file has 1 column (huge file containing ~19200000 lines) and 2nd file has 2 columns (small file containing ~6000 lines). ################################# huge_file.txt a a ab b ################################## small_file.txt a 1.5 b 2.5 ab ... (4 Replies)
Discussion started by: AshwaniSharma09
4 Replies

10. UNIX for Dummies Questions & Answers

Extracting lines present in one file but missing in another using Perl

Hey I have an input file containing a list of numbers like: U01120.CDS.1 D25328.CDS.1 X15573.CDS.1 K03515.CDS.1 L44140.CDS.10 U24183.CDS.1 M97347.CDS.1 U05259.CDS.1 And another input file containing results created on the basis of the above input: G6PT_HUMAN U01120.CDS.1 -1.9450 3.1706... (1 Reply)
Discussion started by: Banni
1 Replies
Login or Register to Ask a Question