Register
It is currently Thu Dec 14, 2017 7:14 am

merge 2 files


All times are UTC - 6 hours


Post new topic Reply to topic  [ 5 posts ] 
Author Message
 PostPosted: Mon Apr 25, 2016 3:04 am   

Joined: Tue Nov 17, 2015 6:30 am
Posts: 70
Code:
$ cat a.dat
APPS:ORACLE_SID_A:Yellow
XXRBS:ORACLE_SID_A:Green
$ cat b.dat
APPS:ORACLE_SID_A:Blue

I'd like to end up with the following file
Code:
$ cat c.dat
APPS:ORACLE_SID_A:YellowBlue
XXRBS:ORACLE_SID_A:Green

I've considered paste, which results in the following:
Code:
$ paste -d':' a.dat b.dat > c.dat
$ c.dat
APPS:ORACLE_SID_A:Yellow:APPS:ORACLE_SID_A:Blue
XXRBS:ORACLE_SID_A:Green:

I could use a while loop along with sed -i to transform the ilnes to how I wand them to be, but I was wondering if anybody could suggest anything simpler ?


Top
 Profile  
 PostPosted: Mon Apr 25, 2016 6:51 am   

Joined: Mon Oct 20, 2014 9:53 am
Posts: 560
try/read man join


Top
 Profile  
 PostPosted: Mon Apr 25, 2016 9:04 am   

Joined: Tue Nov 17, 2015 6:30 am
Posts: 70
as far as I can tell, one cannot join on more than 1 field, so its not really viable.

the following function does the trick, but was hoping to cimplify it:
Code:
function merge_pswd_files() {
  # if a dual passwrd file has been specified then the main password file
  # will be merged with optional second password file

  local new_pswd_file=$1
  local dual_pswd_file=$2
  local merged_file="${new_pswd_file}_merged"

  {
    paste -d':' $new_pswd_file $dual_pswd_file > $merged_file
    while read line; do
      ora_id=":"$(echo $line | cut -f1-2 -d':')":"
      line=${line%:}
      echo ${line/${ora_id}/}
    done < $merged_file
    : > $new_pswd_file
  } > $new_pswd_file

}


Top
 Profile  
 PostPosted: Mon Apr 25, 2016 9:29 am   

Joined: Mon Oct 20, 2014 9:53 am
Posts: 560
from man join:

Unless -t CHAR is given, leading blanks separate fields and are ignored, else fields are
separated by CHAR. Any FIELD is a field number counted from 1. FORMAT is one or more comma
or blank separated specifications, each being 'FILENUM.FIELD' or '0'. Default FORMAT out-
puts the join field, the remaining fields from FILE1, the remaining fields from FILE2, all
separated by CHAR. If FORMAT is the keyword 'auto', then the first line of each file deter-
mines the number of fields output for each line.

Important: FILE1 and FILE2 must be sorted on the join fields. E.g., use "sort -k 1b,1" if
'join' has no options, or use "join -t ''" if 'sort' has no options. Note, comparisons
honor the rules specified by 'LC_COLLATE'. If the input is not sorted and some lines cannot
be joined, a warning message will be given.


Top
 Profile  
 PostPosted: Tue Apr 26, 2016 2:33 am   

Joined: Tue Nov 17, 2015 6:30 am
Posts: 70
many thanks. got it
Code:
$  join -t':' -a1 -o '1.1 1.2 1.3 2.3' <(sort a.dat -t: -k1) <(sort b.dat -t: -k1)
APPS:ORACLE_SID_A:Yellow:Blue
XXRBS:ORACLE_SID_A:Green:

only minor transform left to do now is that I have to remove the final ":" on each line of the output.


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: Bing [Bot] and 19 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP