Thread: find string in line

  1. #1
    Registered User
    Join Date
    May 2011
    Posts
    116

    find string in line

    hi all!

    I'm writing a bash script and I want to find how many times a specific string appears in every line of a file.
    The string I'm looking for is <grow
    The string might be a substring of another string such as:
    old#<grown or young><growup
    Is there a way to achieve that?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Something suitably cryptic...
    $ perl -n -e '$n = ( $_ =~ s/grow//g );print "$n\n";'
    old#<grown or young><growup
    2
    grow grow grow
    3
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Dec 2011
    Posts
    51
    Probably less bullet proof than Salem's, but less cryptic

    grep -w grow /tmp/test |wc -w

    where /tmp/test is:
    grrow grow grow
    old#<grown or young><growup

  4. #4
    Registered User
    Join Date
    May 2011
    Posts
    116
    Quote Originally Posted by Salem View Post
    Something suitably cryptic...
    $ perl -n -e '$n = ( $_ =~ s/grow//g );print "$n\n";'
    old#<grown or young><growup
    2
    grow grow grow
    3
    Thank you for your answer.I have a few questions..
    Is this suitable for a bash script?
    Also I only want the substring <grow
    If there's a mere grow without the < in the front I dont want to count it.

  5. #5
    Registered User
    Join Date
    Dec 2011
    Posts
    51
    How does you script looks like at the moment? Did you try it?
    Now if what you really want is to match the pattern /<grow/ then use this pattern instead of /grow/
    If you use grep, you would have to remove the -w switch.

  6. #6
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by quo View Post
    Thank you for your answer.I have a few questions..
    Is this suitable for a bash script?
    It is in the sense that perl is standard most (but not all) places bash is standard. It isn't in the sense that some people might call it heavy-handed.

    This is a fairly easy thing to do if you break the task into parts (which are hard to spot in one liners), and there are oodles of ways to do it. My advice is that you write a simple script to experiment with and start reading about the use of grep and sed. Might take you a bit longer than if you just cut n' paste someone else's code, but OTOH, next time you want to do something like this (it's also a fairly common kind of task) you'll have a better idea of how to go about it.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  7. #7
    Registered User
    Join Date
    May 2011
    Posts
    116
    Quote Originally Posted by mariostg View Post
    How does you script looks like at the moment? Did you try it?
    Now if what you really want is to match the pattern /<grow/ then use this pattern instead of /grow/
    If you use grep, you would have to remove the -w switch.
    It's just a test script so all it does is find the pattern /<grow/ in a file

    Code:
    #!/bin/bash
     
    grep '\<grow' ./test | wc -w
    for the following data(./test)

    <grow fffffff uuuuuuuu eeeeeeee<growup
    ddddddd iiiiiiii rrrrrrrr<growold <grow_hhhh grow
    grow<s yyyyyy vvvvvyoung><growgrow ccccc bbbbbbbold#<growup

    it should return 6 as the accepted are:


    <grow fffffff uuuuuuuu eeeeeeee<growup
    ddddddd iiiiiiii rrrrrrrr<growold <grow_hhhh grow
    grow<s yyyyyy vvvvvyoung><growgrow ccccc bbbbbbbold#<growup

    but it returns 14(the number of all the words)

  8. #8
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by quo View Post
    but it returns 14(the number of all the words)
    Right, because:

    Quote Originally Posted by man grep
    grep searches the named input FILEs (or standard input if no files are named, or if a single hyphen-minus (-) is given as file name) for lines containing a match to the given PATTERN. By default, grep prints the matching lines.
    If every line contains the pattern, grep will just output the entire file. Of course, you would notice this if you simply tried it on the command line . Fortunately:

    Quote Originally Posted by man grep
    -o, --only-matching
    Print only the matched (non-empty) parts of a matching line, with each such part on a separate output line.
    Which is why I suggested you read the manual, etc and do a little hands on experimenting. If you spend a little time learning to fish (anyone can do it), you'll save yourself time running around asking "Where are the fish?". Honestly.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  9. #9
    Registered User
    Join Date
    May 2011
    Posts
    116
    Quote Originally Posted by MK27 View Post
    Which is why I suggested you read the manual, etc and do a little hands on experimenting. If you spend a little time learning to fish (anyone can do it), you'll save yourself time running around asking "Where are the fish?". Honestly.
    Yes you're right!Still trying to learn scripting.Thanks!

  10. #10
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    This can be done in "pure bash".
    Code:
    while read line ;do
      cnt=0
      while [ 1 ] ;do
        len=${#line}           # length of string
        line=${line/<grow//}   # substitute
        # if no change in length, break loop
        if [ ${#line} -eq $len ] ;then break ;fi
        let cnt++
      done
      let line_num++
      echo "Line $line_num: $cnt"
      let total=total+cnt
    done
    echo "Total: $total"
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. fgeting through a file to find a certain line
    By cnewbie1 in forum C Programming
    Replies: 2
    Last Post: 05-28-2011, 02:26 PM
  2. Can't Find this! Dotted line with scissors
    By Stan100 in forum Tech Board
    Replies: 4
    Last Post: 10-17-2005, 08:24 PM
  3. Help! I cant find line numbers
    By swgh in forum C++ Programming
    Replies: 2
    Last Post: 07-22-2005, 07:02 AM
  4. cant find where my error is in the line counter
    By Led Zeppelin in forum C Programming
    Replies: 5
    Last Post: 03-23-2002, 04:06 PM
  5. Equation to find endpoints on a line
    By Isometric in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 12-11-2001, 01:45 AM