How to join 2 text files using bash scripting?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to join 2 text files using bash scripting?
# 1  
Old 04-08-2016
How to join 2 text files using bash scripting?

Hi Guys,

I want to combine 2 files and and put together in 1 file [ only 2 fields ($4 and $5 in FILE A, $2 and $3 in FILE B) are the common thing between the two files]. See below desired output. Any help will be much appreciated.

FILE A
Code:
X  2134  101L  12345.00   22222.00    1  10
X  2134  101L  12345.00   22222.00  11  20
X  2134  101L  12345.00   22222.00  21  30
X  2134  111L  77777.00   22222.00    1  10
X  2134  111L  77777.00   22222.00  11  20
X  2134  111L  77777.00   22222.00  21  30
X  2134  121L  56347.00   46678.00    1  10
X  2134  121L  56347.00   46678.00  11  20
X  2134  121L  56347.00   46678.00  21  30
........

FILE B
Code:
3333 12345  22222  54367.05  34765.05
3333 34567  12235  54298.05  34568.05
3333 77777  22222  52765.05  32567.05
3333 55555  11111   52875.05  36547.05
3333 56347  46678  53789.05  34566.05
........

_______________________________
RESULT
Code:
3333 2134   10   12345 22222 54367.05 34765.05
3333 NA     NA 34567 12235 54298.05 34568.05
3333 2134   11    77777 22222 52765.05 32567.05
3333 NA     NA  55555 11111  52875.05 36547.05
3333 2134   12 56347 46678 53789.05 34566.05
.......

Thanks in advance!Smilie
Moderator's Comments:
Mod Comment Please use CODE tags around all sample input, sample output, and code segments; not just around filenames.

Last edited by Don Cragun; 04-08-2016 at 06:20 PM.. Reason: Fix CODE and ICODE tags.
# 2  
Old 04-08-2016
Hello H.R,

Welcome to forums, please use code tags for complete Input_file and sample outputs into posts as per forums rules. Here are some points which are not clear into your request.
I- Number of lines into filea and fileb are NOT same, so how comparison should happen?
II- IN case it should be by filea's $4 and $5 and fileb's $2 and $3(as per your post) then also there is a question, let's say we have following scenario where same values of $4 and $5 have more than one values into it then which value we should print?
It is advisable to please describe your requirement little more with sample Input_file and expected Output_file. Anyways based on few wild assumptions following you could take as a startup for this problem.
Code:
awk 'FNR==NR{sub(/\..*/,X,$4);sub(/\..*/,X,$5);A[$4 FS $5]=$2 FS $NF;next} {$2=($2 FS $3) in A?A[$2 FS $3]:"NA NA"} 1' filea fileb

Output will be as follows.
Code:
3333 2134 30 22222 54367.05 34765.05
3333 NA NA 12235 54298.05 34568.05
3333 2134 30 22222 52765.05 32567.05
3333 NA NA 11111 52875.05 36547.05
3333 2134 30 46678 53789.05 34566.05

Please get back to us in case of any queries on same.

Thanks,
R. Singh
# 3  
Old 04-08-2016
How to join 2 text files using bash scripting?

Thank you Mr R. Singh for your reply,

I attached one picture, it will describe well my requirement, thanks in advance
How to join 2 text files using bash scripting?-scriptpng
# 4  
Old 04-09-2016
Do not post classroom or homework problems in the main forums. Homework and coursework questions can only be posted in this forum under special homework rules.

Please review the rules, which you agreed to when you registered, if you have not already done so.

More-than-likely, posting homework in the main forums has resulting in a forum infraction. If you did not post homework, please explain the company you work for and the nature of the problem you are working on.

If you did post homework in the main forums, please review the guidelines for posting homework and repost.

Thank You.

The UNIX and Linux Forums.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to move files with first part as new filenamevia bash shell scripting?

I am on linux red hat, want to use the logic within bash shell script file. I have the following type, named files in folder /staging, want to move with new filename just the firstpart upto underscore, example 20180904105056.dat, how can i get upto first part of filename and validate to chk if... (2 Replies)
Discussion started by: cplusplus1
2 Replies

2. Shell Programming and Scripting

Alignment tool to join text files in 2 directories to create a parallel corpus

I have two directories called English and Hindi. Each directory contains the same number of files with the only difference being that in the case of the English Directory the tag is .english and in the Hindi one the tag is .Hindi The file may contain either a single text or more than one text... (7 Replies)
Discussion started by: gimley
7 Replies

3. Shell Programming and Scripting

Join columns across multiple lines in a Text based on common column using BASH

Hello, I have a file with 2 columns ( tableName , ColumnName) delimited by a Pipe like below . File is sorted by ColumnName. Table1|Column1 Table2|Column1 Table5|Column1 Table3|Column2 Table2|Column2 Table4|Column3 Table2|Column3 Table2|Column4 Table5|Column4 Table2|Column5 From... (6 Replies)
Discussion started by: nv186000
6 Replies

4. Shell Programming and Scripting

Bash: join 2 files

Hello ! I want to join 2 files. They look like this: KE340296.1 1 0/0:11 KE340296.1 2 0/0:12 KE340296.1 6 0/1:13 KE340297.1 1 0/1:14 KE340297.1 3 0/1:15 KE340297.1 4 0/1:16 and KE340296.1 1 0/0:21 KE340296.1 2 0/0:22 KE340296.1 3... (4 Replies)
Discussion started by: MumuGB
4 Replies

5. Shell Programming and Scripting

Is there a way to join 2 text files sorted by

Can anyone please help me i have 2 text files setup like the one below. Textfile1: randomemail1:randompassword1 randomemail2:randompassword2 randomemail3:randompassword3 randomemail4:randompassword4 randomemail5:randompassword5 Textfile2: randompassword1:randomphrase1... (8 Replies)
Discussion started by: nufc
8 Replies

6. UNIX for Dummies Questions & Answers

How to use the the join command to join multiple files by a common column

Hi, I have 20 tab delimited text files that have a common column (column 1). The files are named GSM1.txt through GSM20.txt. Each file has 3 columns (2 other columns in addition to the first common column). I want to write a script to join the files by the first common column so that in the... (5 Replies)
Discussion started by: evelibertine
5 Replies

7. UNIX for Dummies Questions & Answers

How to use the join command to obtain tab delimited text files as an output?

How do you use the join command and obtain tab delimited text files as an output? Thanks! (2 Replies)
Discussion started by: evelibertine
2 Replies

8. UNIX for Dummies Questions & Answers

Join 2 files with multiple columns: awk/grep/join?

Hello, My apologies if this has been posted elsewhere, I have had a look at several threads but I am still confused how to use these functions. I have two files, each with 5 columns: File A: (tab-delimited) PDB CHAIN Start End Fragment 1avq A 171 176 awyfan 1avq A 172 177 wyfany 1c7k A 2 7... (3 Replies)
Discussion started by: InfoSeeker
3 Replies

9. Shell Programming and Scripting

Bash scripting to compare N number of files located in two directories

I want to compare "N" (around 2000+) number of huge files located in a directory A against "N" files located in a different directory using Bash scripting. Please help me with any scripts available. Thanks. (2 Replies)
Discussion started by: Sangtha
2 Replies

10. Shell Programming and Scripting

Log FILES in BASH scripting

Hi Im new to BASH scripting, I have created a simple script #! /bin/sh LOCAL=/home/scanner NOW=$(date +"%b-%d-%y") LOGFILE="log-$NOW.log" cd $LOCAL echo 'Start'; echo $LOGFILE touch $LOGFILE (2 Replies)
Discussion started by: jackdaw
2 Replies
Login or Register to Ask a Question