![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum Rules | FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here. |
|
|
||||
| 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 |
|
|
Submit Tools | LinkBack | Thread Tools | Display Modes |
|
#1
|
|||
|
|||
|
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 |
| Forum Sponsor | ||
|
|
|
#2
|
|||
|
|||
|
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
|
|||
|
|||
|
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 Mish |
|
#4
|
|||
|
|||
|
That's a head start:
Code:
awk 'NR==FNR{_[NR]=$0;next;}{$1=_[FNR];print}' file1 file2
|
|
#5
|
||||
|
||||
|
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 ...) |
||||
| Google The UNIX and Linux Forums |