Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
Hi. I have two data files a.dat and b.dat.
a.dat contains :
s:b:
h:g:wq
and b.dat contains
c:b
d:b
f:b
w:r
My output should be :s:b:c,d,f
h:g:wq,r.Hpw can i accomplish this task in shell scripting
Regards
Arun

It's not pretty. Read a.dat and if it's line 1 grab fields 1, 2, 3 of file b and build the required string.
if it's line 2 of a.dat, grab the 2nd field of line 4 of b.dat, and build the string:
#!/bin/bash
cnt=0
while read line
do
((cnt+=1))
if [[ $cnt -eq 1 ]]
then
f1=$(awk ' BEGIN { FS=":" } { if (NR == 1) { print $1 ; exit } } ' b.dat)
f2=$(awk ' BEGIN { FS=":" } { if (NR == 2) { print $1 ; exit } } ' b.dat)
f3=$(awk ' BEGIN { FS=":" } { if (NR == 3) { print $1 ; exit } } ' b.dat)
line="${line},${f1},${f2},${f3}"
echo $line
fiif [[ $cnt -eq 2 ]]
then
f4=$(awk ' BEGIN { FS=":" } { if (NR == 4) { print $2 ; exit } } ' b.dat)
line="${line},${f4}."
echo $line
fidone < a.dat

Thanks for the message. But what i have shown is only a sample. What i meant was my first file 2nd field matches with 2nd file 2nd field. Delimiter is ":" (Colon). My final output should be 1 st file 1st field,1st file 2nd field which is comman to both and third field should be 2nd file 1st field.
Regards
Arun

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |