Merge two input files and concatenate the rest


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Merge two input files and concatenate the rest
# 1  
Old 11-01-2014
Merge two input files and concatenate the rest

Hi Guys,

I need a help to creating a bash script to merging two files that containing not in order pattern into single file also within that single file I need to concatenate and manipulate the string from two files. Appreciate for any kind help given.

Thanks.

Here are the input files:
File1
Code:
A00018-04,1M,2014-09-15T10:12:36.762Z 
A00025-08,2M,2014-09-19T09:41:48.610Z 
A00025-10,3M,2014-09-15T10:12:36.988Z 
A00033-03,1M,2014-09-15T10:12:36.762Z 
A00037-01,5M,2014-09-11T04:10:47.840Z 
A00040-03,BYPASS,2014-09-02T04:10:11.710Z 
A00054-01,5M,2014-09-15T10:12:36.762Z 
A00055-01,15M,2014-09-15T10:12:36.988Z
A00055-03,15M,2014-10-28T10:32:50.897Z
A00058-01,2M,2014-09-15T04:32:53.020Z

File2
Code:
A00018-04,ABC111,2014-10-29T07:29:56.347Z
A00037-01,ABC555,2014-10-29T07:33:29.287Z
A00025-08,ABC222,2014-10-29T07:30:35.426Z
A00040-03,ABC666,2014-09-02T05:16:50.492Z
A00025-10,ABC333,2014-10-29T07:31:45.609Z
A00033-03,ABC444,2014-10-29T07:33:00.567Z
A00054-01,ABC777,2014-10-29T07:34:05.975Z

Desire output:
Code:
A00018-04_ABC111(1M)
A00025-08_ABC222(2M)
A00025-10_ABC333(3M)
A00033-03_ABC444(1M)
A00037-01_ABC555(5M)
A00040-03_ABC666(BYPASS)
A00054-01_ABC777(5M)
A00055-01_NO-DESCRIPTION(15M)
A00055-03_NO-DESCRIPTION(15M)
A00058-01_NO-DESCRIPTION(2M)

# 2  
Old 11-01-2014
You could try something like:
Code:
awk -F ',' '
FNR == NR { a[$1] = $2; next }
{ printf("%s_%s(%s)\n", $1, ($1 in a) ? a[$1] : "NO-DESCRIPTION", $2) }' File2 File1

If you want to try this on a Solaris/SunOS System, change awk to /usr/xpg4/bin/awk, /usr/xpg6/bin/awk or nawk.
# 3  
Old 11-01-2014
With join and sed

Both File1 and File2 have to be sorted.

Code:
join -t',' -1 1 -2 1 -a 1 -e 'NO-DESCRIPTION' -o '1.1,2.2,1.2' File1 File2 | sed -e 's/,/_/' -e 's/,/(/' -e 's/$/)/'

My versions of join and sed are as below
join (GNU coreutils) 8.23
sed (GNU sed) 4.2.2
# 4  
Old 11-01-2014
Hi Don and ni2, thank you so much for your feedback and help. It is seem Don's method is suitable for my case.

Thanks again guys.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Merge input from two files into one based on conditions

Using Linux (bash), I have two files which contain information about berries. Example: file1.txt: Blueberry blue 14 Raspberry red 12 Blackberry dark 4 file2.txt Blackberry sour 4 3 Blueberry tasty 12 78 Strawberry yummy 33 88 I want to merge these two files into one. The desired... (5 Replies)
Discussion started by: Zooma
5 Replies

2. Shell Programming and Scripting

Read input files and merge them in given order and write them to input one param or one file

Dear Friends, I am looking for a shell script to merge input files into one file .. here is my idea: 1st paramter would be outfile file (all input files content) read all input files and merge them to input param 1 ex: if I pass 6 file names to the script then 1st file name as output file... (4 Replies)
Discussion started by: hyd1234
4 Replies

3. UNIX for Dummies Questions & Answers

Concatenate files and delete source files. Also have to add a comment.

- Concatenate files and delete source files. Also have to add a comment. - I need to concatenate 3 files which have the same characters in the beginning and have to remove those files and add a comment and the end. Example: cat REJ_FILE_ABC.txt REJ_FILE_XYZ.txt REJ_FILE_PQR.txt >... (0 Replies)
Discussion started by: eskay
0 Replies

4. Programming

STL algorithm merge() function to concatenate char arrays

When the STL generic algorithm's merge() function is used to merge two char arrays, the output is not as expected. Below is the program I tried with. #include <iostream> #include <algorithm> #include <cstring> #include <deque> #include <iterator> using namespace std; int main() { ... (3 Replies)
Discussion started by: royalibrahim
3 Replies

5. Shell Programming and Scripting

Concatenate files

I have a file named "file1" which has the following data 10000 20000 30000 And I have a file named "file2" which has the following data ABC DEF XYZ My output should be 10000ABC 20000DEF (3 Replies)
Discussion started by: bobby1015
3 Replies

6. Shell Programming and Scripting

Merge of two input file by search

Hi i am running a issue with the way i handel open file in perl i have the following input file <File1> D33963|BNS Default Swap|-261564.923909249| D24484|BNS Default Swap|-53356.6868058492| D24485|BNS Default Swap|-21180.9904679111| D33965|BNS Default Swap|154181.478745804|... (6 Replies)
Discussion started by: kykyboss
6 Replies

7. Shell Programming and Scripting

Concatenate files

Hi, I want to create a batch(bash) file to combine 23 files together. These files have the same extension. I want the final file is save to a given folder. Once it is done it will delete the 23 files. Thanks for help. Need script. (6 Replies)
Discussion started by: zhshqzyc
6 Replies

8. Shell Programming and Scripting

Concatenate files

I have directory structure sales_only under which i have multiple directories for each dealer example: ../../../Sales_Only/xxx_Dealer ../../../Sales_Only/yyy_Dealer ../../../Sales_Only/zzz_Dealer Every day i have one file produce under each directory when the process runs. The requirement... (3 Replies)
Discussion started by: mohanmuthu
3 Replies

9. Shell Programming and Scripting

want to concatenate multiple files based on the rest of ls -lrt

uadm@4132> ls -lrt -rw------- 1 uadm uadm 3811819 Jun 6 04:08 data_log-2010.05.30-10:04:08.txt -rw------- 1 uadm uadm 716246 Jun 13 01:38 data_log-2010.06.06-10:04:08.txt -rw------- 1 uadm uadm 996 Jun 13 04:00 data_log-2010.06.06-10:04:22.txt -rw------- 1 uadm uadm 7471 Jun 20 02:03... (5 Replies)
Discussion started by: mail2sant
5 Replies

10. Shell Programming and Scripting

How to separate 0kb files from the rest!!!

Hi All, Can you please tell me how to identify a 0kb file from a list a files and redirect only those files to a .txt file. ls -l shows me all the 0kb files...but how to redirect only those files.. Thanks for ur help, Kumar (3 Replies)
Discussion started by: kumarsaravana_s
3 Replies
Login or Register to Ask a Question