LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 04-17-2013, 07:16 AM   #1
sumit.inform
LQ Newbie
 
Registered: Mar 2013
Posts: 19
Blog Entries: 2

Rep: Reputation: Disabled
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.
 
Old 04-17-2013, 08:47 AM   #2
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fedora-35
Posts: 5,313

Rep: Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918
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.

Last edited by schneidz; 04-18-2013 at 04:44 PM.
 
1 members found this post helpful.
Old 04-17-2013, 10:16 AM   #3
sumit.inform
LQ Newbie
 
Registered: Mar 2013
Posts: 19

Original Poster
Blog Entries: 2

Rep: Reputation: Disabled
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
 
Old 04-17-2013, 03:32 PM   #4
TheLexx
Member
 
Registered: Apr 2013
Distribution: Gentoo
Posts: 79

Rep: Reputation: Disabled
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.
 
Old 04-17-2013, 11:25 PM   #5
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,360

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
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
 
1 members found this post helpful.
Old 04-18-2013, 02:48 PM   #6
sumit.inform
LQ Newbie
 
Registered: Mar 2013
Posts: 19

Original Poster
Blog Entries: 2

Rep: Reputation: Disabled
Smile 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
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Matching patterns or partial pattern matching yaplej Programming 6 12-16-2012 10:21 AM
[SOLVED] awk with pipe delimited file (specific column matching and multiple pattern matching) lolmon Programming 4 08-31-2011 12:17 PM
Perl Script needed to be reversed to output matching, not non-matching 0bfuscated Programming 2 07-20-2010 10:51 AM
matching a pattern in a file & prefixing a word shivarajM Linux - Software 3 05-28-2009 01:14 PM
pattern matching help needed. squirrel001 Linux - Newbie 2 06-17-2008 06:28 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 12:57 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration