How to trim a set of numbers from a string in Jenkins? - jenkins

How to trim set of numbers from a sting in Jenkins. This is what I am trying but some how there is one mistake not able to fix it.
def Build_Num = sh(script: "echo ${BUILD_PATH} | awk 'BEGIN {FS="_"};{print $NF}' | sed 's/[A-Za-z]*//g'", returnStdout: true).trim()
BUILD_PATH=ABC/EFGH/ABCD_1.2.3456.78912/
Result should be like this: 1.2.3456.78912
Directly in shell I am trying to achieve with below command but not able to do it in Jenkins:
echo ABC/EFGH/ABCD_1.2.3456.78912/ | awk 'BEGIN {FS="_"};{print $NF}' | sed 's/[A-Za-z]*//g'

You need to escape your $ character for your awk argument, otherwise Groovy will perform string interpolation.
Of course, we do not want to escape the $ for ${BUILD_PATH}, because in that case we do want string interpolation, we just don't want string interpolation for $NF.
Try this:
def Build_Num = (sh script: """echo ${BUILD_PATH} | awk 'BEGIN {FS="_"};{print \$NF}' | sed 's/[A-Za-z]*//g'""", returnStdout: true).trim()

Related

Grep search for binary data

Why does this command work
echo -ne "\x74\x79\xf4"|grep -aPo "\x74\x79"
and this doesn't?
echo -ne "\x74\x79\xf4"|grep -aPo "\x74\x79\xf4"
The following will work:
echo -ne "\x74\x79\xC3\xB4" | grep -aPo "\x74\x79\xf4"
# ^^^^^^^
echo -ne "\x74\x79\u00F4" | grep -aPo "\x74\x79\xf4"
# ^^^^^^
The \xF4 is not a single byte, it consists of two bytes, C3 and B4. Thus, to encode it properly in the echo command, you need to use \xC3\xB4 sequence.
With \u00F4, you specify the code unit.

grep for pattern with special character and output only matched string

Team,
I want to grep for a substring container - and then only output that string and not whole line. how can i? I know i can awk on space and pull using $ but want to know how to do in grep?
echo $test_pods_info | grep -F 'test-'
output
test-78ac951e-89a6-4199-87a4-db8a1b8b054f export-9b55f0d5-071d-431-1d2ux0-avexport-xavierisp-sjc4--a4dd85-102 1/1 Running 0 19h
expected output
test-78ac951e-89a6-4199-87a4-db8a1b8b054f
awk is more suitable for this as you want to get first field in a matching line:
awk '/test-/{print $1}' <<< "$taxIncluded"
test-78ac951e-89a6-4199-87a4-db8a1b8b054f
If you really want to use grep then this might be what you're looking for:
grep -o 'test-\S*' <<< "$taxIncluded"
or:
grep -o 'test-[^[:space:]]*' <<< "$taxIncluded"
Try
echo $test_pods_info | grep -o 'test-'
the -o option is:
show[ing] only the part of a line matching PATTERN
according to grep --help. Of course, this will only print test-, so you'll need to rework your regex:
grep -oE '(test).*[[:space:]]\b'
Figured it out..
echo $test_pods_info | grep -o "\test-\w*-\w*\-\w*\-\w*\-\w*"
outoput
test-78ac951e-89a6-4199-87a4-db8a1b8b054f
but i wish there is simple way. like \test-*\

How to do operations with form values?

so i have a form with 3 text fields, in two of them, users enters a code (and OID), which i use to run a command (snmpbulkwalk). So i want to store the outputs of those commands in a variable, lets say var1 and var2.
[]
But then, in the last text field, i want to do and operation with these variables, like: var1 - var2.
So far i tried this:
#var1 = "nice -n 19 snmpbulkwalk -v 2c -c %snmp_community
%sensor_ip "+ "#{monitor_category_params[:oid].strip}"+" | awk
'/kB$/{ $(NF-1)= $(NF-1)*1024;} {print $0}' | sed 's/ [A-Za-
z]*$//' | awk '{print $NF}' | grep -o '[[:digit:]]*' | paste -s
-d';' -"
puts("var1 = " + "#{#var1}");
#var2 = "nice -n 19 snmpbulkwalk -v 2c -c %snmp_community
%sensor_ip "+ "#{monitor_category_params[:oid2]}"+" | awk
'/kB$/{ $(NF-1)= $(NF-1)*1024;} {print $0}' | sed 's/ [A-Za-
z]*$//' | awk '{print $NF}' | grep -o '[[:digit:]]*' | paste -s
-d';' -"
puts("var2 = " + "#{#var2}");
#var3 = "#{monitor_category_params[:snmp_oper].strip}"
puts("var3 with {} = " + "#{#var3}");
system = '$(#{#var3})'
puts(system(system));
The thing is i dont know how to store the output of the shell command "nice -n 19 ...blabla" in the variable. I used exec and backticks like this:
exec "nice -n 19 snmpbulkwalk -v 2c -c %snmp_community %sensor_ip "+ "#{monitor_category_params[:oid].strip}"+" | awk '/kB$/{ $(NF-1)= $(NF-1)*1024;} {print $0}' | sed 's/ [A-Za-z]*$//' | awk '{print $NF}' | grep -o '[[:digit:]]*' | paste -s -d';' -"
but it gives me this error, which i dont get it since the command is well formatted.
sh: -c: línea 0: EOF inesperado mientras se buscaba un `)' coincidente
sh: -c: línea 1: error sintáctico: no se esperaba el final del fichero
I dont know if im losing arguments (%snmp_community or %sensor_ip), but the final result should be something like system = number - number. but i only get system = var1 - var2, which is not doing nothing since i want the outputs of the commands, not the name of the variables.
sorry if didnt explain myself good, thank you in advance.
So assuming this is related with ruby or ruby on rails
#var1 and #var2 are instance variable of a method in some class.
You can directly use the backticks directly to save to output from the shell command
#var1 = `ls`
p #var1
However, some bash commands do not return a string value directly.
I have not used snmpbulkwalk till now but $PATH might be similar in a way
#var1 = `echo $PATH` #add you own shell code here to check
p #var1

Jenkinsfile how to grep file name in variable

I have file in different server and that name will change.
File: testfile-1.2-12345.sh ,"12345" is going to change.
How I get chancing text to variable?
In server machine this works and it's prints 12345:
ls ~/test/testfile* | awk -F'[-.s]' '{print $5}'
But when I do it from jenkins it wont work:
def versio = sh "ssh user#${ip} ls ~/test/testfile* | awk -F'[-.s]' '{print \$5}'"
It prints "12345" but if I try to print ${versio} it shows null.
Your command is correct. But in pipeline you need to specify returnStdout:true. Detailed documentation is here.
def versio = sh returnStdout: true, script: 'ssh user#${ip} ls ~/test/testfile* | awk -F\'[-.s]\' \'{print \\$5}\''

How do i Extract integer value from a string in Unix

when i type this command
/usr/local/afs7/bin/afs_paftools -a about.afs | grep TOTAL_DOCUMENTS
I get a result
TOTAL_DOCUMENTS = 74195
How i can extract the integer number(74195) after =
using grep command
One way is to use grep:
$ echo "TOTAL_DOCUMENTS = 74195" | grep -o '[0-9]\+'
74195
or since you know, that it's the last field, use awk:
$ echo "TOTAL_DOCUMENTS = 74195" | awk '{print $NF}'
74195
or just use awk for the lot:
your-command -a about.afs | awk '/TOTAL_DOCUMENTS/{print $NF}'
If there are no space:
TOTAL_DOCUMENTS=74195
Use this awk
echo "TOTAL_DOCUMENTS=74195" | awk -F= '{print $NF}'
74195

Resources