Sigularity
Sigularity

Reputation: 967

How to merge lines in 2 difference files(linux awk)

Can you help me to merge two files? Actually its line should be merged, and each line has the same ID(eg. 1 or 2 in this example.).

File1

1#first#scott#prince01
2#second#scott#prince02

File2

1#scott#prince01#88129
2#scott#prince02#34 

Final

1#first#scott#prince01#1#scott#prince01#88129
2#second#scott#prince02#2#scott#prince02#34

Upvotes: 0

Views: 116

Answers (4)

James Brown
James Brown

Reputation: 37404

In awk:

$ awk 'BEGIN{FS=OFS="#"}NR==FNR{a[$1]=$0;next}($1 in a){print a[$1],$0}' file1 file2
1#first#scott#prince01#1#scott#prince01#88129
2#second#scott#prince02#2#scott#prince02#34

Explained:

$ awk '        
BEGIN{ 
    FS=OFS="#"      # set FS AND OFS to #
}
NR==FNR {           # for the first file
    a[$1]=$0        # hash records, use $1 as key
    next            # skip to next record
}
($1 in a) {         # for the second file, if key found in the hash
    print a[$1],$0  # output
}' file1 file2

Upvotes: 0

CWLiu
CWLiu

Reputation: 4043

$ awk '{a[FNR]= a[FNR]=="" ? $0 :a[FNR]"#"$0}END{for(i in a)print a[i]}' File1 File2 
1#first#scott#prince01#1#scott#prince01#88129
2#second#scott#prince02#2#scott#prince02#34 

Brief explanation,

  • store the specific value for line #FNR in a[FNR]
  • a[FNR]= a[FNR]=="" ? $0 :a[FNR]"#"$0: save it as $0 if no initial value was in a[FNR], store a[FNR]"#"$0 otherwise.

Upvotes: 0

Akshay Hegde
Akshay Hegde

Reputation: 16997

Some more way ( I assume files are ordered )

Input

$ cat f1
1#first#scott#prince01
2#second#scott#prince02

$ cat f2
1#scott#prince01#88129
2#scott#prince02#34 

Using

$ pr -mtJS'#' f1 f2
1#first#scott#prince01#1#scott#prince01#88129
2#second#scott#prince02#2#scott#prince02#34

Using

$ paste -d'#' f1 f2
1#first#scott#prince01#1#scott#prince01#88129
2#second#scott#prince02#2#scott#prince02#34 

Upvotes: 0

RomanPerekhrest
RomanPerekhrest

Reputation: 92854

In your simple case it would be enough to use join command:

join -t'#' File1 File2

The output:

1#first#scott#prince01#scott#prince01#88129
2#second#scott#prince02#scott#prince02#34

  • -t'#' - specifying field separator

Upvotes: 4

Related Questions