LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-31-2015, 10:10 PM   #1
byran cheung
Member
 
Registered: Sep 2013
Posts: 321

Rep: Reputation: Disabled
Column matching


I have two file , the format as below .

file A
======
Code:
Name   Education Level	Sex
====   ===============  ===	
Mary   Unviersity	F
Peter  Unviersity	M
John   Unviersity	M
Eva    High School	F
Bill   Post Graduate	M
Eddie  Post Graduate 	M
file B
=======
Code:
Education Level Type 
=============== ====
Unviersity	 A
High School	 B
Post Graduate	 C
I would like to do the matching , add the third column to the matched file , the matching result would be as below file , would advise how to do it ? thanks

Output file after match
========================
Code:
Name   Education Level	Type	Sex	
====   ===============  ====    ===	
Mary   Unviersity	A	F	
Peter  Unviersity	A	M
John   Unviersity	A	M
Eva    High School	B	F
Bill   Post Graduate	C	M
Eddie  Post Graduate 	C	M

Last edited by byran cheung; 03-31-2015 at 10:18 PM.
 
Old 04-01-2015, 01:11 AM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,011

Rep: Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194
Sounds like a plan, where is your attempt and where are you stuck? I have seen similar examples on this site, so you may be able to search for a solution.
 
Old 04-01-2015, 01:30 AM   #3
lucmove
Senior Member
 
Registered: Aug 2005
Location: Brazil
Distribution: Debian
Posts: 1,434

Rep: Reputation: 110Reputation: 110
If I were you, I would be just as lazy as I am and do this:

1) Edit the header on file A manually.

2) Write this script:

script.sed
Code:
s/Unviersity/University\tA/
s/High School/High School\tB/
s/Post Graduate/Post Graduate\tC/
3) Then run:

Code:
sed -f script.sed fileA.txt > result.txt
Not clever, but does the job. You just may have to run the resulting file through a formatting tool, to fix the size of each column.

Note that I fixed the misspelling on 'University' for you.

Last edited by lucmove; 04-01-2015 at 01:32 AM.
 
Old 04-01-2015, 04:22 AM   #4
AnanthaP
Member
 
Registered: Jul 2004
Location: Chennai, India
Posts: 952

Rep: Reputation: 217Reputation: 217Reputation: 217
Using awk and reading in fileB followed by fileA ...

If NR==FNR (ie fileB) then read the values into an associative array.

Else (ie fileA) for each record, seek the result in the associative array and write the output.

OK
 
  


Reply



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
[SOLVED] matching string in specific column and delete line udiubu Linux - Newbie 5 05-25-2012 02:29 AM
[SOLVED] How to grep -v and omit all results matching 'x' in column 'y' zongbot Linux - General 11 09-12-2011 02:07 PM
[SOLVED] awk with pipe delimited file (specific column matching and multiple pattern matching) lolmon Programming 4 08-31-2011 12:17 PM
Perl Script needed to be reversed to output matching, not non-matching 0bfuscated Programming 2 07-20-2010 10:51 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:13 AM.

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