multiple commands inside a awk - awk

sry opening a new thread but awk is driving me nuts! >< im trying to run a few command assignments inside a single awk but i cant get it to work please help if this is ez for u :P i can't get the syntax to work
edit: im using /bin/bash
for f in `seq $nlinpE $loopE`;
do
awk -F ","'
BEGIN {}
'$f' { dataI2[$f]=$2;
dataI3[$f]=$3;
dataI4[$f]=$4;
noD1[$f]=$dataI1[$f];
noD2[$f]=$dataI2[$f];
noD3[$f]=$dataI3[$f];
noD1i[$f]=`echo "$nlinpN1 + $dataI1"|bc -l`;
noD2i[$f]=`echo "$nlinpN1 + $dataI2"|bc -l`;
noD3i[$f]=`echo "$nlinpN1 + $dataI3"|bc -l`;
}
'${noD1i[$f]}' {
dataIi2[$f]=$2;
dataIi3[$f]=$3;
dataIi4[$f]=$4;
}
'${noD2i[$f]}' {
dataIii2[$f]=$2;
dataIii3[$f]=$3;
dataIii4[$f]=$4;
}
'${noD2i[$f]}' {
dataIiii2[$f]=$2;
dataIiii3[$f]=$3;
dataIiii4[$f]=$4;
}
END{}
' <aoa_5.inp;
done
input is like:
17, 3.22854114, 0.562598288, 0.384291202
18, 2.96085286, 0.085116826, 0.285071939
19, 3.40070796, 2.27838659, 0.302027524
20, 3.20035744, 0.333615214, 0.262585849
21, 2.85644341, 0.258691043, 0.369726121
22, 3.73537922, 1.3267405, 0.295917094
23, 3.69372559, 1.32601321, 0.306054831
24, 3.28857207, 0.63199228, 0.378117412
25, 3.27523994, 0.695856452, 0.377585977
imjust assigning variables atm, getting the number w/o commas
i get this syntax type of error:
awk: 9: unexpected character '`'
awk: 10: unexpected character '`'
awk: 11: unexpected character '`'
(standard_in) 2: syntax error
(standard_in) 2: syntax error
awk: line 1: syntax error at or near {
^C
thx

Maybe this will help you clean up your syntax a little so we can understand what you're trying to do.
BEGIN and END blocks are optional. Ignoring patterns for the moment, an awk program might look like this.
BEGIN {
# Things to be done before you start processing rows.
}
{
# Things to be done for each row.
}
END {
# Things to be done after processing the last row.
}
If you don't happen to need BEGIN or END blocks, it might look more like this.
{
# Things to be done for each row.
}
This awk program assigns the value of $2, $3, and $4 to the variable dataI, and prints it once for each row.
{
dataI = sprintf("%s %s %s", $2, $3, $4);
print dataI;
}
That assignment has no effect on the values of $2, $3, and $4.

Related

How to write awk function

I have a quick file file.awk with
function attr(attrname,str, a) {
if (!str) str=$0
match(str,"#" attrname "=([^,/]*)",a)
return a[1]
}
I am getting an error
awk: file.awk: line 139: syntax error at or near ,
where line 139 is the line with match()
Any idea whats wrong with the syntax?
You're trying to use the non-POSIX 3rd arg to match() but not using GNU awk which supports it. See https://www.gnu.org/software/gawk/manual/gawk.html#String-Functions.
As always, Ed Morton has it correct. The only reason I'm posting is that I can share with MacOS users that the awk man page includes the answer.
man awk yields:
match(s, r)
the position in s where the regular expression r occurs, or 0 if it does not. The variables RSTART and RLENGTH are set to the position and
length of the matched string.
So it is very clear that your call has an extra parameter.
I tried runnning awk with your function on MacOS and got this error:
awk: syntax error at source line 1 in function attr source file
context is
match(str,"#" attrname >>> "=([^,/]*)", <<<
The "<<<" is indicating awk doesn't like that comma. This is similar to your error message:
syntax error at or near ,
You absolutely don't need the match() function's capture array to get the attribute value you're seeking :
echo 'april#token=sha256hmackey/0.ts' \
\
| mawk 'function attr(___,_,__,____) { ____="\32\21"
if(_=="") {
_=$(_<_) }
return \
sub("[#]"(___)"=[^,\\/]*",____"&"____,_) \
\
? substr(_=__[split(_,__,____)-\
(_~_)],index(_,"=")+(_~"")) : ""
} {
print attr("token") }'
sha256hmackey

AWK Unexpected Character

I got the solution for my problem from a fellow. Here is the code.
last | awk '{
if(NF>7){
if($(NF-5)=="Mar"){ # You may change the month or even accept it as a parameter
if($(NF-4) != date){datecount++};
match($(NF-3),/^([0-9]+)/,arr);
if(arr[1]>=8 && arr[1]<=16){
# Note I am using different time interval, change it accordingly
count++;
}
}
date=$(NF-4);
}
}
END{
datecount++;
print "Total logins : ",count;
print "Total dates : ",datecount;
print "Average logins : ",count/datecount;
}'
I tried saving it as name.sh and run it with bash name.sh, i tried to save it name.awk and run it as awk -f name.awk, but i still get the same error:
awk: 1: unexpected character '.'
I'm not entirely sure i run the script as i should.
NOTE: I'm working on a virtual machine on ubuntu 18.04 (I think) and I'm trying to run the script through terminal.
Seems you have big } and { braces closing issues :) Always try to indent your code in nice way so that you will get to know easily where is starting and ending block for your conditions. Could you please try following.
last | awk '{
if(NF>7){
if($(NF-5)=="Mar"){
if($(NF-4) != date){datecount++};
match($(NF-3),/^([0-9]+)/,arr);
if(arr[1]>=8 && arr[1]<=16){
count++;
}
date=$(NF-4);
}
}
}
END{
datecount++;
print "Total logins : ",count;
print "Total dates : ",datecount;
print "Average logins : ",count/datecount;
}'

How can I store the length of a line into a var withing awk script?

I have this simple awk script with which I attempt to check the amount of characters in the first line.
if the first line has more of less than 10 characters I want to store the amount
of caracters into a var.
Somehow the first print statement works but storing that result into a var doesn't.
Please help.
I tried removing dollar sign " thelength=(length($0))"
and removing the parenthesis "thelength=length($0)" but it doen't print anything...
Thanks!
#!/bin/ksh
awk ' BEGIN {FS=";"}
{
if (NR==1)
if(length($0)!=10)
{
print(length($0))
thelength=$(length($0))
print "The length of the first line is: ",$thelength;
exit 1;
}
}
END { print "STOP" }' $1
Two issues dealing with mixing ksh and awk scripting ...
no need to make a sub-shell call within awk to obtain the length; use thelength=length($0)
awk variables do not require a leading $ when being referenced; use print ... ,thelength
So your code becomes:
#!/bin/ksh
awk ' BEGIN {FS=";"}
{
if (NR==1)
if(length($0)!=10)
{
print(length($0))
thelength=length($0)
print "The length of the first line is: ",thelength;
exit 1;
}
}
END { print "STOP" }' $1

Awk command to insert corresponding line numbers except for blank lines

I'm doing an assignment at the moment and the question that's stumped me is:
"Write an awk command to insert the corresponding line number before
each line in the text file above. The blank line should NOT be
numbered in this case."
I have an answer, but I'm struggling to find the explanation of what each component does.
The command is:
awk '{print (NF? ++a " " :"") $0}' <textfile.txt>
I know that NF is the field number, and that $0 refers to the whole input record. I tried playing around with the command to find what does what, but it always seems to have syntax errors whenever I omit something.
So, my question is what does each component do? What does the ++a do? The ? after NF? and what does the bit with the quotations do?
Thanks in advance!
The instruction ... ? ... : ... it's an if-else. So, it's the same as:
if ( NF > 0 ) {
++a;
print a " " $0;
} else {
print $0;
}
a is a variable that is only incremented when found a line with fields.
print (NF? ++a " " :"") $0
a ternary operator has been used in your solution.
for a blank line NF will be 0 always
so
cond?true case:false case
if NF is >0 then print a or else print ""
a++ says that after printing increment a by 1 which will be used for next non blank line processing.
awk 'BEGIN{count=1}{if($0~/^$/){print}else{print count,$0;count++}}' your_file
tested below:
> cat temp.cc
int main ()
{
}
> awk 'BEGIN{count=1}{if($0~/^$/){print}else{print count,$0;count++}}' temp.cc
1 int main ()
2 {
3 }
>

Using a variable defined inside AWK

I got this piece of script working. This is what i wanted:
input
3.76023 0.783649 0.307724 8766.26
3.76022 0.764265 0.307646 8777.46
3.7602 0.733251 0.30752 8821.29
3.76021 0.752635 0.307598 8783.33
3.76023 0.79528 0.307771 8729.82
3.76024 0.814664 0.307849 8650.2
3.76026 0.845679 0.307978 8802.97
3.76025 0.826293 0.307897 8690.43
with script
!/bin/bash
awk -F ', ' '
{
for (i=3; i<=10; i++) {
if (i==NR) {
npc1[i]=sprintf("%s", $1);
npc2[i]=sprintf("%s", $2);
npc3[i]=sprintf("%s", $3);
npRs[i]=sprintf("%s", $4);
print npc1[i],npc2[i],\
npc3[i], npc4[i];
}
}
} ' p_walls.raw
echo "${npc1[100]}"
But now I can't use those arrays npc1[i], outside awk. That last echo prints nothing. Isnt it possible or am I missing something?
AWK is a separate process, after it finishes all internal data is gone. This is true for all external processes/commands. Bash only sees what bash builtins touch.
i is never 100, so why do you want to access npc1[100]?
What are you really trying to do? If you rewrite the question we might be able to help...
(Cherry on the cake is always good!)
Sorry, but all of #yi_H 's answer and comments above are correct.
But there's really no problem loading 2 sets of data into 2 separate arrays in awk, ie.
awk '{
if (FILENAME == "file1") arr1[i++]=$0 ;
#same for file2; }
END {
f1max=++i; f2max=++j;
for (i=1;i<f1max;i++) {
arr1[i]
# put what you need here for arr1 processing
#
# dont forget that you can do things like
if (arr1[i] in arr2) { print arr1[i]"=arr2[arr1["i"]=" arr2[arr1[i]] }
}
for j=1;j<f2max;j++) {
arr2[j]
# and here for arr2
}
}' file1 file2
You'll have to fill the actual processing for arr1[i] and arr2[j].
Also, get an awk book for the weekend and be up and running by Monday. It's easy. You can probably figure it out from grymoire.com/Unix/awk.html
I hope this helps.