error while replacing a string by new line character in sed

hi,

when i am doing the following things getting error
Can anyone please suggest

i have a file where there is a line like the following
branch=dev sdf dev jin kilii fin kale boyle dev james dev

i want to search the existance of dev in the above line.

cat "$file" | sed -n '/'$branch'p' | sed 's/'$name'/&\n/g' | grep $name |wc -l

where
$file=filename
$branch=line containing branch
$name=dev

while i am doing the above result is 1,but it should be 4

Please suggest

Why not use a single sed statement ?

sed -e "s/\(${branch}.*\)${name}/\1\n/g" ${file}

The follwing is the correct sed (syntactically)

cat "$file" | sed -n ''$branch''p'' | sed 's/'$name'/&\n/g' | grep $name |wc -l

don't know whether it will gives you correct OP or not

What exactly you need ?..i mean your requirement.

You posted

where
$file=filename
$branch=line containing branch
$name=dev

but if you pass any numeric value for $brach say 1 or 2. The sed will print tht particular line and again some other searches and replaces. As always ther will be only one line , wc -l will result in 1.

How ever if u want to search for the existence of a word called "dev" in a specific line the following will do the job :

cat "$file" | sed -n ''$branch''p'' |deroff -w | sort | uniq -c  |grep $name | awk '{ print $1} ' 

Hi Panyam,

Thanks for ur reply.
Can u please give some idea about deroff command.
i have seen its man page but cannt understand .

Please help.

Hi Milan,

In this context of use "deroff -w" , the output is a word list, one ``word'' per line, with all other characters deleted.

I too not that much aware of it's use. From google i found that :

"Deroff reads each file in sequence and removes all nroff and troff(1) requests and non-text arguments, backslash constructions, and constructs of preprocessors such as eqn(1), pic(1), and tbl(1). Remaining text is written on the standard output"

If you are interested u can go through the link to find more about nroff and troff:

Ch 8 -- Basic Formatting with troff/nroff

Hi Panyam,

Actally the line is of the form as given below
branch=dev:sdf:dev:jin:kilii:fin:kale:boyle:dev:james:dev

i have set IFS=:
then execute

cat "$file" | sed -n ''$branch''p'' |deroff -w | sort | uniq -c |grep $name | awk '{ print $1} '

but it is not giving no fo dev present.
Plese reply

Dear Milan ,

It's working Fine for me.

TEST>cat file
branch=dev:sdf:dev:jin:kilii:fin:kale:boyle:dev:james:dev

TEST>IFS=:

TEST>cat "$file" | sed -n ''$branch''p'' | deroff -w
branch
dev
sdf
dev
jin
kilii
fin
kale
boyle
dev
james
dev
TEST>cat "$file" | sed -n ''$branch''p'' | deroff -w | sort |uniq -c |grep $name | awk '{print $1>
4

Chk it once again the branch and name values your passing.

my bash version is
GNU bash, version 2.05.0(1)-release (sparc-sun-solaris2.9)

while i am executing this it is giving Segmentation Fault as my the line where i am serching the pattern dev is long enough

Try this:

awk -F":|=" -v b="$branch" -v d="$name" '
$0 ~ b {
  for(i=1;i<=NF;i++){
    if($i==d){s++}
  }
  print s
}' "$file"