LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 07-11-2019, 03:30 PM   #1
deep_kol
LQ Newbie
 
Registered: Jul 2017
Posts: 5

Rep: Reputation: Disabled
Smile Compare 2 columns in two files and then put matching data in a file


Hi Folks ,
Need your help in unix to solve this issue .

I have 2 data files a.txt (csv) and b.txt (fixed length ) . Need to search from b.txt for which the 1st 2 columns matches from both the files and the output is kept in a new file .


a.txt
------------
1,b,20
2,c,40


b.txt
------------------
1 b Apple
2 d Jack
3 c Fruits
2 c Adam
4 c Adam



output c.txt

1 b Apple
2 c Adam
 
Old 07-11-2019, 03:39 PM   #2
Turbocapitalist
Senior Member
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 4,057
Blog Entries: 3

Rep: Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958
The join utility will do that but you'll have to standarized the files first so that the delimiter is the same for both.
 
2 members found this post helpful.
Old 07-11-2019, 07:04 PM   #3
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: FreeBSD/Slackware-14.2+/ArcoLinux
Posts: 9,002

Rep: Reputation: 1883Reputation: 1883Reputation: 1883Reputation: 1883Reputation: 1883Reputation: 1883Reputation: 1883Reputation: 1883Reputation: 1883Reputation: 1883Reputation: 1883
I did not know about join,
Code:
#!/bin/bash

 
tempfile=$HOME/workingtemp/tempfile
save1=$HOME/workingtemp/a.txt
save2=$HOME/workingtemp/b.txt
saveA3=$HOME/workingtemp/ResultsA
saveB3=$HOME/workingtemp/ResultsB


formatListings()
{	
   sed -i 's/,/ /g' "$save1"
   join "$save1" "$save2" > "$tempfile"
   cat "$tempfile"
}
formatListings

#just kept these in , they might come in handy for you. 

FindFilesNotProccesed2()
{	echo "FindFilesNotProccesed2"
	while read dirs 
	do
		echo "$dirs"
		echo   "$dirs" >> "$saveB3" 
		#23 what matches between two list
	done < <(comm -23 <(sort $save1) <(sort $save2))
}
FindFilesNotProccesed2


FindFilesNotProccesed1()
{	echo "FindFilesNotProccesed1"
	while read dirs 
	do
		echo "$dirs"
		echo   "$dirs" >> "$saveA3" 
		#13 what does not match between two list		
	done < <(comm -13 <(sort $save1) <(sort $save2))
}
FindFilesNotProccesed1
you'll should work out the rest of it. You also should see what I mean by that if you put this to work for you. cheers!

Last edited by BW-userx; 07-11-2019 at 07:15 PM.
 
Old 07-12-2019, 03:06 AM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,715

Rep: Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034
Looks like homework that should have been attempted before requesting help. Please show your efforts in future
 
1 members found this post helpful.
Old 07-12-2019, 06:47 AM   #5
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: FreeBSD/Slackware-14.2+/ArcoLinux
Posts: 9,002

Rep: Reputation: 1883Reputation: 1883Reputation: 1883Reputation: 1883Reputation: 1883Reputation: 1883Reputation: 1883Reputation: 1883Reputation: 1883Reputation: 1883Reputation: 1883
well if it is , it is still not completely correct, its just a push in a direction that might lead him to the solution.
 
Old 07-12-2019, 06:50 AM   #6
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 21,707

Rep: Reputation: 5739Reputation: 5739Reputation: 5739Reputation: 5739Reputation: 5739Reputation: 5739Reputation: 5739Reputation: 5739Reputation: 5739Reputation: 5739Reputation: 5739
Quote:
Originally Posted by deep_kol View Post
Hi Folks ,
Need your help in unix to solve this issue . I have 2 data files a.txt (csv) and b.txt (fixed length ) . Need to search from b.txt for which the 1st 2 columns matches from both the files and the output is kept in a new file .

a.txt
------------
1,b,20
2,c,40

b.txt
------------------
1 b Apple
2 d Jack
3 c Fruits
2 c Adam
4 c Adam

output c.txt

1 b Apple
2 c Adam
Wow, this seems like familiar territory, seeing as you've made similar no-effort homework-question posts in the past:
https://www.linuxquestions.org/quest...ne-4175609588/
https://www.linuxquestions.org/quest...ix-4175627245/
 
  


Reply

Tags
compare


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Find/grep command to find matching files, print filename, then print matching content stefanlasiewski Programming 9 06-30-2016 05:30 PM
merge columns from multiple files in a directory based on match of two columns prasanthi yanamala Linux - Newbie 2 11-12-2015 10:11 AM
[SOLVED] How to compare a list of files in two directories: compare content and print size Batistuta_g_2000 Linux - Newbie 9 03-24-2013 07:05 AM
Map 1 CSV's columns to matching columns in another CSV 2legit2quit Programming 7 10-27-2011 08:53 AM
How to compare two columns in a file. shilpa.godhe Linux - Newbie 2 03-29-2010 02:42 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 12:55 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration