Problem with variable use


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Problem with variable use
# 1  
Old 06-23-2009
Problem with variable use

Hi,

I need a little help with variable use:

I have the a file with follow format:

Code:
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:
Code:
010 84240 27 15
010 84900 11 09
010 84993 55 44
010 84762 12 12

I´ve been trying with:

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

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

Code:
010
010
010
010

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

Thanks in advance
# 2  
Old 06-23-2009
Quote:
Originally Posted by cgkmal
Hi,

I need a little help with variable use:

I have the a file with follow format:

Code:
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:
Code:
010 84240 27 15
010 84900 11 09
010 84993 55 44
010 84762 12 12

I´ve been trying with:

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

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

Code:
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.
# 3  
Old 06-23-2009
SmilieSmilie

ThanksSmilie
# 4  
Old 06-23-2009
Code:
nawk ' $1 == "Profile:" {key=$2;next;}$1 ~ /^84/ {print key" "$0}' yourfile

# 5  
Old 06-23-2009
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.

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

When use nawk or awk?

Code:
$1 == "Profile:" {key=$2;next;}$1 --> I undertand that is something like, "if $1 = Profile:, assign key=value in $2"

Code:
 
;next;} --> This part?

Code:
$1 ~ /^84/ --> This part?

Thanks in advance
# 6  
Old 06-24-2009
"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
Code:
$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.
# 7  
Old 06-25-2009
Thanks a lot Gee-Money, I understand much better this. I´ll follow your suggestion.

Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

problem in assigning value to variable have value fo other variable

my script is some thing like this i11="{1,2,3,4,5,6,7,8,9,10,11,}" echo "enter value" read value ..............suppose i11 x="$value" echo "$($value)" .............the echo should be {1,2,3,4,5,6,7,8,9,10,11,} but its showing "i11" only. plz help me out to get desired... (10 Replies)
Discussion started by: sagar_1986
10 Replies

2. Shell Programming and Scripting

Problem with a variable withing a variable

hello there, basically im screwed with a variable that should take the last modification date of a file. my code is fileCreationTime=$(( `ls -l $fileName | tr -s " " | cut -d " " -f6` )) my problem arise coz when the code is executed and stored in a file the return value is 1993 and not... (4 Replies)
Discussion started by: thurft
4 Replies

3. Shell Programming and Scripting

problem in variable comparision

Hi All, I need to compare the string variable with input value which have more than one word which also should be case insensitive. #!/bin/sh echo "please choose your choice: " read choice if ` ] then echo "correct" else echo "wrong" fi but i am getting the output for first word... (3 Replies)
Discussion started by: kalai
3 Replies

4. Shell Programming and Scripting

awk variable problem

Hi How are you people .. I am facing another problem regarding awk below is my code -bash-3.00$ export a=10 -bash-3.00$ echo $a 10 -bash-3.00$ echo "" | nawk -v var=$a ' { if(var=10) { print "abc" } else { print "def"} } ' abc -bash-3.00$ echo "" | nawk -v var=$a ' { if(var==10)... (2 Replies)
Discussion started by: aishsimplesweet
2 Replies

5. Shell Programming and Scripting

Problem in storing value to variable

#bash curVer=`cat /var/sadm/clsversion | cut -f 2 -d "_"` echo "CurVer:$curVer" ls |grep -v tar| grep -v sh| grep -v log|cut -f 1 -d "_" | sort -u >tmp1 for line in $(cat tmp1) do ver=`echo $line_$curVer` ls $line* |sort >tmp2 grep -n ${ver} tmp2 >/dev/null ... (2 Replies)
Discussion started by: rajamohan
2 Replies

6. Shell Programming and Scripting

Problem with variable value

Can anyone help me?...I don't know why the second 'echo $contador' always shows 0 (zero): 1 #!/bin/bash 2 contador=0 3 while read linea 4 do 5 echo 6 echo "$linea" | while IFS="" read -n 1 caracter 7 do 8 contador=$((${contador}+1)) 9 echo $contador... (5 Replies)
Discussion started by: albertogarcia
5 Replies

7. Shell Programming and Scripting

Problem wit the $? variable

Hi Friends, I have a problem when i run this piece of script, and do echo $? it always gives me value 0, even if the string "CCRCWebServerINSTALLDIR" is not present in that file. Why is that so, can you give me suggestion to the changes i need to make? $find... (3 Replies)
Discussion started by: asirohi
3 Replies

8. Shell Programming and Scripting

Help me in this variable problem

suppose i declare a=b b=30 echo $(echo $a) i need to get the value as 30 i dont know if im right but it prints only 'b' as output. How can i get the value referred by 'b' through the value of 'a' (4 Replies)
Discussion started by: SankarV
4 Replies

9. UNIX for Dummies Questions & Answers

grep for a variable problem

I am trying to use two files and walk through the first one to see if a value from each record resides in the other file. I am reading the file record by record and awking out the first field into a varaiable. That is working fine. When I try to write my grep command it gives me an error. ... (7 Replies)
Discussion started by: MizzGail
7 Replies

10. Shell Programming and Scripting

Problem with variable type

Dear All, I am trying to write an script to calculate geometric centre of selected residues of a protein structure. Say I have a PDB file (a text containing the x, y, and z coordinates of atoms of a protein). I want to extract the X coordinates of the atoms belonging to the interested residues... (2 Replies)
Discussion started by: siavoush
2 Replies
Login or Register to Ask a Question