Grep/awk/sed help

got a file as y.txt

1 abc,def,ghj
2 defj,abc.kdm,ijk
3 lmn,cbk,mno
4 tmp,tmop,abc,pkl
5 pri,chk,cbk,lmo
6 def,cbk.pro,abc.kdm

i want to search in the above file the key word like abc

looking for two outcomes by passing the parameter value as abc into function and the two outocmes are :

1) how many lines the abc is available. It should search only for abc and not abc.kdm.

count should be 2.

2) also the line numbers of the respective lines where abc is available like below.

1 abc,def,ghj
4 tmp,tmop,abc,pkl

ck.sh abc

it should give two outcomes

can someone give me the solution for this ?

---------- Post updated at 10:10 PM ---------- Previous update was at 10:06 PM ----------

all doing in bash

Is this a homework item?

no not home work item...

i replaced all the content with simple ones..but i need the logic and the code as i need to implement the same.

Show us what have you tried so far. Share your thoughts and efforts.

Is the line number in y.txt as shown:

1 abc,def,ghj
2 defj,abc.kdm,ijk
3 lmn,cbk,mno
4 tmp,tmop,abc,pkl
5 pri,chk,cbk,lmo
6 def,cbk.pro,abc.kdm

or is y.txt:

abc,def,ghj
defj,abc.kdm,ijk
lmn,cbk,mno
tmp,tmop,abc,pkl
pri,chk,cbk,lmo
def,cbk.pro,abc.kdm

and you added the line numbers for discussion purposes?
I.e., if the input file were:

1 abc,def,ghj
4 tmp,tmop,abc,pkl
5 pri,chk,cbk,lmo

Would the output be:

1 abc,def,ghj
4 tmp,tmop,abc,pkl

or:

1 4 tmp,tmop,abc,pkl[

(since "1 abc" does not match "abc")?

If the line numbers are present in your input file, are the line numbers part of the 1st field, or are they a separate field delimited by a space instead of by a comma?

Will any of your comma separated fields (possibly other than the 1st field) ever contain spaces or tabs?

line numbers are different there....not in order

so what ever the numbers are there it should display.

i did some work but as a normal commands its working.

like

export param=abc

awk -F"," -v searchStr=$param '{for(idx=1;idx<=NF; ++idx) {if($idx == searchStr) {print $0;}}}' y.txt

output coming as :

1 abc,def,ghj
4 tmp,tmop,abc,pkl

which is expected and thats what i want.

but

when i execute the same in sh file where i pass the parameter to search as abc, which is getting consider as $1,

the above command print $0 in awk is getting messed up.

not sure if thre is any other way as i want to execute in some function by passing the parameter.

---------- Post updated at 11:37 PM ---------- Previous update was at 11:33 PM ----------

its like

./ck.sh abc

content as :
param =$1

awk -F"," -v searchStr=$param '{for(idx=1;idx<=NF; ++idx) {if($idx == searchStr) {print $0;}}}' y.txt

the output is not working as print $0 is messed up there. with $1, $0 i guess.

---------- Post updated at 11:38 PM ---------- Previous update was at 11:37 PM ----------

there is space between the first column i.e. number and rest are comma separated strings in that line.

First: Please use CODE tags.

Second, I'm VERY surprised that the awk script you're showing us printed:

1 abc,def,ghj
4 tmp,tmop,abc,pkl

With -F"," , the 1st line should not have been printed because 1 abc is not equal to abc .

So, you're close, but:

  1. shell assignments can't have spaces, so change:
    text param =$1
    to:
    text param="$1"
    (Note: Always protect yourself against malformed user input.)
  2. with FS set to comma, field 1 in line 1 is "1 abc"; not "abc", so change:
    text -F"," -v searchStr=$param
    to:
    text -F"[ ,]" -v searchStr="$param"
    and
  3. you don't print the match count, so add a counter that increments when you find a match and add an END clause to your awk script (i.g., END {print counter} ).

Alternatively, here is a way to do it entirely in the shell if your shell is bash or ksh:

lc=0
while IFS="" read -r line
do      IFS=" ," f=( $line )
        for ((i=${#f[@]} - 1; i > 0; i--))
        do      if [ x"$1" = x"${f[$i]}" ]
                then    printf "%s\n" "$line"
                        ((lc++))
                        break
                fi
        done
done < y.txt
printf "%s found on %d lines.\n" "$1" $lc

Note that this script will only print a line once if $1 appears more than once on a line; your awk script would print it multiple times in this case. With your sample input, the output produced when $1 is "abc" is:

1 abc,def,ghj
4 tmp,tmop,abc,pkl
abc found on 2 lines.

when given the input you specified in the 1st message in this thread.

Hope this helps.