LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   Matching Pattern & needed desired Output (https://www.linuxquestions.org/questions/linux-software-2/matching-pattern-and-needed-desired-output-4175458459/)

sumit.inform 04-17-2013 07:16 AM

Matching Pattern & needed desired Output
 
Hi,

I have below pattern

ABCD 1-2-3-4
EFGH 5-6-7-8
ABCD 8-7-4-9
IJKL 9-3-2-5
EFGH 6-9-3-6
IJKL 8-5-9-3
---- -------

Now output is needed like
ABCD : 1-2-3-4,8-7-4-9
EFGH : 5-6-7-8,6-9-3-6
IJKL : 9-3-2-5,8-5-9-3

I am working on TCL script. If solotion is in TCL then much benificial for me.

Thanks in advance.

schneidz 04-17-2013 08:47 AM

sorry, my tcl is rusty but hopefully this quick-and-dirty bash will help:
Code:

awk '{print $1}' sumit.txt | sort | uniq > sumit.key
for key in `cat sumit.key`
do
  echo $key : `grep $key sumit.txt | awk '{print $2}'`
done | sed s/" : "/:/g | sed s/" "/,/ > sumit.out

edit: clean-ups suggested by chrism01.

sumit.inform 04-17-2013 10:16 AM

Hi schneidz,

This help is simply awesome.By going line to line definitely I am finding my desired Pattern.
Could you please help me to understand * point in your code

awk '{print $1}' sumit.txt | sort | uniq > sumit.key ---->> This provide sorted and unique value(I understand)
for key in `cat sumit.key` ------> Taking unique value of first column (I understand)
do
echo $key : `grep $key sumit.txt` --> *What is role of echo $key : as i know 'grep $key sumit.txt' gets patterns matching.
done | sed s/" "//g | sed s/:,/:/ > sumit.out


So if i understand this logic about "echo $key : "I will convert this into TCL.
Using of sumit.txt & sumit.key is really attractive :).

Thanks
Sumit Sharma

TheLexx 04-17-2013 03:32 PM

Humm, I think I could bang something out in Python quickly, but I'm no good in TCL. Python's bult-in data type "dictionary" looks like a quick solution to this problem. If you must use TCL you might try a TCL forum.

chrism01 04-17-2013 11:25 PM

On my system, schneidz's code gives this output
Code:

ABCD:ABCD1-2-3-4ABCD8-7-4-9
EFGH:EFGH5-6-7-8EFGH6-9-3-6
IJKL:IJKL9-3-2-5IJKL8-5-9-3

close but no cigar :)

I tweaked it thus
Code:

awk '{print $1}' sumit.txt | sort | uniq > sumit.key
for key in `cat sumit.key`
do
    echo -n "$key : "
    for val in `grep $key sumit.txt |cut -d' ' -f2`
    do
        echo -n $val","
    done
    echo
done |sed 's/,$//' > sumit.out

which seems to give the reqd
Code:

ABCD : 1-2-3-4,8-7-4-9
EFGH : 5-6-7-8,6-9-3-6
IJKL : 9-3-2-5,8-5-9-3

HTH

sumit.inform 04-18-2013 02:48 PM

Solved
 
Simply Osm ....Thanks chrism01 & schneidz. No doubt you both have solved my problem in easy manner. I am trying to convert this into TCL otherwise my last option is to embedded given shell code with my TCL script.

@TheLexx , I am looking for TCL forum here if possible please provide me link for any suitable.

Anyways Thanks you Guys ...I will look for more post


All times are GMT -5. The time now is 01:53 AM.