Need help in sed command [ printing a pattern + its line no or line no alone ]

Hello friends,

              Only very recently i started learning sed command...an i found that sed is faster in finding the patterns than some of my scripts that uses grep to check the patten inside a file using line by line search method which is time consuming. 

The below script "strMatch" is the one of them which i use to search for a pattern. Once the pattern is found it will display the pattern & the line number or the line number alone as shown below.

>Input
strMatch Sample.cpp "PATTERN STRING"
>Output
5: PATTERN STRING

>Input
strMatch Sample.cpp "PATTERN STRING" 2
>Output
5

Many of my other script has started using this script and the dependency to this script has almost became complex such that the new scripts that uses strMatch directly or indirectly for various purposes is slow in execution. I have noticed that the following sed command can be used to tweak strMatch to increase the search speed...

sed '/PATTERN STRING/p' Sample.cpp

But i need to get the line number also like my previous strMatch....Is there any way to print the output like below

Output 1 [Line number & pattern]
5 PATTERN STRING

Output 2 [Line number where pattern was found]
5

Because some of my scripts uses only these line numbers..... Kindly help in making strMatch script fast....the purpose of strMatch is to search for a pattern and return the pattern location or the pattern along with the location.....Below is actual strMatch script...

there is a special case for handling "include" keyword.....I am not able to find any alternative for "exec" command to pass the file content to a variable as such (with correct alignment).....and somehow whenever i search for include keyword...it will exec on all the files in the folder.......I believe with sed command i wont be requiring "exec",

Thanks in advance...........:):confused:
-------------------------------------------------------------------------

filter(){
pattern=$1
line=$2
for x in $line; do
flag=`echo $x | grep -c $pattern`

            if [ $flag -eq 1 ]; then
                    return $flag
            else
                    return 0
            fi
    done

}

#filterKeywords(){

#}

pattern=$2
exec<$1
#fileCont=`rdfil $1`
linCnt=1

exitCnt=`lincnt $1`
cnt=0
count=0
mode=$3
#rdfil $1
while read line; do

    if [ "$2" == "include" ]; then
            quitCon=\`echo $line | grep -c "//-------------------------"\`
            if [ $quitCon -eq 1 ]; then
                    count=$\(\($count \+ 1\)\)
                    if [ $count -eq 2 ]; then
                            break
                    fi
            fi
    fi

    status=\`echo $line | grep -c "$2"\`
    if [ $status -eq 1 ]; then
            fflag=\`filter $pattern $line\`
            wcnt=\`echo $fflag| wc -l\`
             if [ $wcnt == 1 ]; then
                    if [ $exitCnt -ge $cnt ]; then
                            if [ "$mode" != "2" ]; then
                                    case $mode in
                                            "NUM"\) \# LINE NUMBER
                                                    echo $linCnt
                                                    ;;

                                            "FON"\) \# FIRST OCCURENCE LINE NUMBER
                                                    echo $linCnt
                                                    exit
                                                    ;;

                                            "FOLN"\) \# FIRST OCCURENCE LINE
                                                     echo $line
                                                     exit
                                                     ;;

                                             "LN"\) \# ONLY LINES
                                                     echo $line
                                                     ;;

                                             *\) \# DEFAULT
                                                     echo $linCnt: $line
                                                     ;;
                                     esac
                                     \#echo "Hi"
                             else
                                     echo $linCnt
                             fi
                     \#filterKeywords $line
                     fi
              fi
     fi
     linCnt=$\(\($linCnt \+ 1\)\)

     cnt=$\(\($cnt \+ 1\)\)

#echo "---------------------------------------$cnt $exitCnt"
done

#echo "Bye!"

Have you tried grep? This gives the number of the line and the line delimited by a colon:

grep -n <pattern> <file>

To get the number you can do something like:

grep -n <pattern> <file>| cut -d: -f1

Check the man page of grep and cut for the used options.

Regards

   I need only first appearance of the matched pattern ?

help me......

Have you tried the -m option of grep?

Hi,

If the file is as given below, please let me know if the command is right to get the like number containing the string - Earth with the "=" symbol.

Filename: example.txt
123|Earth|2009

sed '/Earth/
{

}' example.txt