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 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 2 07-01-2008 12:39 AM
Replacing column with column of another file manneni prakash UNIX for Dummies Questions & Answers 1 06-24-2008 08:20 PM
how to read the column and print the values under that column gemini106 Shell Programming and Scripting 6 03-28-2008 04:05 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 06:57 AM
Replace 10th column with a new column--- Terriblly hurry ahmedwaseem2000 Shell Programming and Scripting 2 09-05-2005 10:10 PM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1  
Old 10-10-2008
Registered User
 

Join Date: Aug 2008
Posts: 5
column handling in awk

Dear Scripting experts,

I have a problem which i cannot get my head around and wondered if anyone can help me. I have two files "file1" and "file2" and i want to replace column one from file 1 with column one with file2.(where file two has many columns). see example.. ive tried to use cut and paste but got myslef in a mess...

file1 file2
1 9 66 8 A
2 8 3 9 B
3 66 4 25 C
4 66 2 6 D
7 4 3 3 E
5 4 5 45 F

Output:-
1 66 8 A
2 3 9 B
3 4 25 C
4 2 6 D
7 3 3 E
5 5 45 F

Is there an easy way i can do this in awk? ideally i would like to have a one liner which i could invoke using the system command in a perl script.

Thanks


Mish
Reply With Quote
Forum Sponsor
  #2  
Old 10-10-2008
Moderator
 

Join Date: Sep 2007
Location: Germany
Posts: 958
Sorry, but I can't make out, which part is from file1 and which from file2. Can you post them on separate lines and use those code tags please^^
Also it already smells like "join" would be a good choice for the tool to use for that.
Reply With Quote
  #3  
Old 10-10-2008
Registered User
 

Join Date: Aug 2008
Posts: 5
Hi, Osrry for that hope this helps clarify issues

Code:
File 1
1
2
3
4
5
6
Code:
File 2
 9    66 8  a
 8    3  9  b
 66   4  25 c
 66   2  6  d 
 4    3  3  e
 4    5  45 f
Code:
output
  1    66 8  a
  2    3  9  b
  3    4  25 c
  4    2  6  d 
  5    3  3  e
  6    5  45 f
kindest regards


Mish
Reply With Quote
  #4  
Old 10-10-2008
 

Join Date: Nov 2007
Location: 45.48-73.63
Posts: 727
That's a head start:
Code:
awk 'NR==FNR{_[NR]=$0;next;}{$1=_[FNR];print}' file1 file2
Reply With Quote
  #5  
Old 10-10-2008
radoulov's Avatar
addict
 

Join Date: Jan 2007
Location: Milano, Italia/Варна, България
Posts: 1,859
I would use Perl directly:

Code:
$ head file*
==> file1 <==
1
2
3
4
5
6

==> file2 <==
 9    66 8  a
 8    3  9  b
 66   4  25 c
 66   2  6  d 
 4    3  3  e
 4    5  45 f
$ perl -e'
  open F1,"<file1"or die$!;open F2,"<file2"or die$!;
  s/([^\s]+)\s*/<F1>/eand s/\n/\t/and print while<F2>;
  close F1;close F2'         
 1      66 8  a
 2      3  9  b
 3      4  25 c
 4      2  6  d 
 5      3  3  e
 6      5  45 f

Last edited by radoulov; 10-10-2008 at 05:43 AM. Reason: refactored (still quite new to Perl ...)
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 01:54 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0