wood burning stoves 2.0*
The moose likes Linux / UNIX and the fly likes Column number of a search pattern Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Engineering » Linux / UNIX
Bookmark "Column number of a search pattern" Watch "Column number of a search pattern" New topic
Author

Column number of a search pattern

Pradeep Sahoo
Ranch Hand

Joined: Oct 13, 2002
Posts: 46
I am not sure if this is the right forum to ask this question. Still I thought I should try because I am desperately seeking a solution for this.
I want to search for a pattern in a single line of text and to identify the column position if this pattern is found in the text.
E.g.
If the text is
THIS IS HELLO WORLD and I search for the pattern HELLO, I should get the output as 9 (Position of H assuming 'T' in THIS has a position 1)
I am working on Solaris - which means I have all the standard tools e.g. sed, awk, grep etc available. But I am not sure of the logic.
I would be greatful if anybody can help on this.
Many Thanks in advance,
Pradeep
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16160
    
  21

Here's a rough stab in Perl:

This is untested code, but it should be reasonably close.


Customer surveys are for companies who didn't pay proper attention to begin with.
Leslie Chaim
Ranch Hand

Joined: May 22, 2002
Posts: 336
In Perl there's no need to satb
a dart can do as well...
<code><pre>perl -lne 'print length ($`) + 1 if /HELLO/' file</code></pre>
or if you want the line thing:
<code><pre>perl -lne 'print "$.:", length ($`) + 1 while /HELLO/g'</code></pre>
[ July 28, 2003: Message edited by: Leslie Chaim ]
[ July 29, 2003: Message edited by: Leslie Chaim ]

Normal is in the eye of the beholder
Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11490
    
  95

Does Solaris come with Perl already installed?
If not, then you could try using awk:
<code><pre>echo "This is Hello World" | awk '{print index($0, "Hello");}'
9</pre></code>
If the string you are looking for is variable, you could set it programatically:
<code><pre>SEARCH_FOR="Hello";
echo "This is Hello World" | awk -vFIND="$SEARCH_FOR" '{print index($0, FIND);}'
9</pre></code>
Regards, Andrew


The Sun Certified Java Developer Exam with J2SE 5: paper version from Amazon, PDF from Apress, Online reference: Books 24x7 Personal blog
Pradeep Sahoo
Ranch Hand

Joined: Oct 13, 2002
Posts: 46
Thanks a lot Tim, Leslie and Andrew,
Its incredible to see so many different solutions for such a trivial problem.
Thanks once again,
Pradeep
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16160
    
  21

I liked the length(&`).
It's Perl! There's More Than One Way To Do It! (TM)
[ July 29, 2003: Message edited by: Tim Holloway ]
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Column number of a search pattern