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
# 8  
Old 11-07-2012
Quote:
Originally Posted by rdrtx1
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

Quote:
Originally Posted by Scrutinizer
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


Both solutions are working , but no of columns are varies for each record, so can you generalize this solution for n no of column
# 9  
Old 11-07-2012
try:
Code:
awk -F"|" 'NR==FNR{for(i=1;i<=NF;i++){a[$1,i]=$i;}; next;}{printf $1"|";for(i=2;i<=NF;i++){printf a[$1,i]" -> "$i (a[$1,(i+1)]?"|":"\n")}}' File1 File2

This User Gave Thanks to rdrtx1 For This Post:
# 10  
Old 11-07-2012
Code:
awk -F "|" 'BEGIN { OFS = "|" }
FNR == NR {
        a[$1] = NF
        for(i = 2; i <= NF; i++) a[$1,i] = $i
        next
}
{       for(i = 2; i <= NF; i++) $i = a[$1,i] " -> " $i
        print
}' File1 File2

This User Gave Thanks to Don Cragun For This Post:
# 11  
Old 11-11-2012
Quote:
Originally Posted by rdrtx1
try:
Code:
awk -F"|" 'NR==FNR{for(i=1;i<=NF;i++){a[$1,i]=$i;}; next;}{printf $1"|";for(i=2;i<=NF;i++){printf a[$1,i]" -> "$i (a[$1,(i+1)]?"|":"\n")}}' File1 File2

Quote:
Originally Posted by Don Cragun
Code:
awk -F "|" 'BEGIN { OFS = "|" }
FNR == NR {
        a[$1] = NF
        for(i = 2; i <= NF; i++) a[$1,i] = $i
        next
}
{       for(i = 2; i <= NF; i++) $i = a[$1,i] " -> " $i
        print
}' File1 File2

Hi Now requirement has changed. Now I dont need to merge two columns of different files but I have merged output in single file. still some word processing remaining

file A:
Code:
1|fkgk
2|sfkgn
3|kfhd
4|hfj
5|fghfd
6|dsfjs
7|fbg.
8|dfds
9|efh

fileB
Code:
2|colour->red
3|colour->green
3|height->3
5|colour->
5|height->6
5|weight->4
8|colour->blue
8|weight->

I want to show output as follows
Code:
1|fkgk|-
2|sfkgn|colour->red
3|kfhd|colour->green|height->3
4|hfj|-
5|fghfd|colour->|height->6|weight->4
6|dsfjs|-
7|fbg|-
8|dfds|colour->blue|weight->
9|efh|-

---------- Post updated at 01:21 PM ---------- Previous update was at 01:18 PM ----------

I have tried following but this is not working Smilie
Code:
#!/bin/sh
rm debug.log
rm $3
fileA=$1
fileB=$2
fileC=$3
var=

cat $fileA|while read lineA
do
#get handle of fileA
Handle=`echo $lineA|awk -F"|" '{print $1}'`
echo "debug fileA handle:" $Handle >>debug.log
lineBhandle=`echo $
lineB=`grep -w $Handle $fileB`
echo "debug lineB :"$lineB>>debug.log
if [ -z "$lineB" ]
then
    echo "$lineA    -" >> $fileC
    echo "inside if ">>debug.log
else
    cat $fileB|while read lineC
    do
        file2handle=`echo $lineC|awk -F"|" '{print $1}'`
        echo "debug fileB handle:"$file2handle>>debug.log
        if [ $Handle -eq $file2handle ]
        then
            param=`echo $lineC|awk -F"|" '{print $2}'`
            var="$var $param"
        fi
    done
    echo "debug param:"$var>>debug.log
    echo "$lineA    $var">>$fileC
fi
done

# 12  
Old 11-11-2012
Try

Code:
awk -F "|" 'NR==FNR{X[$1]=X[$1]?X[$1]"|"$2:$2;next}{if(X[$1]){print $0"|"X[$1]}else{print $0"-"}}' fileB fileA

This User Gave Thanks to pamu For This Post:
# 13  
Old 11-11-2012
Quote:
Originally Posted by pamu
Try

Code:
awk -F "|" 'NR==FNR{X[$1]=X[$1]?X[$1]"|"$2:$2;next}{if(X[$1]){print $0"|"X[$1]}else{print $0"-"}}' fileB fileA


It is working, could you please explain me in layman terms how this code woks
# 14  
Old 11-11-2012
Quote:
Originally Posted by Harshal22
It is working, could you please explain me in layman terms how this code woks
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

Last edited by pamu; 11-11-2012 at 03:36 PM..
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