merge two file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting merge two file
# 1  
Old 03-21-2011
merge two file

how to merge two file, i have 2 input file; file_1 & file_2, so i want the output file_3 as below

file_1
NAME
aa
111
222
bb.txt
111
222
cc
111
111
dd
222
ee
111
222



file_2
NAME
/usr/my_work/aa
/usr/my_work/bb
/usr/my_work/cc
/usr/my_work/dd
/usr/my_work/ee



output file

file_3
/usr/my_work/aa
111
222
/usr/my_work/bb
111
222
/usr/my_work/cc
111
111
/usr/my_work/dd
222
/usr/my_work/ee
111
222
# 2  
Old 03-22-2011
Code:
awk -F"[. ]" 'NR==FNR{f=$0;sub(".*/","",f);F[f]=$0;next} $1 in F{$0=F[$1]} !/NAME/' file_2 file_1

# 3  
Old 03-22-2011
Quote:
Originally Posted by Chubler_XL
Code:
awk -F"[. ]" 'NR==FNR{f=$0;sub(".*/","",f);F[f]=$0;next} $1 in F{$0=F[$1]} !/NAME/' file_2 file_1

its worked..thanks, can u explain the code
# 4  
Old 03-22-2011
Uses dot and space as field separators, this is so we can pickup filename without extension (eg bb from bb.txt).

NR==FNR{f=$0;sub(".*/","",f);F[f]=$0;next} Store pathnames in file_2 into array F[] with index as basename of file.

$1 in F{$0=F[$1]}if field 1 (bit before any fullstop) from file_1 is available in the F[] array then assign line as the fullpathname we stored from file_1 earlier, otherwise leave it as it is.

!/NAME/If the new line from previous command does not contain NAME then print it out.[/icode]
This User Gave Thanks to Chubler_XL For This Post:
# 5  
Old 03-23-2011
Quote:
Originally Posted by Chubler_XL
Uses dot and space as field separators, this is so we can pickup filename without extension (eg bb from bb.txt).

NR==FNR{f=$0;sub(".*/","",f);F[f]=$0;next} Store pathnames in file_2 into array F[] with index as basename of file.

$1 in F{$0=F[$1]}if field 1 (bit before any fullstop) from file_1 is available in the F[] array then assign line as the fullpathname we stored from file_1 earlier, otherwise leave it as it is.

!/NAME/If the new line from previous command does not contain NAME then print it out.[/icode]
thanks bro
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk can't open file; file merge attempt

Dear all, I have an AWK related issue. I have two data files; the first, FileA has fewer lines, the second, FileB has more. FileA is a subset of FileB . Both files are tab delimited. What I want to do? When the first two columns for FileA match the first two columns of FileB, I want to... (4 Replies)
Discussion started by: A_Human_Person
4 Replies

2. UNIX for Beginners Questions & Answers

Match and Merge two file

Hi All, I have two file . I need to merge both file based on a match. File 1: Column1 column2 column3 column4 File 2: column3 column5 I need to combine the two file based on match , Which in my case is column3 and combine the file as below Combined file Column1 (10 Replies)
Discussion started by: arunkumar_mca
10 Replies

3. UNIX for Dummies Questions & Answers

How to merge two files into third file?

Hi All, i have a requirement as below. There are two files. In 1st file a.txt i have four lines as below Ramesh Suresh Pradeep Franklin In 2nd file b.txt i have 5 lines as below Francis Elizabeth David John Ravi The output file suppose c.txt should contain 1st line of... (5 Replies)
Discussion started by: sureshk_85
5 Replies

4. UNIX Desktop Questions & Answers

Merge Last Word to Another File

Hi, Im a newbie to scripting. I have a file that looks like the one below. How can i change the last "/" to become a space. Thank You so Much for the help. :) hostname date Feb-9 /u100/DEVCO/Patching/a.log hostname date Jun-25 /u100/DEVCO/DumpCleaner/a.log hostname date Jun-25... (2 Replies)
Discussion started by: lienyca
2 Replies

5. Shell Programming and Scripting

Merge two files into one file

Hi, I need help with merging two files. My requirement is as below: File 1: MachineA MachineB File 2: Process1 Process2 Desired File: MachineA Process1 MachineB Process2 If file 2 contains only one entry Process1, then second line in the desired output should be: MachineA Process1... (1 Reply)
Discussion started by: chiru_h
1 Replies

6. Programming

Merge files from /etc to one file using C

Hi guys, I have a question which might be easy to answer but I don't how to do it. The thing is I need to make a program in C which creates a file with all the content from the files in \etc. I'm not new to C language but to UNIX. I've read somewhere I need to use functions like f_read... (2 Replies)
Discussion started by: sussil
2 Replies

7. Shell Programming and Scripting

[Bash]Attempting to Merge text from one file into another file at the line directly under a word

Hello, This is my first post on the forums. So I want to start by thanking anyone who is kind enough to read this post and offer advise. I hope to be an active contributor now that I've found these forums. I have an issue that I figure would be a good first post.. I have 2 text files... (5 Replies)
Discussion started by: efciem
5 Replies

8. Shell Programming and Scripting

two file merge with awk

Help I read a file that has 2 fields. look for in a second file the first field and update it with the second field of first file. file1 1131518fat11416.txt ../newaod/2001/04/2001-04-00129233-1.txt file2 INSERT INTO tabric VALUES... (2 Replies)
Discussion started by: mcarlo65
2 Replies

9. UNIX for Dummies Questions & Answers

file merge

Hi, I have 3 files. The containts of the files are as followes. File 1: 1 2 3 File 2: A B C File 3: Ind US Aus (6 Replies)
Discussion started by: siba.s.nayak
6 Replies

10. Shell Programming and Scripting

merge two files in one file

All, I've basic knowlege on understanding UNIX Shell Programming. Right now I need to write a script for the following requirement. Kindly help me or suggest me how to write a Shell script. I've 8 .csv files that are containing "Detail" records. Each file getting more than 1 million. Another... (3 Replies)
Discussion started by: nvkuriseti
3 Replies
Login or Register to Ask a Question