The UNIX and Linux Forums  


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
how to access values of awk/nawk variables outside the awk/nawk block? saniya Shell Programming and Scripting 5 05-13-2008 08:37 AM
Sed,nawk for inputfile and out put file same svenkatareddy Shell Programming and Scripting 2 04-09-2008 08:12 AM
I need help counting the fields and field separators using Nawk scrappycc Shell Programming and Scripting 3 02-06-2008 11:47 PM
Append tabs at the end of each line in NAWK -- varying fields madhunk Shell Programming and Scripting 6 07-12-2006 07:20 PM
Append a field to the end of each line of a file based on searching another file. ultimate Shell Programming and Scripting 2 03-29-2005 11:21 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rating: Thread Rating: 2 votes, 4.50 average. Display Modes
  #1 (permalink)  
Old 11-10-2004
axl axl is offline
Registered User
  
 

Join Date: Jul 2004
Location: Botswana
Posts: 5
append field to file(nawk)

I have two files. File A and File B.

File A has two fields separated by comma

352020252365988, 652020100572356
546876543215667, 652065465654686
...

File B has many Fields separate by spaces

Date Name 352020252365988 Reference
Date2 Name2 546876543215667 Reference


I want to search for a match of Field 1 of file A in file 2. If there is a match print the line that contains the match, and append field 2 of File A at the end of each matched line.

so to have

Date
  #2 (permalink)  
Old 11-10-2004
zazzybob's Avatar
zazzybob zazzybob is offline Forum Advisor  
Registered Geek
  
 

Join Date: Dec 2003
Location: Melbourne, Australia
Posts: 2,100
This code works for me


Code:
#!/bin/sh

while read line
do
  lookup=`echo "$line" | awk -vFS=',' '{print $1}'`
  field=`echo "$line" | awk -vFS=',' '{print $2}' | sed 's/^ //'`
  match=`grep "\<$lookup\>" file_b`
  if [ "$?" -eq "0" ]; then
    echo "$match" | while read record
    do
      echo "$record $field"
    done
  fi
done < file_a

assuming your files are named file_a and file_b.

The nested while is in case you have duplicate records and more than one record matches.

You'll need to redirect the output of this script to a file obviously.

Cheers
ZB
  #3 (permalink)  
Old 11-10-2004
Ygor's Avatar
Ygor Ygor is offline Forum Staff  
Moderator
  
 

Join Date: Oct 2003
Location: -31.96,115.84
Posts: 1,409
If the match is on the 3rd field of the 2nd file, then you can try this awk...
Code:
BEGIN {
   FS=","
   while(getline<xref) arr[$1]=$2
   FS=" "
}
$3 in arr {
   print $0, arr[$3] 
}

Use it like this...

awk -f above.awk -v xref=file_a file_b

Tested on the sample data...

Date Name 352020252365988 Reference 652020100572356
Date2 Name2 546876543215667 Reference 652065465654686
  #4 (permalink)  
Old 11-14-2004
axl axl is offline
Registered User
  
 

Join Date: Jul 2004
Location: Botswana
Posts: 5
Thanx, both these scripts have worked!

I prefer the last one since I get to name files at the command prompt.

gurus u are, u guys!

___
Axl
  #5 (permalink)  
Old 11-14-2004
zazzybob's Avatar
zazzybob zazzybob is offline Forum Advisor  
Registered Geek
  
 

Join Date: Dec 2003
Location: Melbourne, Australia
Posts: 2,100
Just FYI, if you change the code I posted thusly

Code:
#!/bin/sh

while read line
do
  lookup=`echo "$line" | awk -vFS=',' '{print $1}'`
  field=`echo "$line" | awk -vFS=',' '{print $2}' | sed 's/^ //'`
  match=`grep "\<$lookup\>" ${2}`
  if [ "$?" -eq "0" ]; then
    echo "$match" | while read record
    do
      echo "$record $field"
    done
  fi
done < ${1}

You can call this with ./my_script file_a file_b - i.e. specifying the files at the prompt as you wish!

Cheers
ZB
Closed Thread

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:31 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
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