Help me to combine two file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help me to combine two file
# 1  
Old 07-15-2010
Help me to combine two file

Hi, can anybody help me.
I want to combine these two file into one horizontally.

1st File:
Code:
...
10:00
10:01
10:02
10:03
10:04
10:05
...

2nd File:
Code:
...
0715 10:00 Caps: 50
0715 10:01 Caps: 65
0715 10:02 Caps: 75
0715 10:04 Caps: 50
0715 10:05 Caps: 87
...

Expected output:
Code:
...
0715 10:00 Caps: 50
0715 10:01 Caps: 65
0715 10:02 Caps: 75
0715 10:03
0715 10:04 Caps: 50
0715 10:05 Caps: 87
...

I already try using a join command but not succeed.
Code:
join -1 1 -2 2 1stFile 2ndFile

but the result are only data which exist in both file.
Pardon for the poor english. Smilie

Last edited by Scott; 07-16-2010 at 07:01 AM.. Reason: Code tags
# 2  
Old 07-16-2010
Hi

Try:

Code:
awk 'NR==FNR{a[i++]=$0;x=$1;next}{if (a[j] ~ $1){print a[j];j++}else print x,$0}' i=1 j=1 file2 file1

Guru.
This User Gave Thanks to guruprasadpr For This Post:
# 3  
Old 07-16-2010
Quote:
Originally Posted by guruprasadpr
Hi

Try:

Code:
awk 'NR==FNR{a[i++]=$0;x=$1;next}{if (a[j] ~ $1){print a[j];j++}else print x,$0}' i=1 j=1 file2 file1

Guru.
I tried the code that you gave me, but the result are this:

Code:
23:59 0715 08:01 Caps: 12
23:59 0715 08:02 Caps: 11
23:59 0715 08:03 Caps: 10
23:59 0715 08:04 Caps: 10
23:59 0715 08:05 Caps: 9
23:59 0715 08:07 Caps: 9
23:59 0715 08:08 Caps: 11
23:59 0715 08:09 Caps: 10

the result should be:
Code:
08:01 0715 08:01 Caps: 12
08:02 0715 08:02 Caps: 11
08:03 0715 08:03 Caps: 10
08:04 0715 08:04 Caps: 10
08:05 0715 08:05 Caps: 9
08:06
08:07 0715 08:07 Caps: 9
08:08 0715 08:08 Caps: 11
08:09 0715 08:09 Caps: 10

but thanks though, Smilie
Can you help me again?

Last edited by Scott; 07-16-2010 at 07:01 AM.. Reason: Code tags
# 4  
Old 07-16-2010
Try...
Code:
$ head file[12]
==> file1 <==
10:00
10:01
10:02
10:03
10:04
10:05

==> file2 <==
0715 10:00 Caps: 50
0715 10:01 Caps: 65
0715 10:02 Caps: 75
0715 10:04 Caps: 50
0715 10:05 Caps: 87

$ join -1 1 -2 2 -a 1 -o "1.1 2.1 2.2 2.3 2.4" file1 file2
10:00 0715 10:00 Caps: 50
10:01 0715 10:01 Caps: 65
10:02 0715 10:02 Caps: 75
10:03
10:04 0715 10:04 Caps: 50
10:05 0715 10:05 Caps: 87

$

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Need to combine two lines in a file based on first character of each line in a file

Hi, I have a requirement where I need to combine two lines in a file based on first character of each line in a file. Please find the sample content of the file below: Code: _______________________ 5, jaya, male, 4-5-90, single smart 6, prakash, male, 5-4-84, married fair 7, raghavi,... (1 Reply)
Discussion started by: jayaP
1 Replies

2. UNIX for Dummies Questions & Answers

Need to combine two lines in a file based on first character of each line in a file

Hi, I have a requirement where I need to combine two lines in a file based on first character of each line in a file. Please find the sample content of the file below: Code: _______________________ 5, jaya, male, 4-5-90, single smart 6, prakash, male, 5-4-84, married fair 7, raghavi,... (1 Reply)
Discussion started by: jayaP
1 Replies

3. Shell Programming and Scripting

Combine two parts of a file

Hello All, I have a file like this APPLY ( 'INSERT INTO brdcst_media_cntnt ( cntnt_id ,brdcst_media_cntnt_cd ,cntnt_prvdr_cd ,data_src_type_cd ,cntnt_titl_nm ,cntnt_desc ,batch_dt ,batch_id ) VALUES ( :cntnt_id (3 Replies)
Discussion started by: nnani
3 Replies

4. Shell Programming and Scripting

Combine in single file

I have 2 files: phone.txt and mobile.txt ex. phone.txt MOBILENO|DISABLE_DATE 919687877754|9/1/2011| 919687877762|9/1/2011| 919687880573|9/1/2011| 919687882658|9/2/2011| Ex. mobile.txt MOBILENO |TIME 919687880573|2011-09-17 12:23:40| 919687882658|2011-10-10 21:15:33|... (4 Replies)
Discussion started by: khingx
4 Replies

5. Shell Programming and Scripting

combine two values within a file

Can you guys help me, I have two files: File A 60|11111| 601|11111| 6012|11111| 6013|11111| 60142|11111| File B 642|11111| 659|11111| 852|11111| 6012|11111| 6013|11111| I want to combine the two file into: File C 60|642| 601|642| 6012|642| (2 Replies)
Discussion started by: lurak
2 Replies

6. Shell Programming and Scripting

want to combine multiple file into one

Below are the list of files I have I want to consolidate the monthly log in the one file Eg : all the july log to be moved to log_july_full.txt inside the script Path : /home/user/data/log/ Jun 6 04:08 data_log-2010.05.30-10:04:08.txt Jun 13 01:38 data_log-2010.06.06-10:04:08.txt... (3 Replies)
Discussion started by: mail2sant
3 Replies

7. UNIX for Dummies Questions & Answers

How do I combine the last 2 lines of a file

I tried to put the history line number and the date into the file with one command, and failed. Can't figure out how to get the date variable substituted for the last space captured. history | tail -1 | sed -e 's/.\{7\}/&/g' | head -1 | sed 's/ $/$date/' Result was: 729 $date So, I... (8 Replies)
Discussion started by: jimbob75
8 Replies

8. Shell Programming and Scripting

How to combine first,second and third fields in a file

Hi Guys, I have a file as follows: 1 2 3 There are 5 spaces between 1 and 2 and one space between 2 and 3. I want: 123 How can I do this using awk? I tried using: (8 Replies)
Discussion started by: npatwardhan
8 Replies

9. Shell Programming and Scripting

combine multiple .xl file

Hi Friends, Is it possible in Unix ? The requirement is ============= Create a unix shellscript that combines multiple excel files into one. Note : it is possible for data files.but the requirement is for excel file (4 Replies)
Discussion started by: bikas_jena
4 Replies

10. Shell Programming and Scripting

Combine file

Hi all, If I have 4 kind of files , and each file have different date, all of them are flat file and have the same 4 fields Date|ID|Class|City english.20060228.dat 02/28/2006|ABC|ENG|San Tomas 02/28/2006|BCD|ENG|San Jone 02/28/2006|AFD|ENG|San Luis 02/28/2006|ADD|ENG|San Mateo ... (3 Replies)
Discussion started by: sabercats
3 Replies
Login or Register to Ask a Question