Need to print the expansion of the found string (the expansion is beween two delimiters '-' , '||'

Hi ,
could anyone help me out with this problem.
sample.txt has this content :
u001- this is used for project1 ||
u002- this is used for p2|| not to be printed
u003- this is used
for project3 ||
u004- this is
used for p4 ||
u005- this is used for project5 ||
u006- this is used for p6 ||

I need a shell script , for which the input will be 'u001' or 'u00X'...
and i need to print the words between - and || of that particular string.

I am able to print the word if all the words are in the same line..

a=`grep -iw $1 sample.txt | awk -F"-" '{print $2}' | awk -F"||" '{print $1}'`
echo $a

this holds good for u001,u002,u005,u006..

can anyone help me with the code ...if my input is u003,u004

:slight_smile:

Is this homework? This is a similar question to:

code

nawk '
/^u00[34]/ {c=2;printf "%s ",$0} ; /^u00[^34]/
!--c
' sample.txt

BR

Try something like this :

awk -v va="u003" '$0 ~ va,/\|\|/ {print }' file.txt 

To get the text and then your approach to get the text b/w "-" and "||".

this is printing everythin in the sample.txt.. I want it like if my input is u003 , the out put should be" this is used for project3 "...

pls help it.

---------- Post updated at 05:06 AM ---------- Previous update was at 05:05 AM ----------

awk: syntax error near line 1
awk: bailing out near line 1

this is the error i am getting.

Balaji PK,

Is this home work?

this is what i am getting :

TEST/TES>awk -v va="u003" '$0 ~ va,/\|\|/ {print }' file.txt
u003- this is used
for project3 ||

[KRYPTON]/appltgb/test/repc/balaji > awk -v va="u003" '$0 ~ va,/\|\|/ {print }' s.txt
awk: syntax error near line 1
awk: bailing out near line 1

:frowning:

use "gawk" or "nawk" if available.

Since you don't answer my question I assume this is home work.

Thread closed.