• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Column number of a search pattern

 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Saloon Keeper
Posts: 27762
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here's a rough stab in Perl:

This is untested code, but it should be reasonably close.
 
Ranch Hand
Posts: 336
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
author and jackaroo
Posts: 12200
280
Mac IntelliJ IDE Firefox Browser Oracle C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Pradeep Sahoo
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 27762
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
reply
    Bookmark Topic Watch Topic
  • New Topic