Problem with variable use

Hi,

I need a little help with variable use:

I have the a file with follow format:

Type:            Test
Profile:   010


84240 27 15
84900 11 09
84993 55 44
84762 12 12

I need to look the value in the line containing "Profile" and put in front of the lines containing numbers beginning with 84.

Desired output:

010 84240 27 15
010 84900 11 09
010 84993 55 44
010 84762 12 12

I�ve been trying with:

awk '/Profile:/ {var=$2} /^84/ {print var " " $0 }}' inputfile 

but I get only lines containing the value of "var"

010
010
010
010

Maybe somebody can help me saying me how to fix this issue.

Thanks in advance

You have an extra braces } at the end...see above in bold .
Remove it and it will work.

:D:D

Thanks:)

nawk ' $1 == "Profile:" {key=$2;next;}$1 ~ /^84/ {print key" "$0}' yourfile

Hi summer_cherry,

Thanks, it works perfect. A little more help.

But may somebody explain how this works, I undertand a little bit some parts, but not the complete joined sentence.

 
nawk ' $1 == "Profile:" {key=$2;next;}$1 ~ /^84/ {print key" "$0}' yourfile

When use nawk or awk?

$1 == "Profile:" {key=$2;next;}$1 --> I undertand that is something like, "if $1 = Profile:, assign key=value in $2"
 
;next;} --> This part?
$1 ~ /^84/ --> This part?

Thanks in advance

"next" in awk means "stop processing the current line, and move onto the next line, starting at the top".

Don't confuse "next" with "getline".

"~" (tilde) is for a regular expression comparison, so

$1 ~ /^84/{do stuff}

means that if the first field starts with 84, then "do stuff"

The gnu awk user's manual (not the man page, but the on-line guide) is a great resource for these types of questions. That's how I learned.

Thanks a lot Gee-Money, I understand much better this. I�ll follow your suggestion.

:b: