UNIX joins : facing issue while joining three files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting UNIX joins : facing issue while joining three files
# 8  
Old 07-28-2015
Hello Rahul,

Following may help you in same.
Code:
 awk '
  BEGIN {                                                    ####### Start begin statement here.
    FS=" *; *"                                               ####### mentioning field seprator here as "*; *"  means it will take look for all text from starting to till semi colon as mentioned in your input file
    OFS=";"                                                  ####### mentioning output field seprator as semi colon ;
  } 
  FNR==1{                                                    ####### Whenever FNR is equal to 1, this condition will be TRUE whenever a new file read. As FNR will be reset whenever a new file is being read and on other hand NR will be keep on increasing it's value till all files being read.
    c++                                                      ####### incrementing the value of a variable named c by one.
  }
  c==1 {                                                     ####### When variable C's value is 1 this will be TRUE after first file is being read.
    A[$1]=$0                                                 ####### Creating an array named A whose index is $1 and value is $0 (complete line).
  } 
  c==2 {                                                     ####### When variable named C's value is 2 which will be TRUE when second file is being read because when read second file's turn will come then FNR==1 conditon will increase variable named C's value to 1 more which will be 2 now.
    A[$2]=A[$2] OFS $1                                       ####### Taking same array named A this time taking index as $2 because $1 of sampleouput1.txt and $2 of sampleoutput2.txt are same. Assigning the value to A{$2] OFS $1 where OFS is Output fielfd seprator.
  }
  c==3 {                                                     ####### When variable named C 's valie is 3 this will be TRUE when 3rd file is being read.
    $1=A[$1]                                                 ####### now making $1's value as A[$1]; because $1 of file named sampleoutput2.txt and $1 of sampleoutput1.txt and $2 of sampleoutput3.txt are same as per input files provided.
    print                                                    ####### Performing print action now, which will print complete line then.
  }
' sampleoutput1.txt sampleoutput3.txt sampleoutput2.txt      ####### mentioning filenames now.

Thanks,
R. Singh
These 2 Users Gave Thanks to RavinderSingh13 For This Post:
# 9  
Old 07-28-2015
Quote:
Originally Posted by rahul2662
Hello SriniShoo , The script which you provided went into long running and eventually i stopped it after approx 15 minutes. Thanks a lot for your efforts
Sorry! That was untested and there was a bug
try below
Code:
awk -F ';' '
 FILENAME == ARGV[1] {A[$1] = ($2 ";" $3); next}
 FILENAME == ARGV[2] {A[$2] = A[$2] == "" ? (";;" $1) : (A[$2] ";" $1); next}
 FILENAME == ARGV[3] {A[$1] = A[$1] == "" ? (";;;" $2 ";" $3 ";" $4) : (A[$1] ";" $2 ";" $3 ";" $4)}
 END {for(x in A) print x ";" A[x]}' sampleoutput1.txt sampleoutput3.txt sampleoutput2.txt > sampleoutput1-2-3.txt

This User Gave Thanks to SriniShoo For This Post:
# 10  
Old 07-28-2015
Quote:
Originally Posted by RavinderSingh13
Hello Rahul,

Following may help you in same.
Code:
[..]
    FS=" *; *"                                               ####### mentioning field seprator here as "*; *"  means it will take look for all text from starting to till semi colon as mentioned in your input file
filenames now. [..]

Thanks,
R. Singh
Thanks Ravinder. Just one correction:

Code:
    FS=" *; *"                                               ####### using the field separator (FS) here as " *; *"  means it will consider spaces around the semicolons also part of the field separator (so that these spaces do not becomes part of the fields)

These 2 Users Gave Thanks to Scrutinizer For This Post:
# 11  
Old 07-28-2015
Thanks a lot Ravinder Sir , Scrutinizer and SriniShoo. Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Issue with awk when joining two files when field has '-' hyphen

Dear Community; I need to join two files but I am facing issues. 1st file has multiple columns. Primary (1st) columns has unique values. There are other columns out of which some has non-ascii characters as well (other language). Example File below: 1-1001JRL,BiRecurring... (5 Replies)
Discussion started by: mystition
5 Replies

2. SuSE

Facing issue configuring network

Please let me know how to configure network in suse Linux, I have configured the network using ifup and network manager, it is not giving any error but not working, using suse Linux 11.0 sp3 I have checked network connectivity is working. (0 Replies)
Discussion started by: manoj.solaris
0 Replies

3. Solaris

Facing issue while installing weblogic on Solaris 11

Hi, i am facing issue while installing weblogic on solaris..its giving me invalid argument error. solaris is intstalled on my VM. uname -a SunOS Vishal 5.10 Generic_137138-09 i86pc i386 i86pc screenshot attached. (5 Replies)
Discussion started by: Vishal Baghla
5 Replies

4. Infrastructure Monitoring

Facing Issue in Nagios 3.3

Hi, I have installed Nagios on Redhat Linux, I have installed Nagios+Plugins+NRPE on Server A (Host Server) and Nagios Plugins + nrpe on remote linux server (red hat linux) run the command on remote linux host, it returns nrpe version usr/local/nagios/libexec/check_nrpe -H localhost ... (1 Reply)
Discussion started by: manoj.solaris
1 Replies

5. Shell Programming and Scripting

Facing Issue after configuring logrotate

Hi, I have a logrotate configuration which rotates a log every night 1 min before midnight, but somehow its not working and unfortunately not showing any error message as well. Sharing the code for the cron job as well as the conf file, I am using, if some one coule help me..whats wrong with... (2 Replies)
Discussion started by: Neeryan
2 Replies

6. Programming

SQL Developer JOINS / GROUP BY issue.

Am having a nightmare with a certain piece of code.. have tried almost everything and just cannot see what the issue is.. CREATE OR REPLACE VIEW TOP_EARNER_PER_LOCATION AS SELECT E.FIRST_NAME || ' ' || E.LAST_NAME AS EMPLOYEE_NAME, L.REGIONAL_GROUP AS REGIONAL_GROUP, ... (1 Reply)
Discussion started by: U_C_Dispatj
1 Replies

7. Shell Programming and Scripting

Issue with Joining lines from two files

Hi, I have two text files, that need their data joining/concatenation. 'Paste' works for this. But have an issue when there is mismatch in number of rows in each file. E.g. (main file) File1 - has 20 rows File2 - has 30 rows. Command 'paste file1 file2 > file3' joins all lines. I want the... (4 Replies)
Discussion started by: sharath160
4 Replies

8. Shell Programming and Scripting

Facing issue while using xsltproc tp parse XML in bash

I have written a bash script which opens a folder, reads all the *.xml files in it, and pulls the required data that i need from XML tags. I am using xsltproc (my xsl name) (my xml folder location/*.xml) and running this in a for each loop The problem is that some XML files are having special... (3 Replies)
Discussion started by: shivashankar.g
3 Replies

9. Shell Programming and Scripting

Facing issue in Solaris OS in crontab for running shell script

Hello i have a shell script. it is running fine when i manually run at command prompt using following command ./script_file but while running shell script from crontab, it is giving error in each line. (2 Replies)
Discussion started by: mabrar
2 Replies

10. HP-UX

Facing an issue related to cronjob

Dear All, I am facing an issue related to cronjob and explained below is the case study: 1. I have a java class named "DmCheckRenditionQueue.java" and placed under "/cpc/documentum/product/5.2.5/tomcat/webapps/rendition" 2. When I am using the command "/usr/openv/java/jre/bin/java -cp... (1 Reply)
Discussion started by: parindam
1 Replies
Login or Register to Ask a Question