LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-27-2009, 07:46 AM   #1
mukherjee_debajyoti
LQ Newbie
 
Registered: Nov 2009
Posts: 2

Rep: Reputation: 0
merging content of two files using sed or awk



Hi
I have two files file_1 and file_2.
The contents of the two files are as shown below :

file_1
-------

Name : <Data>
Gender : <Data>
Age : <Blank , no data in this field>
Designation : <Data>


file_2
--------

Name : <Data.
Age : <Data>

The requirement is , i need to cut the Age value from file_2 and paste it to the Age field of file_1 for each and every Name.

Please provide me the solution of the problem.
(Sed or Awk anyone is OK for me)


Thanks,
Debajyoti
 
Old 11-27-2009, 07:56 AM   #2
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,836
Blog Entries: 1

Rep: Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251
Welcome to LQ.

What have you done so far to solve this problem?
 
Old 11-29-2009, 11:34 PM   #3
mukherjee_debajyoti
LQ Newbie
 
Registered: Nov 2009
Posts: 2

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by sycamorex View Post
Welcome to LQ.

What have you done so far to solve this problem?
Hi ,
Please find the details as give below

File 1 :

Name : xx
Gender : Male
Age :
Designation : Engineer

Name : yy
Gender : Male
Age :
Designation : Manager

Name : zz
Gender : Male
Age :
Designation : Lead


File 2 :

Name : xx
Age : 28

Name : yy
Age : 30

Name : zz
Age : 40

I was trying to use awk NR==FNR{} commands to merge the contents of File_1 and File_2 to a new file File 3
But i was not getting the desired result as per my requirement

Please help me to get rid of the issue

Thanks
Debajyoti

P.S: Content of File 3 will be as shown below

Name : xx
Gender : Male
Age : 28
Designation : Engineer

Name : yy
Gender : Male
Age : 30
Designation : Manager

Name : zz
Gender : Male
Age :40
Designation : Lead
 
Old 11-30-2009, 02:19 AM   #4
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
bash 4
Code:
#!/bin/bash
declare -A names
while IFS=" : " read -r key value
do
    
    case "$key" in
        "" ) continue;;
        Name*) names["$value"]="";n=$value;;
        Age*) names["$n"]=$value;;       
            
    esac   
done <"file2"

while IFS=" : " read -r k v
do
    case "$k" in
        "" ) continue;;        
        Age*)
            echo "$k : ${names[$n]}";;
        Name*) n=$v ;;&
        *) echo "$k : $v";;
    esac               
done <"file1"

Last edited by ghostdog74; 11-30-2009 at 02:41 AM.
 
Old 11-30-2009, 03:35 AM   #5
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by mukherjee_debajyoti View Post
Hi ,
Please find the details as give below

File 1 :

Name : xx
Gender : Male
Age :
Designation : Engineer

Name : yy
Gender : Male
Age :
Designation : Manager

Name : zz
Gender : Male
Age :
Designation : Lead


File 2 :

Name : xx
Age : 28

Name : yy
Age : 30

Name : zz
Age : 40

I was trying to use awk NR==FNR{} commands to merge the contents of File_1 and File_2 to a new file File 3
But i was not getting the desired result as per my requirement

Please help me to get rid of the issue

Thanks
Debajyoti

P.S: Content of File 3 will be as shown below

Name : xx
Gender : Male
Age : 28
Designation : Engineer

Name : yy
Gender : Male
Age : 30
Designation : Manager

Name : zz
Gender : Male
Age :40
Designation : Lead
And why should this be done using 'sed' or 'awk' ?
 
Old 11-30-2009, 05:43 AM   #6
AnanthaP
Member
 
Registered: Jul 2004
Location: Chennai, India
Posts: 952

Rep: Reputation: 217Reputation: 217Reputation: 217
'awk' and 'sed' are two progs commonly available on most *nixes and so are probably easiest to start with.

In awk, I would stuff data from the first file into hashes (associative arrays) using name as the key and "" for the age.

When I encounter second file data, I would read the name and update the age into the hash.

Finally (on END), I would output the hash.

End
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Modify a text files with awk/sed/perl climber75 Programming 15 08-05-2008 03:35 PM
using awk...merging two files...lines...one after another... visitnag Linux - Newbie 4 07-14-2008 11:37 AM
awk and/or sed linux2man Linux - General 7 01-22-2007 10:02 AM
awk and change file content Ayman.mashal Programming 4 06-02-2005 06:03 AM
awk/sed help pantera Programming 1 05-13-2004 11:59 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:48 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration