Reputation: 967
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.).
1#first#scott#prince01
2#second#scott#prince02
1#scott#prince01#88129
2#scott#prince02#34
1#first#scott#prince01#1#scott#prince01#88129
2#second#scott#prince02#2#scott#prince02#34
Upvotes: 0
Views: 116
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
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,
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
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
$ pr -mtJS'#' f1 f2
1#first#scott#prince01#1#scott#prince01#88129
2#second#scott#prince02#2#scott#prince02#34
Using paste
$ paste -d'#' f1 f2
1#first#scott#prince01#1#scott#prince01#88129
2#second#scott#prince02#2#scott#prince02#34
Upvotes: 0
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 separatorUpvotes: 4