problem with printing out variable in awk

 
Thread Tools Search this Thread
Homework and Emergencies Homework & Coursework Questions problem with printing out variable in awk
# 1  
Old 04-13-2012
problem with printing out variable in awk

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted!

1. The problem statement, all variables and given/known data:

couldn't print out stored variable in awk

2. Relevant commands, code, scripts, algorithms:

i have in a foreach loop:

set num=`blah blah`
if ( $num != "0" ) then
echo $x | awk 'BEGIN { FS="." } { printf "%s %s %s\n", $1, $0, num }'
else return
endif
end

im wondering why awk just couldnt print 'num' :/

3. The attempts at a solution (include all code and scripts):

the above

4. Complete Name of School (University), City (State), Country, Name of Professor, and Course Number (Link to Course):

Uni of Southampton, so on
Note: Without school/professor/course information, you will be banned if you post here! You must complete the entire template (not just parts of it).
# 2  
Old 04-13-2012
You don't get shell variables outside of the shell, and awk is not a shell extension. awk is its own, independent, self-contained language.

If you want to insert a variable into awk, you can do so with -v:

Code:
awk -v var="$var" ...

This User Gave Thanks to Corona688 For This Post:
# 3  
Old 04-13-2012
Quote:
Originally Posted by Corona688
You don't get shell variables outside of the shell, and awk is not a shell extension. awk is its own, independent, self-contained language.

If you want to insert a variable into awk, you can do so with -v:

Code:
awk -v var="$var" ...

so is that when i assign a variable (say 'num') some value outside awk, and then assign num to a variable in awk (say 'var' ) in this way:

Code:
awk -v 'BEGIN { FS="." } var="$num" { printf "%s %s %s\n", $1, $0, var }'

like this?
# 4  
Old 04-13-2012
No, like I showed you:

Code:
awk -v var="$var" ...

Where the ... is everything else you were putting into awk.

Code:
awk -v num="$num" 'BEGIN { FS="." } { printf "%s %s %s\n", $1, $0, num }'

This User Gave Thanks to Corona688 For This Post:
# 5  
Old 04-16-2012
Quote:
Originally Posted by Corona688
No, like I showed you:

Code:
awk -v var="$var" ...

Where the ... is everything else you were putting into awk.

Code:
awk -v num="$num" 'BEGIN { FS="." } { printf "%s %s %s\n", $1, $0, num }'

thanks for being helpful, anyway just found out that num=$num should work Smilie
# 6  
Old 04-16-2012
Quote:
Originally Posted by ymc1g11
thanks for being helpful, anyway just found out that num=$num should work Smilie
awk -v num="$num" is the preferred way, awk -v num=$num will break if $num contains spaces or certain special characters (this is not a shell assignment) ...

Last edited by Scrutinizer; 04-16-2012 at 05:42 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk code to inspect variable before printing

i have a unique scenario id like help with. im currently running this command and it does what i want: printf '%s\n' "${RawContent}" | awk '/## Beginning Stages ##/,/## Ending Stages ##/' | awk '!/^#.*\!|^#\!|DefaultError/' Can this be shortened? I'm looking for something portable as... (8 Replies)
Discussion started by: SkySmart
8 Replies

2. Shell Programming and Scripting

Problem printing with awk

Hi, so i have this command: ps axo cmd=,user=,%cpu=,priority= --sort=-%cpu | awk '{print $2}' And i want to print the second column, that is user. The thing is that i dont know why this is my output: root Eskizoi+ Eskizoi+ axo {print I dont know why axo and {print are there, the... (20 Replies)
Discussion started by: Eskizoide
20 Replies

3. Shell Programming and Scripting

Issue when printing filename through cygwin using a variable with awk

Hi, If i were to do this an print out the file, it will show as it is in the command $ awk '/Privilege Use/ {P=0} /Object Access/ {P=1} P' AdvancedAudit.txt Object Access File System No Auditing Registry No Auditing Kernel... (1 Reply)
Discussion started by: alvinoo
1 Replies

4. Shell Programming and Scripting

[awk] printing value of a variable assignment from a file

Heyas Me try to print only the value of a (specific) variable assignment from a file. What i get (1): :) tui $ bin/tui-conf-get ~/.tui_rc TUI_THEME dot-blue "" "$TUI_DIR_INSTALL_ROOT/usr" "$TUI_DIR_INSTALL_ROOT/etc/tui" "$TUI_PREFIX/share/doc/tui" "$TUI_PREFIX/share/tui"... (2 Replies)
Discussion started by: sea
2 Replies

5. Shell Programming and Scripting

awk variable substitution problem

Hi I am having a file like this ############################## j=1 while ] do temp_5=MODULE$j awk ' $1 ~ /'${!temp_5}'/ { do something }1' file1 > file2 ((j = j +1 )) done ################### Setting the variables like this (8 Replies)
Discussion started by: kshitij
8 Replies

6. Shell Programming and Scripting

Problem using variable inside awk

HI, This is the code I am using: awk -v aaa="connect" 'BEGIN {IGNORECASE} /aaa/,/!/ {print NR}' bb This does not throw any error but it does not work. Pls help Thanks. (4 Replies)
Discussion started by: sudvishw
4 Replies

7. Shell Programming and Scripting

Printing a variable column using awk

Hi everyone, Ok here's the scenario. I have a control file like this. component1,file1,file2,file3,file4,file5 component2,file1,file2,file3,file4,file5I want to do a while loop here to read all files for each component. file_count=2 while ] do file_name=`cat list.txt | grep... (2 Replies)
Discussion started by: The Gamemaster
2 Replies

8. Shell Programming and Scripting

Awk Issues - Not printing the 10th Variable.

All, I am attempting to print the tenth ($COPY2) varaibales into one file. But i am finding that all variables are being outputted except for $10. Can someone help!!!! Code Below ---------- echo $SERVER $IMAGE $IMAGEDAY $IMAGEMONTH $IMAGEYEAR $COPY1 $EXPIREDAY $EXPIREMONTH... (1 Reply)
Discussion started by: Junes
1 Replies

9. 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

10. UNIX for Dummies Questions & Answers

Problem with formatted printing in AWK

Hi, I want to print 130 fileds using formatted printing in AWK. It looks like awk '{printf ("%7.2f%7.2f...%7.2\n",$1,$2,...,$130)}' inflie>oufile But it gives me an error: Word too long! Can you please help me with this? Is there another way to do this? (1 Reply)
Discussion started by: PHL
1 Replies
Login or Register to Ask a Question