LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
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 04-30-2016, 05:48 PM   #1
beca123456
LQ Newbie
 
Registered: Apr 2016
Posts: 10

Rep: Reputation: Disabled
awk: compare fields of file 1 to fields of file 2


Hi,


Using awk, I am trying to append the comment(s) of file2 at the end of file1 if:
- field1 of file1 is equal to field1 of file2
AND
- field2 of file1 is ≥ field2 of file2 and ≤ field3 in file2

file1.tab (NF is constant):
Code:
1   20   value1
4   73   value2
file2.tab (NF is variable):
Code:
1   10   30   commentA1   commentA2
2   50   89   commentB1
4   55   90   commentC1
output:
Code:
1   20   value1   commentA1   commentA2
4   73   value2   commentC1
I can (kind of) output the first condition (i.e. if $1 file1 = $2 file2), but don't see how to do deal with the second condition:
Code:
gawk 'BEGIN{FS="\t"}NR==FNR{a[$1]=$0; next}{print $0 FS a[$1]}' file2 file1
which produces:
Code:
1   20   value1   1   10   30   commentA1   commentA2
4   73   value2   4   55   90   commentC1

Last edited by beca123456; 04-30-2016 at 05:49 PM.
 
Old 04-30-2016, 08:03 PM   #2
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,140

Rep: Reputation: 4122Reputation: 4122Reputation: 4122Reputation: 4122Reputation: 4122Reputation: 4122Reputation: 4122Reputation: 4122Reputation: 4122Reputation: 4122Reputation: 4122
First you'd better get your head around the first condition. You aren't testing for equality of anything, let alone $1. Also, saving into the array like that will replace (i.e. lose) previous records if $1 in file2 is not unique. And once saved, field-splitting on the saved data is no longer in effect.
Take it one step at a time.
 
Old 05-01-2016, 03:28 AM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,008

Rep: Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193
The other issue I see is that file2 seems to be able to have a variable number of comments and as these are the only things you need to keep, you need a way to append all but the first 3 fields.

As syg00 has said, you need to take this one step at a time. I would ignore file1 for the time being and work out how to store all the required information from file2.
 
  


Reply

Tags
awk, files



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
How to compare two files using four fields and print out the mismatched fields ? avatar007 Programming 3 05-04-2014 11:35 AM
Configuring GNOME to have Full File Path Text Fields in File Open Dialog Boxes bgoodr Linux - Desktop 3 11-29-2011 09:07 AM
compare files with fields separated with ':' Alkass Programming 3 07-18-2011 03:54 AM

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

All times are GMT -5. The time now is 08:00 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
Open Source Consulting | Domain Registration