| Newsgroups |
gmane.editors.sed.user |
| Message-ID |
<[email protected]> |
The sed version doesn't work since it is using the nonportable construct like [^\n]
However, all's not lost and you can try the below mentioned.
All the data is being stored in the pattern space.
sed -e '
# ps = ^IDx;s1;s2;s3;...\nIDy;r1;r2;r3;...\nIDcurr;a1;a2;a3;...$
:loop
$q; N
y/\n_/_\n/
s/\(_\([^;]*\);[^_]*\)\(_.*\)_\2\([^_]*\)$/\1\4\3/; tdone
s/^\(\([^;]*\);[^_]*\)\(_.*\)_\2\([^_]*\)$/\1\4\3/; tdone
s/\(_\([^;]*\);[^_]*\)_\2\([^_]*\)$/\1\3/; tdone
s/^\(\([^;]*\);[^_]*\)_\2\([^_]*\)$/\1\3/
:done
y/\n_/_\n/
bloop
' fileA.txt fileB.txt fileC.txt ..........
-Rakesh
---In [email protected], <yildirim_ufuk@...> wrote :
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 dave_br@... mailto:dave_br@... [sed-users]" <[email protected] mailto:[email protected]> wrote:
On Wed, 18 May 2016 17:17:49 +0300, "Ufuk YILDIRIM
yildirim_ufuk@... mailto:yildirim_ufuk@... [sed-users]" <[email protected] mailto:[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 <dave_br@... mailto:dave_br@...>
------------------------------------
--
------------------------------------
Yahoo Groups Links
[Non-text portions of this message have been removed]
[Non-text portions of this message have been removed]