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

Closed Thread
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 10-10-2008
Mish_99 Mish_99 is offline
Registered User
  
 

Join Date: Aug 2008
Posts: 10
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
  #2 (permalink)  
Old 10-10-2008
zaxxon's Avatar
zaxxon zaxxon is offline Forum Staff  
Moderator
  
 

Join Date: Sep 2007
Location: Germany
Posts: 2,257
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.
  #3 (permalink)  
Old 10-10-2008
Mish_99 Mish_99 is offline
Registered User
  
 

Join Date: Aug 2008
Posts: 10
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
  #4 (permalink)  
Old 10-10-2008
danmero danmero is offline Forum Advisor  
  
 

Join Date: Nov 2007
Location: 45.48-73.63
Posts: 1,367
That's a head start:
Code:
awk 'NR==FNR{_[NR]=$0;next;}{$1=_[FNR];print}' file1 file2
  #5 (permalink)  
Old 10-10-2008
radoulov's Avatar
radoulov radoulov is offline Forum Staff  
addict
  
 

Join Date: Jan 2007
Location: Варна, България / Milano, Italia
Posts: 2,793
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 08:43 AM.. Reason: refactored (still quite new to Perl ...)
Sponsored Links
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 05:00 AM.


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