hi,
i have records like this
supplier,product, persons involved
10,150,ravi@yahoo.com
30,200,ravi@yahoo.com
20,111,payal@gmail.com
40,211,ravi@yahoo.com
i want to write a command which displays values like this
10,30,40,ravi@yahoo.com
for ravi@yahoo.com if there are n number of suppliers all need to be concatenated as shown above.
can any one help me on this
kshji
August 10, 2009, 1:33am
2
#!/bin/ksh
# or bash, bsh, ...
# join.sh
prev=""
saveid=""
sort -t , -k 3,3 | while IFS="," read id num email xstr
do
[ "$prev" = "" ] && prev="$email" #1st line
[ "$prev" = "$email" ] && saveid="$saveid$id," && continue
# buf out
echo "$saveid$prev"
prev="$email"
done
# last buf
echo "$saveid$prev"
chmod a+rx join.sh
# using ex.
cat file | ./join.sh | ...
./join.sh < file > file2
Try...
awk -F"," 'NR>1{arr[$3]=arr[$3]","$1}END {for (i in arr)print arr","i}' yourfile
thanks malcomex999
i tried with the approach given by you
but it is not working
plz find the snapshot of the same
ibm4@raclin13:~> cat testfile.txt
10,150,selvas12@yahoo.com
20,213,selvas12@yahoo.com
30,553,rahim@lycos.com
222,666,redial@dial.com
ibm4@raclin13:~> awk -F"," 'NR>1{arr[$3]=arr[$3]","$1}END {for (i in arr)print arr[i]","}' testfile.txt
,222,
,20,
,30,
--------------------------------------------------
hi kshji
thanks for the response
after creating the script (join.sh)
i executed the same command as below
but it is giving some error
cat file | ./join.sh | ...
can u tell me what is the meaning of ...
i just gave
cat file | ./join.sh
and ./join.sh < file > file2
and what i got was not meeting my requirement
EXpecting your replies
trichyselva:
thanks malcomex999
i tried with the approach given by you
but it is not working
plz find the snapshot of the same
ibm4@raclin13:~> cat testfile.txt
10,150,selvas12@yahoo.com
20,213,selvas12@yahoo.com
30,553,rahim@lycos.com
222,666,redial@dial.com
ibm4@raclin13:~> awk -F"," 'NR>1{arr[$3]=arr[$3]","$1}END {for (i in arr)print arr[i]","}' testfile.txt
,222,
,20,
,30,
--------------------------------------------------
hi kshji
thanks for the response
after creating the script (join.sh)
i executed the same command as below
but it is giving some error
cat file | ./join.sh | ...
can u tell me what is the meaning of ...
i just gave
cat file | ./join.sh
and ./join.sh < file > file2
and what i got was not meeting my requirement
EXpecting your replies
that's not what i have posted, cos u have 4got "i" at the end be4 the file.
so please copy exactly what i have posted for u and try....which is...
awk -F"," 'NR>1{arr[$3]=arr[$3]","$1}END {for (i in arr)print arr","i}' yourfile
---------- Post updated at 03:24 PM ---------- Previous update was at 03:07 PM ----------
I just tested it now and it is working fine except it prints extra comma at the begening so u can add sed to remove that comma as below...
awk -F"," 'NR>1{arr[$3]=arr[$3]","$1}END {for (i in arr)print arr","i}' yourfile | sed 's/^\,//'
following should be close to what you need:
# nawk -F"," '{OFS=",";t[$3]?t[$3]=t[$3]","$1:t[$3]=$1}END{for (i in t){print t,i}}' infile
20,payal@gmail.com
10,30,40,ravi@yahoo.com
HTH
kshji
August 10, 2009, 8:29am
7
Only examples to use, because defination not included input and output (file, cmd, ...).
Script read data from stdin and write data to the stdout. Caller can tell what is stdin and stdout.
From file to the terminal:
./join.sh < testfile.txt
Output is:
20,payal@gmail.com
20,30,40,ravi@yahoo.com
If you like to save result then ex. to file:
./join.sh < testfile.txt > result.txt
thanks all
I can get what I needed