Join same line from multiple file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Join same line from multiple file
# 1  
Old 01-24-2009
Question Join same line from multiple file

Hi,

Can we use shell script to combine multiple file ?
For example we have 3 file

1.txt
Quote:
111
222
333
2.txt
Quote:
aaa
bbb
ccc
3.txt
Quote:
1ab
2ab
3ab
and the result should be :
Quote:
111,aaa,1ab
222,bbb,2ab
333,ccc,3ab
# 2  
Old 01-24-2009
With awk:

Code:
awk '{
  getline f2 < "2.txt"
  getline f3 < "3.txt"
  print $0,f2,f3
}' OFS="," 1.txt

Regards
# 3  
Old 01-24-2009
Wow..
great.. thanks Franklin
# 4  
Old 01-24-2009
Hi Franklin
So if we have file :
Quote:
number=111
char: bbb,xxx
name-1ab
number=222
char: aaa,xxx
name-2ab
number=333
char: ccc,xxx
name-3ab
then should i dump first to temp file :
Quote:
cat file | grep number | cut -d= -f2 > 1.txt
cat file | grep char | awk '{print$2}' | cut -d, -f1 > 2.txt
cat file | grep name | cut -d- -f2 > 3.txt
and then running that script..

Quote:
awk '{
getline f2 < "2.txt"
getline f3 < "3.txt"
print $0,f2,f3
}' OFS="," 1.txt
# 5  
Old 01-25-2009
You can get the output with one awk command:

Code:
$ cat file
number=111
char: aaa,xxx
name-1ab
number=222
char: bbb,xxx
name-2ab
number=333
char: ccc,xxx
name-3ab
$
$
$ awk -F" |,|-|=" '
/number/{s=$2}
/char/{s=s","$2}
/name/{s=s","$2;print s}
' file
111,aaa,1ab
222,bbb,2ab
333,ccc,3ab
$
$

Regards
# 6  
Old 01-25-2009
Quote:
Originally Posted by justbow
Hi,

Can we use shell script to combine multiple file ?
For example we have 3 file

1.txt
2.txt
3.txt
:

Code:
paste -d , 1.txt 2.txt 3.txt

# 7  
Old 01-26-2009
Great .. thanks all Smilie

regards,
Bow
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Perl command line option '-n','-p' and multiple files: can it know a file name of a printed line?

I am looking for help in processing of those options: '-n' or '-p' I understand what they do and how to use them. But, I would like to use them with more than one file (and without any shell-loop; loading the 'perl' once.) I did try it and -n works on 2 files. Question is: - is it possible to... (6 Replies)
Discussion started by: alex_5161
6 Replies

2. Shell Programming and Scripting

Join multiple lines from text file

Hi Guys, Could you please advise how to join multiple details lines into single row, with HEADER 1 as the record separator and comma(,) as the field separator. Input: HEADER 1, HEADER 2, HEADER 3, 11,22,33, COLUMN1,COLUMN2,COLUMN3, AA1, BB1, CC1, END: ABC HEADER 1, HEADER 2,... (3 Replies)
Discussion started by: budz26
3 Replies

3. Shell Programming and Scripting

How to join one file with multiple files in a directory in UNIX?

Dear folks Hello I have a one file called (file1) which the structure looks like this 1 gi|358484521|ref|NW_003764373.1| 1 gi|358484520|ref|NW_003764374.1| 1 gi|358484519|ref|NW_003764375.1| . . . 30 gi|368484519|ref|NW_00449375.1| In addition, I have around 300... (19 Replies)
Discussion started by: sajmar
19 Replies

4. Shell Programming and Scripting

Join common patterns in multiple lines into one line

Hi I have a file like 1 2 1 2 3 1 5 6 11 12 10 2 7 5 17 12 I would like to have an output as 1 2 3 5 6 10 7 11 12 17 any help would be highly appreciated Thanks (4 Replies)
Discussion started by: Harrisham
4 Replies

5. UNIX for Dummies Questions & Answers

How to use the the join command to join multiple files by a common column

Hi, I have 20 tab delimited text files that have a common column (column 1). The files are named GSM1.txt through GSM20.txt. Each file has 3 columns (2 other columns in addition to the first common column). I want to write a script to join the files by the first common column so that in the... (5 Replies)
Discussion started by: evelibertine
5 Replies

6. Shell Programming and Scripting

join based on line number when one file is missing lines

I have a file that contains 87 lines, each with a set of coordinates (x & y). This file looks like: 1 200.3 -0.3 2 201.7 -0.32 ... 87 200.2 -0.314 I have another file which contains data that was taken at certain of these 87 positions. i.e.: 37 125 42 175 86 142 where the first... (1 Reply)
Discussion started by: jackiev
1 Replies

7. UNIX for Dummies Questions & Answers

Join 2 files with multiple columns: awk/grep/join?

Hello, My apologies if this has been posted elsewhere, I have had a look at several threads but I am still confused how to use these functions. I have two files, each with 5 columns: File A: (tab-delimited) PDB CHAIN Start End Fragment 1avq A 171 176 awyfan 1avq A 172 177 wyfany 1c7k A 2 7... (3 Replies)
Discussion started by: InfoSeeker
3 Replies

8. Programming

sql,multiple join,outer join issue

example sql: select a.a1,b.b1,c.c1,d.d1,e.e1 from a left outer join b on a.x=b.x left outer join c on b.y=c.y left outer join d on d.z=a.z inner join a.t=e.t I know how single outer or inner join works in sql. But I don't really understand when there are multiple of them. can... (0 Replies)
Discussion started by: robbiezr
0 Replies

9. Shell Programming and Scripting

join on a file with multiple lines, fields

I've looked at the join command which is able to perform what I need on two rows with a common field, however if I have more than two rows I need to join all of them. Thus I have one file with multiple rows to be joined on an index number: 1 randomtext1 2 rtext2 2 rtext3 3 rtext4 3 rtext5... (5 Replies)
Discussion started by: crimper
5 Replies

10. OS X (Apple)

Command line tool to join multiple .wmv files?

I need a simple command line executable that allows me to join many wmv files into one output wmv file, preferrably in a simple way like this: wmvjoin file1.wmv file2.wmv .... > outputfile.wmv So what I want is the wmv-equivalent of mpgtx I cannot find it on internet. Thanks. (2 Replies)
Discussion started by: karman
2 Replies
Login or Register to Ask a Question