Visit Our UNIX and Linux User Community


How to append two files(file1 and file2) Please help me?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to append two files(file1 and file2) Please help me?
# 1  
Old 05-09-2014
Oracle How to append two files(file1 and file2) Please help me?

Hi

I have two files file1 and file2
File1 10,000 entries:It has 3 columns below.
Code:
conn=232257   client=xxx.xxx.xx.xxx:60491  protocol=LDAP

File2 has 500 entries It has two columns.
Code:
conn=232257   dn="uid=xxxx,ou=xxxx,ou=xxxx,dc=xxxxx,dc=xxxx"
conn=232398 dn="uid=yyyy,ou=yyyyy,ou=yyyyy,dc=yyyy,dc=yyy"

Output file should be like below with four columns: If first column matches between file1 and file2 then shell command should add column-2 from file-2 to file-1 as column-4. Sorry for confusion. Output should be like this.

O/p:

Code:
conn=232257   client=xxx.xxx.xx.xxx:60491  protocol=LDAP  dn="uid=xxxx,ou=xxxx,ou=xxxx,dc=xxxxx,dc=xxxx"


Last edited by vgersh99; 05-09-2014 at 02:25 PM.. Reason: code tags, please!
# 2  
Old 05-09-2014
This should be easily done with awk.
With 31 posts already, how far have you gotten in your implementation AND where exactly are you stuck?
Please show the effort!
# 3  
Old 05-09-2014
Hi

I'm newe to shell and this is my first post can you please share the solution.
I tried using awk as below but i did't understand few parameters so i posted here for solution.

Code:
awk 'NR==FNR {h[$2] = $3; next} {print $1,$2,$3,h[$2]}' file1 file3


Last edited by vgersh99; 05-09-2014 at 02:56 PM.. Reason: once again - code tags, please!
# 4  
Old 05-09-2014
Code:
awk 'NR==FNR {h[$1] = $2; next} {print $0,h[$1]}' file2 file1

btw, this is NOT your first post - you have 31 posts already!
# 5  
Old 05-09-2014
Hi Vgersh

Sorry i did't get my expected results after running your command which you posted. Can you please look in to my requirement which i posted with example.

I need 4 coulms in my output file like below.

Code:
conn=232257  client=xxx.xxx.xxx.xx:51237 protocol=LDAP    dn="uid=xxxx,ou=xxxx,ou=xxxx,dc=xxxx,dc=xxx"


Last edited by vgersh99; 05-09-2014 at 03:19 PM.. Reason: PLEASE, start using code tag!!!
# 6  
Old 05-09-2014
given file1:
Code:
conn=232257   client=xxx.xxx.xx.xxx:60491  protocol=LDAP

and file2:
Code:
conn=232257   dn="uid=xxxx,ou=xxxx,ou=xxxx,dc=xxxxx,dc=xxxx"
conn=232398 dn="uid=yyyy,ou=yyyyy,ou=yyyyy,dc=yyyy,dc=yyy"

running the following:
Code:
awk 'NR==FNR {h[$1] = $2; next} {print $0,h[$1]}' file2 file1

produces the following output:
Code:
conn=232257   client=xxx.xxx.xx.xxx:60491  protocol=LDAP dn="uid=xxxx,ou=xxxx,ou=xxxx,dc=xxxxx,dc=xxxx"

What's wrong with this output?
What do you get? And how's that different from the output above?
Please be as specific as possible AND use code tags when posting your code and/or data samples.
# 7  
Old 05-09-2014
Hi

I know its 31 posts but i'm not in to this filed.
After running your command,i see ourput is merged two files(file1 and file2) into file3

---------- Post updated at 03:29 PM ---------- Previous update was at 03:09 PM ----------

Hi
Can you explain what is h[$1],$1,$2,$0 values so that i can change code accordingly
Code:
awk 'NR==FNR {h[$1] = $2; next} {print $0,h[$1]}' file2 file1


Last edited by Scrutinizer; 05-09-2014 at 05:18 PM..

Previous Thread | Next Thread
Test Your Knowledge in Computers #546
Difficulty: Medium
In computer science, an operator takes the values of its operands and combines them in some way to produce a new value.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to search field2 in file2 using range of fields file1 and using match to another field in file1

I am trying to use awk to find all the $2 values in file2 which is ~30MB and tab-delimited, that are between $2 and $3 in file1 which is ~2GB and tab-delimited. I have just found out that I need to use $1 and $2 and $3 from file1 and $1 and $2of file2 must match $1 of file1 and be in the range... (6 Replies)
Discussion started by: cmccabe
6 Replies

2. UNIX for Dummies Questions & Answers

Compare file1 and file2, print matching lines in same order as file1

I want to print only the lines in file2 that match file1, in the same order as they appear in file 1 file1 file2 desired output: I'm getting the lines to match awk 'FNR==NR {a++}; FNR!=NR && a' file1 file2 but they are in sorted order, which is not what I want: Can anyone... (4 Replies)
Discussion started by: pathunkathunk
4 Replies

3. UNIX for Dummies Questions & Answers

Copy(append ) the contents of file1 and file2 to file3

Platform : Oracle linux 6.5 I have two log files with the following contents # ls -l total 8 -rw-r--r--. 1 root root 75 Dec 10 20:55 myLogfile1.log -rw-r--r--. 1 root root 51 Dec 10 20:57 myLogfile2.log # # cat myLogfile1.log hello world jaded zombies acted quaintly but kept driving... (9 Replies)
Discussion started by: kraljic
9 Replies

4. Shell Programming and Scripting

If file1 and file2 exist then

HI, I would like a little help on writing a if statement. What i have so far is: #!/bin/bash FILE1=path/to/file1 FILE2=path/to/file2 echo ${FILE1} ${FILE2} if ] then echo file1 and file2 not found else echo FILE ok fi (6 Replies)
Discussion started by: techy1
6 Replies

5. UNIX for Dummies Questions & Answers

if matching strings in file1 and file2, add column from file1 to file2

I have very limited coding skills but I'm wondering if someone could help me with this. There are many threads about matching strings in two files, but I have no idea how to add a column from one file to another based on a matching string. I'm looking to match column1 in file1 to the number... (3 Replies)
Discussion started by: pathunkathunk
3 Replies

6. Shell Programming and Scripting

Compare a common field in two files and append a column from File 1 in File2

Hi Friends, I am new to Shell Scripting and need your help in the below situation. - I have two files (File 1 and File 2) and the contents of the files are mentioned below. - "Application handle" is the common field in both the files. (NOTE :- PLEASE REFER TO THE ATTACHMENT "Compare files... (2 Replies)
Discussion started by: Santoshbn
2 Replies

7. Shell Programming and Scripting

AWK: Pattern match between 2 files, then compare a field in file1 as > or < field in file2

First, thanks for the help in previous posts... couldn't have gotten where I am now without it! So here is what I have, I use AWK to match $1 and $2 as 1 string in file1 to $1 and $2 as 1 string in file2. Now I'm wondering if I can extend this AWK command to incorporate the following: If $1... (4 Replies)
Discussion started by: right_coaster
4 Replies

8. Shell Programming and Scripting

grep -f file1 file2

Hi I started to learn bash a week ago. I need filter the strings from the last column of a "file2" that match with a column from an other "file1" file1: chr10100036394-100038350AK077761 chr10100041065-100046547AK032226 chr10100041065-100046547AK016270 chr10100041065-100046547AK078231 ...... (6 Replies)
Discussion started by: geparada88
6 Replies

9. Shell Programming and Scripting

append text from file1 to the end of each line in file2

hi; my file2.txt:portname=1;list=10.11;l- portname=2;list=10.12;l- portname=3;list=10.13;l- ... my file1.txt:;"{'sector=%27'}"\&> so; i want to see:portname=1;list=10.11;l-;"{'sector=%27'}"\&> portname=2;list=10.12;l-;"{'sector=%27'}"\&> portname=3;list=10.13;l-;"{'sector=%27'}"\&>... (4 Replies)
Discussion started by: gc_sw
4 Replies

10. Shell Programming and Scripting

match value from file1 in file2

Hi, i've two files (file1, file2) i want to take value (in column1) and search in file2 if the they match print the value from file2. this is what i have so far. awk 'FILENAME=="file1"{ arr=$1 } FILENAME=="file2" {print $0} ' file1 file2 (2 Replies)
Discussion started by: myguess21
2 Replies

Featured Tech Videos