Merge two files line by line and column by column


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Merge two files line by line and column by column
# 15  
Old 11-11-2012
Quote:
Originally Posted by pamu
Code:
awk -F "|" 'NR==FNR{X[$1]=X[$1]?X[$1]"|"$2:$2;next}  # Read fileB - if X[$1] present then append $2 to the X[$1]. if not then assin $2 to X[$1]

{if(X[$1]){print $0"|"X[$1]}else{print $0"-"}}' fileB fileA   # Read fileA - if $1 presnet it fileA then print $0 and X[$1]. if not the print $0"-"

I hope this helps youSmilie

pamu

Thanks , I understood your logic but not code.

I mean in first sentence we are using fileB only
and in second sentence fileB and fileA both.
How code decides it
# 16  
Old 11-11-2012
Quote:
Originally Posted by Harshal22
Thanks , I understood your logic but not code.

I mean in first sentence we are using fileB only
and in second sentence fileB and fileA both.
How code decides it
First we read fileB and stores it into array X[].
Then we read second fileA and print array X[] with fileA.
This User Gave Thanks to pamu For This Post:
# 17  
Old 11-11-2012
Quote:
Originally Posted by Harshal22
Thanks , I understood your logic but not code.

I mean in first sentence we are using fileB only
and in second sentence fileB and fileA both.
How code decides it
Let's look at the code again in a slightly different format:
Code:
1 awk -F "|" '
2 NR==FNR{
3       X[$1]=X[$1]?X[$1]"|"$2:$2
4       next
5 }     
6 {     if(X[$1]) print $0"|"X[$1]
7       else print $0"|-"
8 }' fileB fileA

The test on line 2 NR==FNR selects lines where the input line number (NR) and the input line number within the current file (FNR) are the same. This selects lines that are in the 1st file only.

The "next" command on line 4 tells awk to skip the remainder of the script for the current line and start processing the next input line. So, the commands on lines 6 and 7 are skipped when processing lines from the first file and are only used when processing ilnes from the second file.

Note that to meet the output requjirements you originally stated, the additional
"|" in red needs to be added to your script.
These 2 Users Gave Thanks to Don Cragun For This Post:
# 18  
Old 11-12-2012
Why don't you modify your ORACLE selection in the first place to accomodate your needs?
# 19  
Old 11-27-2012
thanks

Last edited by Harshal22; 11-27-2012 at 12:45 PM.. Reason: My mistake :o
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bring values in the second column into single line (comma sep) for uniq value in the first column

I want to bring values in the second column into single line for uniq value in the first column. My input jvm01, Web 2.0 Feature Pack Library jvm01, IBM WebSphere JAX-RS jvm01, Custom01 Shared Library jvm02, Web 2.0 Feature Pack Library jvm02, IBM WebSphere JAX-RS jvm03, Web 2.0 Feature... (10 Replies)
Discussion started by: kchinnam
10 Replies

2. Shell Programming and Scripting

Insert data in first column(if blank) from previous line first column

Dear Team I need to insert field(which is need to taken from previous line's first field) in first column if its blank. I had tried using sed but not find the way. Detail input and output file as below. Kindly help for same. INPUT: SCGR SC DEV DEV1 NUMDEV DCP ... (7 Replies)
Discussion started by: jaydeep_sadaria
7 Replies

3. Shell Programming and Scripting

merge same pattern of same column in one line

Hello, I have some problem in the modified shell script. I would like to merge the same word in column 1 in one line. Example : A1 B1 2 A2 B1 4 A3 B1 7 A1 B2 1 A2 B2 10 A3 B2 8 Expected output : A1 B1 B2 2 1 A2 B1 B2 4 10 A3 ... (6 Replies)
Discussion started by: awil
6 Replies

4. Shell Programming and Scripting

Extract Line and Column from CSV Line in ksh or bash format

Hi, I was doing some research and can't seem to find anything. I'm trying to automate a process by creating a script to read a csv line and column and assigning that value to a variable for the script to process it. Also if you could tell me the line and column if it's on another work ... (3 Replies)
Discussion started by: vpundit
3 Replies

5. Shell Programming and Scripting

1st column,2nd column on first line 3rd,4th on second line ect...

I need to take one column of data and put it into the following format: 1st line,2nd line 3rd line,4th line 5th line,6th line ... Thanks! (6 Replies)
Discussion started by: batcho
6 Replies

6. Shell Programming and Scripting

search a string in a particular column of file and return the line number of the line

Hi All, Can you please guide me to search a string in a particular column of file and return the line number of the line where it was found using awk. As an example : abc.txt 7000,john,2,1,0,1,6 7001,elen,2,2,0,1,7 7002,sami,2,3,0,1,6 7003,mike,1,4,0,2,1 8001,nike,1,5,0,1,8... (3 Replies)
Discussion started by: arunshankar.c
3 Replies

7. UNIX for Dummies Questions & Answers

loop? print max column in each line for 800 files and merge

Hello, I have 800 or so files with 3 columns each and >10000 lines each. For each file and each line I would like to print the maximum column number for each line. Then I would like to 'paste' each of these files together (column-wise) so that the file with expression in label '_1' is the... (6 Replies)
Discussion started by: peanuts48
6 Replies

8. Shell Programming and Scripting

How to manipulate first column and reverse the line order in third and fourth column?

How to manipulate first column and reverse the line order in third and fourth column as follws? For example i have a original file like this: file1 0.00000000E+000 -1.17555359E-001 0.00000000E+000 2.00000000E-002 -1.17555359E-001 0.00000000E+000 ... (1 Reply)
Discussion started by: Max Well
1 Replies

9. Shell Programming and Scripting

Insert first line of a file to first column of remaining files

I want to extraxt data from a html table the html file is downloaded from UG / PG Univ - Exam.Results April/May 2008 After processing the html file using sed i got the output like this 11305106082,RANJANI R, CS1251,20,69,P CS1302,20,45,P EC1006,20,52,P EC1351,20,53,P... (5 Replies)
Discussion started by: a_artha
5 Replies

10. Shell Programming and Scripting

compare the column from 3 files and merge that line

I have 3 file, each of has got 80000 records. file1.txt ----------------------- ABC001;active;modify;accept; ABC002;notactive;modify;accept; ABC003;notactive;no-modify;accept; ABC004;active;modify;accept; ABC005;active;no-modify;accept; file2.txt ---------------------------... (8 Replies)
Discussion started by: ganesh_mak
8 Replies
Login or Register to Ask a Question