Comparing lines of two different files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Comparing lines of two different files
# 1  
Old 05-16-2011
Data Comparing lines of two different files

Hello,

Please help me with this problem if you have a solution.

I have two files:
<file1> : In each line, first word is an Id and then other words that belong to this Id

Code:
piMN-1 abc pqr xyz py12
niLM y12 FY4 pqs
fiRLym F12 kite red

<file2> : same as file1, but can have extra lds and also same Ids with different words belonging to that Id

Code:
niLM FY4 gt45
piMN-1 pqr ss3 abc
aiPQ44 asf ggy-9

I need an output file which checks for all the Ids of file2 in file1 and if matches, it checks for the associated words and prints only the matched words (order doesn’t matter). If the Id of file2 does not exist in file1 or do not match with any of its words, then print only the Id.

<file3>

Code:
niLM FY4 
piMN-1 pqr abc
aiPQ44

I’ll be really thankful to you for your help.
Moderator's Comments:
Mod Comment
Please use code tags when posting data and code samples!

Last edited by vgersh99; 05-16-2011 at 03:23 PM.. Reason: code tags, please!
# 2  
Old 05-16-2011
Here is one way of doing it:

Code:
#!/usr/bin/ksh
typeset -i mCnt
while read mLine
do
  mOutLine=""
  for mFld in ${mLine}
  do
    if [[ "${mOutLine}" = "" ]]; then
      mOutLine=${mFld}
      mTag=${mFld}
      continue
    fi
    mCnt=$(egrep -c "${mTag}.*${mFld}" target_file)
    if [[ ${mCnt} -ne 0 ]]; then
      mOutLine=${mOutLine}' '${mFld}
    fi
  done
  echo $mOutLine
done < source_file

This User Gave Thanks to Shell_Life For This Post:
# 3  
Old 05-16-2011
Code:
awk 'NR == FNR {
  for (i = 1; ++i <= NF;)
    k[$1, $i] 
  next
  }
{  
  split(x, ok)
  for (i = 1; ++i <= NF;) {
    if (($1, $i) in k) 
      ok[$1] = ok[$1] ? ok[$1] FS $i : $i      
    }
  print $1, ok[$1]  
  }' file1 file2

With GNU awk you can use delete ok instead of split(x, ok).
This User Gave Thanks to radoulov For This Post:
# 4  
Old 05-17-2011
@Shell_Life your script contains some mistakes.
and there is a solution without awk with justdoit..Smilie

Code:
# cat file1
piMN-1 abc pqr xyz py12
niLM y12 FY4 pqs
fiRLym F12 kite red

Code:
# cat file2
niLM FY4 gt45
piMN-1 pqr ss3 abc
aiPQ44 asf ggy-9

Code:
# ./justdoit.sh
niLM FY4
piMN-1 pqr abc
aiPQ44

Code:
#!/bin/bash
while read -r linef; do
 la=();
 m1=$(echo $linef|sed -n "s/\([^ ]*\).*/\1/p ")
 while read -r lineff; do
  m2=$(echo $lineff|sed -n 's/\([^ ]*\).*/\1/p ')
if [[ "$m1" = "$m2" ]] ; then
  for i in $(echo "$lineff $linef"|sed 's/ /\n/g')
   do
   la=(${la[@]} $i)
   done
x=0
y=$(echo ${#la[@]} )
while [ $(( y -= 1 )) -gt -1 ]
do
  for i in ${la[@]}
   do
   if [ ${la[x]} = $i ] ; then
   ((c++))
   fi
   done
  if [ $c -ge 2 ] ; then
  added=(${added[@]} ${la[x]})
  fi
  for i in ${added[@]}
   do
   if [ $i = ${la[x]} ] ; then
   ((adc++))
   fi
  done
  if [[ $adc -gt 1 ]] ; then
   ar=(${ar[@]} ${la[x]})
  fi
  adc=0 ;((x++)); c=0
done
 echo ${ar[@]}
 ne=0
else
 ne=1
fi
done<file1
if [ $ne -eq 1 ] ; then
 if [[ ${ar[0]} != $m1 ]] ; then
  echo $m1
 fi
fi
ar=();eq=1
done<file2

regards
ygemici
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Comparing multiple lines in same file

Hello, I would like to write a /bin/ksh script to manipulate a file and compare its contexts. Comparing lines 1 & 2, 3 & 4, 5 & 6, and so forth until the end of the file. This is what I would like the script to compare (using line 1 & 2 as an example): 1. Verify if the last column in line 1 is... (10 Replies)
Discussion started by: seekryts15
10 Replies

2. UNIX for Dummies Questions & Answers

Comparing lines within a word list

Hello all- New to this forum, and relatively new to using grep at the Terminal command line to work with regular expressions. I've got a background in math and some programming experience, so it's not been too difficult to learn the basics of searching through my word lists for particular types of... (13 Replies)
Discussion started by: dtalvacchio
13 Replies

3. Shell Programming and Scripting

Comparing 2 text files & downloading a file if the last lines are different

Hello I'm having a little difficulty in writing a shell script for a few simple tasks. First I have two files "file1.txt" and "file2.txt" and I want to read and compare the last line of each file. The files look like this. File1.txt File2.txt After comparing the two lines I would... (2 Replies)
Discussion started by: RustikGaming
2 Replies

4. UNIX for Dummies Questions & Answers

Comparing lines of data

Total UNIX Rookie, but I'm learning. I have columns of integer data separated by spaces, and I'm using a Mac terminal. What I want to do: 1. Compare "line 1 column 2" (x) to "line 2 column 2" (y); is y-x>=100? 2. If yes, display difference and y's line number 3. If no, increment x and y by... (9 Replies)
Discussion started by: markymarkg123
9 Replies

5. UNIX for Dummies Questions & Answers

Comparing two files and count number of lines that match

Hello all, I always found help for my problems using the search option, but this time my request is too specific. I have two files that I want to compare. File1 is the index and File2 contains the data: File1: chr1 protein_coding exon 500 600 . + . gene_id "20532";... (0 Replies)
Discussion started by: DerSeb
0 Replies

6. Shell Programming and Scripting

comparing lines in file

i have 2 files and i want to compare i currently cat the files and awk print $1, $2 and doing if file1=file2 then fail, else exit 0 what i want to do is compare values, with column 1 being a reference i want to compare line by line and then still be able to do if then statement to see if worked... (1 Reply)
Discussion started by: sigh2010
1 Replies

7. Shell Programming and Scripting

Omit Blank Lines while comparing two files.

Hello All, I am writting file comparison Utility and I have encountered such a senario where there are 2 files such as follows- 1#!/usr/local/bin/python 2 import gsd.scripts.admin.control.gsdPageEscalate 3.gsd.scripts.admin.control.gsdPageEscalate.main() 1 #!/usr/local/bin/python... (10 Replies)
Discussion started by: Veenak15
10 Replies

8. Shell Programming and Scripting

Comparing 2 files and return the unique lines in first file

Hi, I have 2 files file1 ******** 01-05-09|java.xls| 02-05-08|c.txt| 08-01-09|perl.txt| 01-01-09|oracle.txt| ******** file2 ******** 01-02-09|windows.xls| 02-05-08|c.txt| 01-05-09|java.xls| 08-02-09|perl.txt| 01-01-09|oracle.txt| ******** (8 Replies)
Discussion started by: shekhar_v4
8 Replies

9. Shell Programming and Scripting

Comparing two files and appending only missing lines.

Hi All, I am a newbie to Shell scripting. Please help me with the Following problem, 1. I have two files with the same name in different locations in the same machine. Eg: /root/testfolder/a ---- location 1 /tmp/testfolder/a ----- location 2 2. I want to compare the files in... (5 Replies)
Discussion started by: Karthick333031
5 Replies

10. Shell Programming and Scripting

comparing lines from 2 files

Hi Friends, I have 2 files A and B . I want to compare the 3rd line of file A and B . (I dont want to compare the 2 files, using diff or cmp). I just want to know whether 3rd line of A matches the 3 rd line of B. Can anybody share their knowledge on the same? Thanks , Vijaya (12 Replies)
Discussion started by: vijaya2006
12 Replies
Login or Register to Ask a Question