The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Changing one column of delimited file column to fixed width column manneni prakash Shell Programming and Scripting 5 06-22-2009 05:27 AM
two files.say a and b.both have long columns.i wanna match the column fron 1st file w TRUPTI UNIX for Dummies Questions & Answers 4 10-17-2008 01:17 PM
Insert first line of a file to first column of remaining files a_artha Shell Programming and Scripting 5 07-28-2008 05:47 AM
How to check Null values in a file column by column if columns are Not NULLs Mandab Shell Programming and Scripting 7 03-15-2008 09:57 AM
join cols from multi files into one file vbshuru Shell Programming and Scripting 2 11-20-2003 12:52 PM

Reply
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 06-30-2009
bigsmile bigsmile is offline
Registered User
  
 

Join Date: Jun 2009
Posts: 4
Question Join 3 files using key column in a mapping file

I'm new of UNIX shell scripting. I'm recently generating a excel report in UNIX(file with delimiter is fine). How should I make a script to do it?

1 file to join comes from output of one UNIX command, the second from another UNIX command, and third from a database query. The key columes of all these 3 files come from a mapping file. It's basically as following:

MappingFile:
===========
job1 a_name1
job2 c_name2
job3 b_name3
job4 e_name4

file1_toJOIN:
============
b_name3 12:00:03 15:00:09
e_name4 10:30:00 11:00:00
c_name2 09:40:00 10:12:00
a_name1 22:00:00 23:00:00

file2_toJOIN:
=============
job2 tom 22
job1 dan 18
job4 jim 25
job3 kim 20

the spreadsheet format to generate:
===========================
job1 22:00:00 23:00:00 01:00:00 dan 18
......

Appreciate your help!
  #2 (permalink)  
Old 07-01-2009
prasperl prasperl is offline
Registered User
  
 

Join Date: Jun 2009
Posts: 22
Try this!

try this

for i in `cat map | awk 'BEGIN { FS = " " }; { print $1}'`
do
mapkey1=$i
mapkey2=`cat map | grep $mapkey1 `
mapkey2=`echo $mapkey2 | awk 'BEGIN { FS = " " }; { print $2}'`
file1key=`cat file1 | grep $mapkey2`
file1key=`echo $file1key | awk 'BEGIN { FS = " " }; { print $2,$3}'`
file2key=`cat file2 | grep $mapkey1`
file2key=`echo $file2key | awk 'BEGIN { FS = " " }; { print $2,$3}'`
echo $i $file1key $file2key
done


map - your map file
file1 - your file1_toJOIN
file2 - file2_toJOIN:

The o/p i got is :
job1 22:00:00 23:00:00 dan 18
job2 09:40:00 10:12:00 tom 22
job3 12:00:03 15:00:09 kim 20
job4 10:30:00 11:00:00 jim 25
  #3 (permalink)  
Old 07-01-2009
summer_cherry summer_cherry is online now Forum Advisor  
Registered User
  
 

Join Date: Jun 2007
Location: Beijing China
Posts: 1,051
Code:
nawk 'NR==FNR{
arr[$1]=$2
brr[$2]=$1
}
NR!=FNR{
	if (arr[$1]=="")
		brr[$1]=sprintf("%s %s",$2,$3)
	else
		print $1" "brr[arr[$1]]" "$2" "$3
}' map file1 file2
  #4 (permalink)  
Old 07-02-2009
bigsmile bigsmile is offline
Registered User
  
 

Join Date: Jun 2009
Posts: 4
Smile Join 3 files using key colume in a mapping file

Appreciate both of you.

I've tried the first script. It works perfectly. I feel a bit difficult to understand array, as a shell script beginner. But I'll try tomorrow, as it looks really compact. I'd like to use in the future.
  #5 (permalink)  
Old 07-03-2009
prasperl prasperl is offline
Registered User
  
 

Join Date: Jun 2009
Posts: 22
With comments for better understanding

#Opening the file
for i in `cat map | awk 'BEGIN { FS = " " }; { print $1}'`
do

#Column1 in map file is key to file2
mapkey1=$i

mapkey2=`cat map | grep $mapkey1 `

#Column2 in map file is key to file1

mapkey2=`echo $mapkey2 | awk 'BEGIN { FS = " " }; { print $2}'`

#Selecting values from file1 based on mapkey2 from map file
file1key=`cat file1 | grep $mapkey2`
file1key=`echo $file1key | awk 'BEGIN { FS = " " }; { print $2,$3}'`

#Selecting values from file2 based on mapkey1 from map file
file2key=`cat file2 | grep $mapkey1`
file2key=`echo $file2key | awk 'BEGIN { FS = " " }; { print $2,$3}'`

#Diplay the results
echo $i $file1key $file2key
done
  #6 (permalink)  
Old 07-04-2009
bigsmile bigsmile is offline
Registered User
  
 

Join Date: Jun 2009
Posts: 4
Thank you, Prasperl.

I used ksh, for some reason, it gave me awk syntex error. After I removed field seperator, it let me through. Do you know the reason?
  #7 (permalink)  
Old 07-05-2009
prasperl prasperl is offline
Registered User
  
 

Join Date: Jun 2009
Posts: 22
Could you please post the code u used and the error message?
Sponsored Links
Reply

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -4. The time now is 11:10 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language translation by Google.
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0