Re: Combine Text Files
"Ufuk Yildirim [email protected] [sed-users]" <[email protected]> Thu, 19 May 2016 12:46:14 +0000 (UTC)
| Newsgroups | gmane.editors.sed.user |
|---|---|
| Message-ID | <[email protected]> |
With small test files, awk version Davide Brini suggested seems to work perfectly. Thank you so much.
sed version, on the other hand, is not working as I wanted. All it does is put all the lines from the txt files in a file in order, which is problematic since some students appear twice.
ps. I am currently on Mac. No idea if that changes anything. Will try the sed version again on linux, if that makes any difference.
Thanks everyone for your help.
All the best,
Ufuk
On Wednesday, 18 May 2016, 19:38, "Davide Brini [email protected] [sed-users]" <[email protected]> wrote:
On Wed, 18 May 2016 17:17:49 +0300, "Ufuk YILDIRIM
[email protected] [sed-users]" <[email protected]> wrote:
> fileA.txt
>
>
> ID111;a;a;a;a;a;a;a;a;a
> ID222222;a;a;a;a;a;a;a;a;a
> ID3333;a;a;a;a;a;a;a;a;a
>
>
> FileB.txt
>
>
> ID111;b;b;b;b;b;b;b;b;b;b;b;b;b;b;b
> ID44;b;b;b;b;b;b;b;b;b;b;b;b;b;b;b
> ID3333;b;b;b;b;b;b;b;b;b;b;b;b;b;b;b
> ID555;b;b;b;b;b;b;b;b;b;b;b;b;b;b;b
>
>
>
>
> result.txt
> ID111;a;a;a;a;a;a;a;a;a;b;b;b;b;b;b;b;b;b;b;b;b;b;b;b
> ID222222;a;a;a;a;a;a;a;a;a
> ID3333;a;a;a;a;a;a;a;a;a;b;b;b;b;b;b;b;b;b;b;b;b;b;b;b
> ID44;b;b;b;b;b;b;b;b;b;b;b;b;b;b;b
> ID555;b;b;b;b;b;b;b;b;b;b;b;b;b;b;b
A possible approach with (GNU) sed:
sed '
:a
$!{
N
ba
}
s/$/\n/
:loop
s/\(ID[^;]*;\)\([^\n]*\)\n\(.*\)\1\([^\n]*\)\n/\1\2;\4\n\3/
t loop
s/\n$//' filea.txt fileb.txt
Another way is with awk:
awk -F';' -v OFS=';' '
NR==FNR{
id=$1; sub(/^[^;]*;/, ""); a[id]=$0; next
}
{
id=$1; sub(/^[^;]*;/, ""); sep=(id in a)?";":""; a[id]=a[id] sep $0
}
END {
for (id in a) {
print id";"a[id]
}
}
' filea.txt fileb.txt
--
D.
------------------------------------
Posted by: Davide Brini <[email protected]>
------------------------------------
--
------------------------------------
Yahoo Groups Links
[Non-text portions of this message have been removed]