column handling in awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting column handling in awk
# 1  
Old 10-10-2008
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  
Old 10-10-2008
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  
Old 10-10-2008
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  
Old 10-10-2008
That's a head start:
Code:
awk 'NR==FNR{_[NR]=$0;next;}{$1=_[FNR];print}' file1 file2

# 5  
Old 10-10-2008
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 09:43 AM.. Reason: refactored (still quite new to Perl ...)
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Handling embedded double quotes within column data

I have a text file where each field is enclosed in double quotes and separated by a comma. But in certain rows we have embedded double quotes within column data For e.g """TRUSPICE CENTRE"" CLAYTON AU" The above value is for a single column but there are embedded quotes within... (2 Replies)
Discussion started by: abhilashnair
2 Replies

2. Shell Programming and Scripting

Handling 2 files simultaneously with awk

Hello, Is it possible to handle data from two different files at once in awk (latest version and platform is Fedora). I found on the net that we cannot nest awk. My requirement is that I have two similar files : File 1: Name: abc Val = 58 Name: cdf Val = 1; .................. File... (7 Replies)
Discussion started by: fifteate
7 Replies

3. UNIX for Dummies Questions & Answers

Awk - Handling different types of newlines

Hi. We have some data that's generated from a webpage. Part is pretty well-formatted, but part of it preserves newlines in a way that breaks the record separating in awk. Here's 2 records, filtered through cat -e: Jones,Bob,20,Q: What is your favorite ice cream?$ A: Butter Pecan$ Q: Do you... (6 Replies)
Discussion started by: treesloth
6 Replies

4. Shell Programming and Scripting

handling asterix in AWK

I have a file like below. colA^col2^col3^col4^col5 aa^11^aaa^a1a^111^aa* bb*^22^bbb*^bb2^222^bb cc^33^ccc*^3cc^333^ccc dd^44^d*dd*^d4d^444^ddd ee^55^e*ee^e5e*^555^e*e NOTE: '^' is the field separator. I need to get the output as colA^col2^col3^col4^col5 aa^11^aaa^a1a^111^aa... (5 Replies)
Discussion started by: rinku11
5 Replies

5. Shell Programming and Scripting

Data handling using AWK

Hi, I have requirement to fetch lines with a particular character in one column e.g. 2.5M asdsad 3.5M sadsadas 12323M ssdss i tried following so far #echo 2.3M asdsad | nawk -F " " '{print substr($1,length($1))}' M So far i have tried following # echo 2.3M asdsad | nawk... (4 Replies)
Discussion started by: mtomar
4 Replies

6. UNIX for Advanced & Expert Users

awk function in handling quotes

Hi all, I have input lines like below empno,ename,sal,description ---------------------------- 311,"jone,abc",2000,manager 301,david,200,"president,ac" I need to sum the salary of them i.e. 2000+200 anything suggested Thanks, Shahnaz. Use code tags. (5 Replies)
Discussion started by: shahnazurs
5 Replies

7. Shell Programming and Scripting

handling arrays with awk

Hi, I have an issue that I am trying to resolve using arrays in awk. I have two files, the first one is a dictionary with this format: FILE 1 (dictionary) 'Abrir' 'Open' 'Aceptar' 'Accept' Every line has two fields, a word in two languages. The second file is a simple list of... (3 Replies)
Discussion started by: gmartinez
3 Replies

8. UNIX for Dummies Questions & Answers

Sum up a decimal column in a tab separated text file and error handling

Hi, I have a small requirement where i need to sum up a column in a text file. Input file 66ab 000000 534385 -00000106350.00 66cd 000000 534485 -00013364511.00 66ad 000000 534485 -00000426548.00 672a 000000 534485 000000650339.82... (5 Replies)
Discussion started by: pssandeep
5 Replies

9. Shell Programming and Scripting

Handling regular expressions in awk

Script is: accept filename as argument(also handle CTRL+C).to check whether th file exist in the current directory,it it then using awk find the employees who are either born in 1990 or drawing a salary greater than 25000. In my database file 1st field is of id ,2nd field is name,5th field is of... (5 Replies)
Discussion started by: Priyanka Bhati
5 Replies

10. Shell Programming and Scripting

AWK handling of single quote

Hi, Can someone let me know how I can acheive the following. I have ~ delimited file and I need to convert into something like SQL insert statements. SrcFile : 1~sjdsdj~asasas~ 2~aaaaa~qwqwqwq~qwq ..... I tried AWK -F"~" '{print "INSERT INTO XX VALUES("$1 " ,\' "$2" \' , \' "$3 }'... (3 Replies)
Discussion started by: braindrain
3 Replies
Login or Register to Ask a Question