Value - ask-API-v1.0.7. Value must increment like ask-API-v1.0.8 ,ask-API-v1.0.9 and so on. Pls get me syntax using awk - scripting

Value - ask-API-v1.0.7.
Value must increment like ask-API-v1.0.8 ,ask-API-v1.0.9 and so on.
Please get me syntax using awk
Expectation -ask-API-v1.0.8 ,ask-API-v1.0.9 and so on

Related

Decrement or increment a variable in the robot framework

I just want to decrement the variable N_groups in the last line.
This is my robot file:
Preconditions - Delete Groups But Not First
${N_groups} Setup Groups Count Groups
Log to console N_groups: ${N_groups}
: FOR ${INDEX} IN RANGE 1 20
\ Run Keyword If '${N_groups}' == '1' Exit For Loop
\ Setup Groups Delete Group ${group}
\ ${N_groups}= ${N_groups}-1
I get the error:
No keyword with name '${N_groups}-1' found.
What I am doing wrong here?
Try putting it inside the var name. i.e.
${N_groups-1}
If the variable is already a number, you can use:
${N_groups}= ${N_groups-1}
To do this you need to coerce it to a number (otherwise you'll get an error saying failed: TypeError: coercing to Unicode: need string or buffer, int found), e.g.
*** Variables ***
${N_groups}= ${0} # ${} notation coerces value to a number
Alternatively, you can use Evaluate like this, which works whether ${N_groups} has been coerced to a number or not:
${N_groups}= Evaluate ${N_groups} - 1
Try this:
${decrement_counter}= set variable 1
-- inside the loop
${N_groups}= Evaluate ${N_groups} - ${decrement_counter}
Note - only one space before and after subtraction sign.

awk print 4 columns with different colours - from a declared variable

I'm just after a little help pulling in a value from a variable. I'm writing a statement to print the contents of a file to a 4 columns output on screen, colouring the 3rd column depending on what the 4th columns value is.
The file has contents as follows...
Col1=date(yymmdd)
Col2=time(hhmmss)
Col3=Jobname(test1, test2, test3, test4)
Col4=Value(null, 0, 1, 2)
Column 4 should be a value of null, 0, 1 or 2 and this is the value that will determine the colour of the 3rd column. I'm declaring the colour codes in a variable at the top of the script as follows...
declare -A colours
colours["0"]="\033[0;31m"
colours["1"]="\033[0;34m"
colours["2"]="\033[0;32m"
(note I don't have a colour for a null value, I don't know how to code this yet but I'm wanting it to be red)
My code is as follows...
cat TestScript.txt | awk '{ printf "%20s %20s %20s %10s\n", "\033[1;31m"$1,"\033[1;32m"$2,${colours[$4]}$3,"\033[1;34m"$4}'
But I get a syntax error and can't for the life of me figure a way around it no matter what I do.
Thanks for any help
Amended code below to show working solution.
I've removed the variable set originally which was done in bash, added an inline variable into the awk line...
cat TestScript.txt | awk 'BEGIN {
colours[0]="\033[0;31m"
colours[1]="\033[0;34m"
colours[2]="\033[0;32m"
}
{printf "%20s %20s %20s %10s\n","\033[1;31m"$1,"\033[1;32m"$2,colours[$4]$3,"\033[1;34m"$4}'
}
Just define the colours array in awk.
Either
BEGIN {
colours[0]="\033[0;31m"
colours[1]="\033[0;34m"
colours[2]="\033[0;32m"
}
or
BEGIN { split("\033[0;31m \033[0;34m \033[0;32m", colours) }
But in the second way, remind the first index in the array is 1, not 0.
Then, in your printf sentence the use of colours array must be changed to:
,colours[$4]$3,
But if you have defined the array using the second method, then a +1 is required:
,colours[$4+1]$3,
Best regards
In awk you can use the built-in ENVIRON hash to access the environment variables.
So instead of ${colours[$4]} (which syntax is for bash not for awk) you can write ENVIRON["something"]. Unfortunately arrays cannot accessed on this way. So instead of using colours array in environment you should use colours_1, ..., colours_2. and then you can use ENVIRON["colours_"$4].

Awk Sum skipping Special Character Row

I am trying to take the sum of a particular column in a file i.e. column 18.
Using awk command along with Printf to display it in proper decimal format.
SUM=`cat ${INF_TARGET_FILE_PATH}/${EXTRACT_NAME}_${CURRENT_DT}.txt|awk -F"" '{s+=$18}END{printf("%24.2f\n", s)}'
Above command is skipping those rows in file which has the special character in one of the column 5 - RÉPARATIONS. Hence Awk skips these rows and doesnt consider sum for that row. Please help how to resolve this issue to take sum of all rows.
There is missing a back tics in your example, should be:
SUM=`cat ${INF_TARGET_FILE_PATH}/${EXTRACT_NAME}_${CURRENT_DT}.txt|awk -F"" '{s+=$18}END{printf("%24.2f\n", s)}'`
But you should not use back tics, you should use parentheses $(code)
Using cat to enter data to awk is also wrong way to do it, add pat after awk
SUM=$(awk -F"" '{s+=$18} END {printf "%24.2f\n",s}' ${INF_TARGET_FILE_PATH}/${EXTRACT_NAME}_${CURRENT_DT}.txt)
This may resolve your problem, but gives a more correct code.
If you give us your input file, it would help us to understand the problem.

How does associative arrays work in awk?

I wanted to remove duplicate lines from a file based on a column. A quick search let me this page which had the following solution:
awk '!x[$1]++' filename
It works, but I am not sure how it works. I know it uses associate arrays in awk but I am not able to infer anything beyond it.
Update:
Thanks everyone for the explanation. With my new knowledge, I have wrote a blog post with further explanation of how it works.
That awk script !x[$1]++ fills an array named x. Suppose the first word ($1 refers to the first word in a line of text) in a line of text is line1. It effectively results in this operation on the array:
x["line1"]++
The "index" (the key) of the array is the text encountered in the file (line1 in this example), and the value associated with that key is an integer that is incremented by 1.
When a unique line of text is encountered, the current value of the array is zero, which is then post-incremented to 1. The not operator ! evaluates to non-zero (true) for each new unique line of text and so prints it. The next time the same value is encountered, the value in the array is non-zero and so the not operation results in zero (false), so the line is not printed.
A less "clever" way of writing the same thing (but possibly more clear and less fun) would be this:
{
if (x[$1] == 0 )
print
x[$1]++
}

Assigning value of a filed (positional variable) into a user defined variable in gawk/awk

I am creating a variable called "size" and trying to assign a value to it from gawk positional variable. But, that does not seem to work. In the example below, I am trying to store the value of field 4 into a variable "size". When I print the variable size, entire line is printed instead just the filed 4.
How can I save the filed value into a variable for later use?
prompt> echo "Live in a big city" | gawk '/Live/ {size=$4; print $size}'
The following is outputted:
Live in a big city
I would like to see just this:
big
Leave out the dollar sign. awk is like C, not like shell or perl, where you don't need any extra punctuation to dereference a variable. You only use a dollar sign to get the value of the n'th field on the current line.
echo "Live in a big city" | gawk '/Live/ {size=$4; print size}'
The reason you get the whole line printed is this: the awk variable size is assigned the value big. Then, in the print statement, awk dereferences the size variable and attempts print $big. The string "big" is interpreted as an integer and, as it does not begin with any digits, it is treated as the number 0. So you get print $0, and hence the complete line.