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
# 1  
Old 11-07-2012
Merge two files line by line and column by column

Hi All,

I have two files having oracle query result. I want to merge to files line by line and also with column

File1
Code:
23577|SYNC TYPE
23578|Order Number|ConnectionState
23585|Service State|Service Name

File2
Code:
23577|AR Alarm Sync
23578|A5499|9
23585|7|test_nov7

Result should be

Code:
23577|SYNC TYPE -> AR Alarm Sync
23578|Order Number -> A5499|ConnectionState -> 9
23585|Service State -> 7|Service Name -> test_nov7

Moderator's Comments:
Mod Comment Please use code tags for code and data

Last edited by Scrutinizer; 11-07-2012 at 02:07 PM.. Reason: code tags
# 2  
Old 11-07-2012
Code:
join -t"|" file1 file2

# 3  
Old 11-07-2012
Quote:
Originally Posted by bipinajith
Code:
join -t"|" file1 file2

This is adding columns of file B after columns of fileA.
I need one column of fileA and then fileB
# 4  
Old 11-07-2012
Then change the order of params:-
Code:
join -t"|" file2 file1

# 5  
Old 11-07-2012
Quote:
Originally Posted by bipinajith
Then change the order of params:-
Code:
join -t"|" file2 file1

I want alternate columns of fileA and fileB like
col1of A -> col1ofB |col2ofA -> col2ofB
# 6  
Old 11-07-2012
try:
Code:
awk -F"|" 'NR==FNR{a[$1]=$1; b[$1,2]=$2; c[$1,3]=$3; next;}{print a[$1]"|"b[$1,2]" -> "$2 (c[$1,3]?("|"c[$1,3]" -> "$3): "")}' File1 File2

# 7  
Old 11-07-2012
Try:
Code:
awk 'NR==FNR{A[$1]=$2; B[$1]=$3; next} A[$1]{$2=$2 " -> " A[$1]} B[$1]{$3=$3 " -> " B[$1]}1' FS=\| OFS=\| file2 file1

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