Finding a word in a file

Hi frndz,

i have a flat file like,

xxx yyy zzz sss aaa bbb yyy xxx rrr sss ttt yyy ddd zzzz cccc..

look, in this file i want to fetch the substring from one yyy to another one and need to print it then from next values between yyy's..

can you please give me some inputs on this..

Thanks in advance,

  • Raja.
 echo "xxx yyy zzz sss aaa bbb yyy xxx rrr sss ttt yyy ddd zzzz cccc" | sed 's/.*yyy\(.*\)yyy.*/\1/'

may not work if there are more "yyy"s .

# awk 'BEGIN{FS="yyy"}{for(i=2;i<NF;i++) print $i}' file
 zzz sss aaa bbb
 xxx rrr sss ttt

check if this is what you are looking for:

perl -nl -e 'while (m/yyy(.*?)yyy/g) { print $1; }' file

yes this is working... however, the thing is.. i want to do some operation on each print $1... that is, need to work with,
zzz sss aaa bbb
then,
xxx rrr sss ttt
ect.,

am i clear frnd?

it depends on what you want to do. You can do it inside awk, or outside in the shell

awk 'BEGIN{FS="yyy"}
{    for(i=2;i<NF;i++) {
           print $i
          #do something here
      }
}' file

until that information of what you want to do is spelt out, otherwise not much i can do

yes, thank you frnd,

i have to find string1 inside each iteratiion and if it found then i need to fetch string2 from the location based on the string1's location....
string one can be present more than once too.

does it make any sense frnd?

frnd, (whatever it stands for)
you've been shown a skeleton of a solution by ghostdog74. It's been left to you to take it and enhance it to satisfy your requirements.

Hi,

Pls try below one, i think it is ok for you.

sed 's/yyy/,/g' filename | awk 'BEGIN{FS=","}
{
for(i=2;i<=NF-1;i++)
print $i
}'

look into the following frnds,

how can i extend the scop of the variable "i"?

cat > tst.txt
"aa bb cc aa dd ee aa bb ff aa gg hh"
^D

v[$i]=`awk 'BEGIN{FS="aa"}
{
for(i=2;i<NF;i++)
{
print $i
}
}' tst.txt`

#need to print values in v through for loop