finding characters with new line

I have a file sample.txt with the below contents:

Aaa - providioning add ||
dev - reeec
dev kapl ||
ball - HERO ||
bal - provisioning  pro ||

for given name i need the output to be the contents between - and || (excluding both)
for eg : input - Aaa
output - providioning add

I came up with the following

grep -iw $1  $2 | awk -F"-" '{print $2}' | awk -F"||" '{print $1}'

where $1 - name to search
and $2 - name of the file (sample.txt in this case)

Problem arises for the name dev .The above code only displays reec while i need reeec dev kapl (for more than one line we need the new line to be removed ) . In other words

grep -iw dev sample.txt | awk -F"-" '{print $2}' | awk -F"||" '{print $1}'  displays  "reeec "

But I need "reeec dev kapl "

Thanks in advance folks

awk 'BEGIN { RS = "||", FS = "-" } /$1/ { print $2 }'

Where $1 is the argument you pass to the script, or the name you want to look for.

EDITED: Now I think that "$1" won't work at all... Maybe

grep -iw $1 | awk 'BEGIN { RS = "||", FS = "-" } { print $2 }'

will do.

Hi Casey,
Thanks for the response . But unluckily this is what i keep getting.

awk: syntax error near line 1
awk: illegal statement near line 1

Is there something i am missing

Thanks in advance

awk -v str="$1" '
   BEGIN { RS = "||"; FS = "-" }
   $0 ~ str { print $2 }
'

Hi Johnnson,
Thanks for the reply.

Again i get a similiar error .
awk: syntax error near line 1
awk: bailing out near line 1

I am in ksh and should i change parameter or something. This is driving me nuts.

Thanks in advance

Use gawk /nawk instead and try.

gawk not found :frowning:

any other plan

Are you on Solaris?

nawk ' ... '

Hi Johnnson ,
That worked with a small modification .
Can you explain what happens in this command

---------- Post updated at 04:40 AM ---------- Previous update was at 03:30 AM ----------

Hi Johnson,
This doesnt work. This is my command
nawk -v str="dev" ' { RS = "||"; FS = "-" } $0 ~ str { print $2}' sample.txt gives me
reeeec
and removes the new line. Any help is much appreciated

any help for this wil also help me..

Use nawk or /usr/xpg4/bin/awk on Solaris.

awk '
!/\|/{getline s;$0 = $0 FS s}
{ $1=$NF""
  gsub("\|\|","")
  sub("^ ","")
  sub("- ","")
  print
}' file