Bring two files together


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Bring two files together
# 1  
Old 11-10-2015
Bring two files together

Dear Forum,

I was wondering if anybody could help me to bring to files together.

I have two text files. In the text file each record occupies two lines. First the ID number and in the next line text.

Code:
$ cat text.txt
+12345678
text_A
+12345644
text_B
+12334241
text_C
+1234586
text_D
+12231241
text_E
+1234
text_F

The second file also contains the ID and a header. Each record is a line.

Code:
$ cat head.txt
12345678	Head_A
12345644	Head_B
12334241	Head_C
1234	Head_F

I would need to combine the two files according to the exact ID number.

Code:
$ cat outfile.txt
+12345678 Head_A
text_A
+12345644 Head_B
text_B
+12334241 Head_C
text_C
+1234	Head_F
text_F

I could reformat the text.txt file into one line per record and use the ID to grep and combine the hits. I guess not very elegant and slow. I was also thinking of use a grep inside a while loop but I guess this would be slow too.

I was hoping to find an array solution but I would need some help:

Code:
awk 'FNR==NR{a[$1];next}{ for(i=1;i<=NF;i++){ if($i ~ "+" && $i in a) {print $i} else print $i} }' head.txt text.txt

I would appreciate some help. Thanks!
# 2  
Old 11-10-2015
Code:
awk '
        NR == FNR {
                i = substr($0,2);
                getline;
                A[i] = $0
                next
        }
        $1 in A {
                print "+" $0 RS A[$1]
        }
' text.txt head.txt

# 3  
Old 11-10-2015
Code:
awk 'FNR==NR {h["+" $1]=$2;next} {print $0, h[$1]}' head.txt text.txt

# 4  
Old 11-10-2015
The requestor seemed to want to eliminate the records not in the head file. So try a small adaption to Aia's code:
Code:
awk 'FNR==NR {h["+" $1]=$2;next} $1 in h {print $0, h[$1]; getline; print}' head text
+12345678 Head_A
text_A
+12345644 Head_B
text_B
+12334241 Head_C
text_C
+1234 Head_F
text_F

# 5  
Old 11-10-2015
Quote:
Originally Posted by RudiC
The requestor seemed to want to eliminate the records not in the head file. So try a small adaption to Aia's code:
Code:
awk 'FNR==NR {h["+" $1]=$2;next} $1 in h {print $0, h[$1]; getline; print}' head text
[...]

Based on the title of the post and on the following opening sentence:
Quote:
I was wondering if anybody could help me to bring [two] files together.
I took it as the opposite, even when I did not see any output for text_D or text_E

Nevertheless, for that case I would suggest:
Code:
awk 'FNR==NR {h[$1]=$2;next} $1 in h {print "+" $1, h[$1];print $2}' head.file RS="+" text.file

# 6  
Old 11-11-2015
Dear Yoda, dear Aia, dear RudiC,

thank you all for helping me to find three perfect solutions for my problem. @Aia, sorry if my description of the problem was not so clear.

Here the three solutions that worked for me:

Code:
time awk 'FNR==NR {h["+" $1]=$2;next} $1 in h {print $0, h[$1]; getline; print}' text.txt header.txt > outA.tmp

real	0m20.552s
user	0m17.823s
sys	0m2.697s

Code:
time awk '
        NR == FNR {
                i = substr($0,2);
                getline;
                A[i] = $0
                next
        }
        $1 in A {
                print "+" $0 RS A[$1]
        }
' text.txt head.txt > outB.tmp

real	0m22.600s
user	0m19.482s
sys	0m3.078s

Code:
time awk 'FNR==NR {h[$1]=$2;next} $1 in h {print "+" $1, h[$1];print $2}' header.txt RS="+" text.txt > outC.tmp

real	1m29.579s
user	1m25.451s
sys	0m3.584s

Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bring prompt from a different process

Hi, while running a program one of the command is creating a sub-shell which requires user inputs. Is there a way to bring the input prompt to a parallel session or an alternate way to input the values? the outer program doesn't allow to accept the parameter value for the command run in the sub... (8 Replies)
Discussion started by: ahmedwaseem2000
8 Replies

2. HP-UX

Adding Storage, do I need to bring the DB down?

Hi Guys, Do I need to bring the DB down? masd023x:: uname -r B.11.11 Thanks! Where can I find this type of information? JC (3 Replies)
Discussion started by: 300zxmuro
3 Replies

3. UNIX for Dummies Questions & Answers

bring up/down numerous interfaces

On a Solaris 9 system I had something happen to some of my interfaces; for some reason half of them went down. Since I have over 30 different Virtual IP's (or logical IP's) up on ce10 I don't want to take the time to find out which ones are up and which ones are down or even run the addif command... (2 Replies)
Discussion started by: snoman1
2 Replies

4. Red Hat

Cannot bring up Firefox

I have installed firefox and it previously worked before I installed the oracle software on my machine. Now when I click on the icon to bring up the browser it fails. I tried to do it from the command line and this is what I get: $ firefox Cannot find mozilla runtime directory. Exiting ... (1 Reply)
Discussion started by: jxh461
1 Replies

5. Solaris

Not able to bring up the server

Hi All, I have a iplanet server running on solaris , i'm not able to start it using the start Its giving following error root@bld00: /usr/iplanet/servers6.1sp8/https-MLL-8.3dev # ./start Sun ONE Web Server 6.1SP8 B06/13/2007 23:15 info: CORE5076: Using from info: WEB0100: Loading web... (2 Replies)
Discussion started by: renjithrnath
2 Replies

6. UNIX for Dummies Questions & Answers

Unable to bring up Sun OS

The last thing I did was set REMOTE_LOGIN_PASSWORDFILE to EXCLUSIVE. Then rebooted server. Since that time I have not been able to login. During bootup it can't find root and generates alot of errors. Does anybody know of a default login and password that will allow me to log in and rebuild the... (2 Replies)
Discussion started by: mayewil
2 Replies

7. Solaris

bring up GUI on solaris

I have just installed solaris 10 on a sever .would appreciate if somebody can help me bringing up the GUI(basically i want to launch the browser) (1 Reply)
Discussion started by: Anant
1 Replies

8. IP Networking

unable to bring up any interface

I have Linux 7.1 setup on a Dell and am unable to bring up any interfaces. Both the lo and eth0 fail upon boot up and I get a insmod error possibly due to bad irq or IO setting. If I disable eth0 upon boot, then lo works fine, but offcourse I get the "delaying initialization" upon boot for eth0... (1 Reply)
Discussion started by: alam
1 Replies

9. UNIX for Dummies Questions & Answers

Bring back removed files

Dear People I have removed some of my files and directories( by using rm and rmdir commands) by mistake. I wish to bring them back. How is it possible?( I am using solaris 2.6) best regards Reza Nazarian:( (2 Replies)
Discussion started by: Reza Nazarian
2 Replies
Login or Register to Ask a Question