capturing info between words.

Dear Friends,
I am facing 2 problems while writing a script
1) I have a flat file and I want to captur specific information from it.

Example
I have this string in the file

PACK: P42 77 UNPACK: MHTT DMK

I want to capture whatever is between word PACK: and word UNPACK: including spaces.

Expected o/p
P42 77

2) I want to capture next up to 20 characters appearing after pattern UNPACK: including spaces.

Example:
I have
PACK: P42 77 UNPACK: MHTT DMK

Expected o/p
MHTT DMK

Kindly guide me in captuing it.

# eval $(echo PACK: P42 77 UNPACK: MHTT DMK | awk '{sub("PACK: ","VAR1=\"");sub(" UNPACK: ","\";VAR2=\"");$0=$0"\""}1')
# echo $VAR1
P42 77
# echo $VAR2
MHTT DMK

GNU sed:

$> sed 's/^PACK:[ \t]*\(.*\)UNPACK.*/\1/g' infile
P42 77
$> sed 's/.*UNPACK:[ \t]*\(.\{,20\}\).*/\1/g' infile
MHTT DMK

You have 104 posts already and still don't use code tags. Please edit your post accordingly as commented by radoulov. If have problems using code tags, let us know, ty.

Please help me in finding code tags document so that i can start following it.

Check Forum Rule (B): The UNIX and Linux Forums - Forum Rules

Some misunderstanding.

I have

READY: BLPS3Q   PACK: DBL25464         UNPACK: UY3546P4

I want

DBL25464         in op.txt
UY354758         in op1.txt

here everything is variable in length except words READY:, PACK: and UNPACK:
We want to send anything appearing between word PACK: and UNPACK: to op.txt
Similarly next 20 characters of word UNPACK:

echo READY: BLPS3Q   PACK: DBL25464         UNPACK: UY3546P4 | awk -F:\  '{gsub("[PACK|UNPACK]",X);print $3 > "op.txt";print $NF > "op1.txt"}'
perl -lne 'open G, ">op1.txt" if ($.== 1); /PACK:(.+?)UNPACK:(.{1,20})/; print $1; print G $2' t > op.txt

Here's a Perl solution. I have not found an elegant way to make perl divvy up STDOUT to multiple files on the command line in the way awk does. :frowning:

my $str="PACK: P42 77 UNPACK: MHTT DMK asdf asdfA   ad";
print $1,"--",$2, "\n" if $str=~/PACK:(.*?)UNPACK:(.{0,20}).*/;