pasting fields from two files into one


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting pasting fields from two files into one
# 1  
Old 02-02-2011
pasting fields from two files into one

i have two files with contents
file a
Code:
1234,abcf
2345,drft
4444,befr

file b
Code:
tom,3
sam,5
dog,7

i want to print first column of file b and join to file a and get output as below
Code:
tom,1234,abcf
sam,2345,drft
dog,4444,befr

any help using awk


Moderator's Comments:
Mod Comment Please use [code] and [/code] tags when posting code, data or logs etc. to preserve formatting and enhance readability, thanks.

Last edited by zaxxon; 02-02-2011 at 09:33 AM..
# 2  
Old 02-02-2011
Code:
paste a b | tr '\t' ',' | awk 'BEGIN{FS=","}{print $3","$1","$2}'

Is this okay?
# 3  
Old 02-02-2011
Why must it be awk? What have you tried so far?...

Another one
Code:
awk -F, 'NR==FNR {_[NR]=$0; next} {print $1,_[FNR]}' OFS=, f1 f2
tom,1234,abcf
sam,2345,drft
dog,4444,befr

Also use more descriptive subjects next time than just "files"...!!

Last edited by zaxxon; 02-02-2011 at 10:21 AM.. Reason: added comment
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Pasting multiple files using awk with delimiter

hi, i want to PASTE two files, with a delimiter in between, using awk and pipe the output to another file. i am able to achive the reqirement using PASTE command. but it has a limitation of length till 511 bytes. Example: ------- File1: ---- sam micheal file2: ---- bosco... (11 Replies)
Discussion started by: mohammedsadath
11 Replies

2. Shell Programming and Scripting

pasting two files in every directory (+100 directories)

Hi, I have around 400 directories each one named as hour_1/ , hour_2/ .....hour_400/ and each of these contains two files, namely: File1: hour_1.txt (in hour_1/) , hour_2.txt (in hour_2/) ....hour_400.txt (in hour_400/) etc... File2: client_list_hour_1.txt (in hour_1/),... (7 Replies)
Discussion started by: amarn
7 Replies

3. Shell Programming and Scripting

help with pasting files in filesystem

quick question.. say i have few files in D or E drive.. i want to paste them in Filesystem that is /home/vivek folder... but when i try to do that it shows some error saying "There is not enough space on the destination. Try to remove files to make space." but i think its due to authorization... (3 Replies)
Discussion started by: vivek d r
3 Replies

4. Shell Programming and Scripting

reading files and pasting in another text file

Hi all, I have certain task to do, which involves reading the first column of 1.txt file. This is variable "event" 28434710 23456656 3456895 & finding this "event" in some other text file 2.txt, which has information in the following format #Zgamma: 1 run: 160998 event: ... (7 Replies)
Discussion started by: nrjrasaxena
7 Replies

5. Shell Programming and Scripting

Pasting files with different number of lines

Hi all, I tried to use the paste command to paste two files with different number of lines. e.g. file1 A 1 B 1 C 2 D 2 file2 A 2 B 3 C 4 D 4 E 4 (2 Replies)
Discussion started by: f_o_555
2 Replies

6. Shell Programming and Scripting

pasting two files while transposing one of them

hey, I have more a structural problem. I have two input files: 1.inp: 1 2 3 a b c 2 3 4 d f g and the 2.inp 6 6 6 7 7 7 8 8 8 The goal is to get as much output files (with a name 1_2_3.dat) as lines in 1.inp are like this: 6 6 6 a 7 7 7 b 8 8 8 c (5 Replies)
Discussion started by: ergy1983
5 Replies

7. Shell Programming and Scripting

Copying and pasting columns from different files

hi all, first time posting, hoping i can get some help on this. I have about 80 text files containing text in this format: # Rg Mass Density Rcm-x Rcm-y Area Rsph/sqrt(2) # == ==== ======= ===== ===== ==== ... (9 Replies)
Discussion started by: Arlamos
9 Replies

8. Shell Programming and Scripting

Preserve byte size of fields while pasting it to other file in unix

Preserve byte size of fields while pasting it to other file Hello I want to set two fields in a text file to be of size 20. how to do it using unix ? eg: ABC.txt Name | City I want Name and City both to be of size 20. Also If I am pasting it in other file the byte size should be... (1 Reply)
Discussion started by: dashing201
1 Replies

9. UNIX for Dummies Questions & Answers

Preserve byte size of fields while pasting it to other file

Hello I want to set two fields in a text file to be of size 20. how to do it using unix ? eg: ABC.txt Name | City I want Name and City both to be of size 20. Also If I am pasting it in other file the byte size should be preserved.i.e. If I want to append content of ABC.txt to other... (0 Replies)
Discussion started by: dashing201
0 Replies

10. UNIX for Dummies Questions & Answers

Trouble pasting multiple files together!!

Hi, I would like to paste multiple files together into one large file. I have 23 of them and I would like to link them on a common variable without writing all the file names out (like in a simple join). Each has about 28,000 columns, but only 17 rows. So the final product would be a single file... (2 Replies)
Discussion started by: etownbetty
2 Replies
Login or Register to Ask a Question