conjunction two files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting conjunction two files
# 1  
Old 08-10-2008
conjunction two files

I need to your help. I want write a script search for rows in file1 if exist in file2 it will print rows from file2 else it will print rows from file1 with out any duplicate


Example:
file1

4599207
47182983
46900870
21015724
63358010
222222
33333


file2

4599207 ,20070914 ,abc
47182983,20080513 ,abc
46900870,20080513 ,abc
21015724,20080617 ,abc
63358010,20080520 ,abc
25985516,20080624 ,abc
92829623,20080712 ,abc
58015436,20080629 ,abc
53139549,20080629 ,abc
21644907,20080617 ,abc
82325402,20080707 ,abc
82325406,20080707 ,abc
82325404,20080707 ,abc
82325405,20080707 ,abc





Output

4599207 ,20070914 ,abc
47182983,20080513 ,abc
46900870,20080513 ,abc
21015724 20080617 ,abc
63358010,20080520 ,abc
222222
33333
# 2  
Old 08-10-2008
Code:
while read rec
do
grep -f "$rec" file1
if [[ $? -ne 0 ]] ; then
  echo "$rec"
fi
done < file2

# 3  
Old 08-10-2008
Try (and adapt) the following script (assumes that records in file are fixed length) :
Code:
awk '
BEGIN {
   FS = OFS = ",";
}
NR==FNR {
   datas[$1] = $0;
   next;
}
{
   if ($1 in datas)
      print datas[$1]
   else
      print $0;
}
' file2 file1

If records in file1 aren't fixed length :
Code:
awk '
BEGIN {
   FS = OFS = ",";
}
NR==FNR {
   sub(/[[:space:]]*/, "", $1)
   datas[$1] = $0;
   next;
}
{
   sub(/[[:space:]]*/, "", $1)
   if ($1 in datas)
      print datas[$1]
   else
      print $0;
}

' kmu2.dat kmu1.dat

Jean-Pierre.
# 4  
Old 08-10-2008
Or:
Code:
awk -F, 'NR==FNR{a[$1]=$0;next}$0 in a{print a[$0];next}1' file2 file1

Regards
# 5  
Old 08-12-2008
Thanks
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Automate splitting of files , scp files as each split completes and combine files on target server

i use the split command to split a one terabyte backup file into 10 chunks of 100 GB each. The files are split one after the other. While the files is being split, I will like to scp the files one after the other as soon as the previous one completes, from server A to Server B. Then on server B ,... (2 Replies)
Discussion started by: malaika
2 Replies

2. Shell Programming and Scripting

Append string to all the files inside a directory excluding subdirectories and .zip files

Hii, Could someone help me to append string to the starting of all the filenames inside a directory but it should exclude .zip files and subdirectories. Eg. file1: test1.log file2: test2.log file3 test.zip After running the script file1: string_test1.log file2: string_test2.log file3:... (4 Replies)
Discussion started by: Ravi Kishore
4 Replies

3. Shell Programming and Scripting

How to create zip/gz/tar files for if the files are older than particular days in UNIX or Linux?

I need a script file for backup (zip or tar or gz) of old log files in our unix server (causing the space problem). Could you please help me to create the zip or gz files for each log files in current directory and sub-directories also? I found one command which is to create gz file for the... (4 Replies)
Discussion started by: Mallikgm
4 Replies

4. UNIX for Dummies Questions & Answers

Nohup in conjunction with time

hi, if I exectute "nohup time ls -1" I get the following output $ nohup time ls -1 file_1 file_2 file_3 file_4 file_5 0.000u 0.001s 0:00.00 0.0% 0+0k 0+0io 0pf+0w This is all OK. But if I want to capture this whole output in to a text file I would want to use something like ... (1 Reply)
Discussion started by: BearCheese
1 Replies

5. Shell Programming and Scripting

need a shell script to extract the files from source file and check whether those files existonserve

Hi, I am new to shell scripting.Please help me on this.I am using solaris 10 OS and shell i am using is # echo $0 -sh My requirement is i have source file say makefile.I need to extract files with extensions (.c |.cxx |.h |.hxx |.sc) from the makefile.after doing so i need to check whether... (13 Replies)
Discussion started by: muraliinfy04
13 Replies

6. Shell Programming and Scripting

Problem with date in conjunction with cut?

I got rather bored so i decided to create a script that creates a countdown, and shows hours:minutes:seconds till that time. It works fine until the seconds of the actual time reaches 8, then it tries to use it to work out the difference as in "SECONDDIFF=$" Here's my code where I get the... (12 Replies)
Discussion started by: DuskFall
12 Replies

7. Shell Programming and Scripting

How to go about Using a "Validate an IP script" in conjunction with Logfile?

Hi, so I have been trying to write a shell script to go through a log file and through that, generate another file with all the Valid IP addresses it finds. So there's the complication that there could be incomplete or invalid data which would disqualify it from making my "Valid IPs" file I need... (3 Replies)
Discussion started by: shellcow
3 Replies

8. Shell Programming and Scripting

Missing conjunction

Hi Gurus, I have prepared a script to find the log file based on a date range defined in one of the environment files, archive the logs files and move them to a particular directory. Below is the script: . /home/.profile . /home/.inf_env logfile=$scripts_path/Logs/file_archive1.log... (17 Replies)
Discussion started by: svajhala
17 Replies

9. Shell Programming and Scripting

How to retrieve all the linked script files/ctl files/sql files?

Hi I am going to migrate our datawarehouse system from HP Tru 64 Unix to the Red Hat Linux. Inside the box, it is running around 40 cron jobs; inside each cron job, it is calling other shell script files, and the shell script files may again call other shell script files or ctl files(for... (1 Reply)
Discussion started by: franksubramania
1 Replies
Login or Register to Ask a Question