I'm having trouble getting awk to read a variable with spaces in it.
The awk command isn't recognizing the entire string. It points to the first letter of the first string in the variable. How can I make it so awk recognizes the entire variable?
Also, I'm not entirely sure if a single quote screws up the awk command either.
Thanks
FYI: The caret symbol is supposed to be pointing to the capital B.
Well...
1. Shell and awk are different utilities and they have different variables. Shell variables and awk variables are not the same things.
2. You can transfer values of shell variables to awk variables or awk expressions.
3. What you try to do - transfer values of shell variable to awk expression. (You can see what happens with 'set -x'). Shell variables expanded before awk get its program and you have this command:
awk should get its program as one piece but it gets four:
This all is a consequence of the shell syntax quoting rules.
4. Ok, what can you do. The universal but unsafe and often cumbersome method is to put your shell variables in double quotes:
Shell expand the variable but stop here. You get:
And once shell see several different adjacent quoted strings it glues them in one so the final result is something like this:
5. This method works for any command utilities, like sed, perl, sql clients, etc. But awk has much better (less cumbersome and much safe) method (or two to be exact). You can transfer value of shell variables to awk variables in command line arguments. The first one is with -v option:
The second one: use the assignment among file arguments:
vendorName="Bob's Steakhouse"
awk -F":" '$2 ~ vendor {print $1}' vendor="$vendorName" Purchases.dat
Hope it may help. Sorry for my English (this post really is my daily English exercise ).
I wrote a very simple script to understand how to call user-defined functions from within awk after reading this post.
function my_func_local {
echo "In func $1"
}
export -f my_func_local
echo $1 | awk -F"/" '{for (k=1;k<=NF;k++) {
if ($k == "a" ) {
system("my_local_func $k")
}
else{... (19 Replies)
I have a data file like this:
49960 1157 32390 1227 1268
31 8 21
12 115 18493 67
250 2 2 237704 369658
52 21 312 38 27746 3174
19 160 9 555 6337 6071
43 33
I want to separate the field to three groups, $1 and $2 are the first group, $3 and $4 are the second group, $5 and $6... (1 Reply)
Using ksh to call a function which has awk script embedded.
It parses a long two element list file, filled with text numbers (I want column 2, beginning no sooner than line 45, that's the only known thing) . It's unknown where to start or end the data collection, dynamic variables will be used. ... (1 Reply)
Hi all, I am new to the forum and Shell Script programming.
The problem is:
I need to do a script to search all system processes and show me hierarchical way the number of bytes occupied by each of the regions of the memory map of each process.
Today I got to show me the number of regions in... (3 Replies)
Hi,
I'm pretty new to AWK and was wondering if someone could let me know how to execute varibles within an AWK statement. An example is below:
NO=6
end=25
awk = 'NR == $NO, NR == $end' file1 > file2
I'm currently attempting to use this within a script but awk seems to read $NO and... (2 Replies)
hi i am using the following awk code for some calculations:
awk '/,1,/' "$LINE" >> "$count".dat
awk '/,1,/ { i++ } END { print i }' "$count".dat
awk '{sum+=$3} END {print sum}' 1.spd > test
awk '{average=sum/i} END {print average}' 1.spd >> test
can i use a variable created in one... (12 Replies)
Hi,
I have some files in metrica (assume a pre-defined format) which i need to process and have some values in a .csv file. The script that does this, part of which is:
procMetricaOMData()
{
host=$1
fileIN=$2
fileOUT=$3
$CATCMD $fileIN | \
awk -f... (4 Replies)
I want to print old and new values of some field in Awk script. This is wat I done!
Sample file is as follows
But the output I god is bit different
What must be the problem here?
I am using bash. (2 Replies)
Hi
I have a file with a line like this, wich is read by an awk script :
Logical Name | Server Type | Server1 | DB1 | User1 | $PASSWORD | Serv2 |DB2 | User2 | $PASSWORD
Awk reads it fine, but...it doesnt interpret the variable $PASSWORD as I wish. How do I tell awk to substitute $PASSWORD... (3 Replies)