Any idea on how to set global variables in gitlab-ci.yml dynamically?
DebFileName: echo $TagName | awk -F-stage '{print $1}'
Look, I am trying to do it this way where DebFileName is a global variables defined inside gitlab-ci.yml and when I am trying to access it in some job below like this
script:
- echo "DebFileName=" $DebFileName
Then it is giving this output:
$ echo "DebFileName=" $DebFileName
DebFileName= echo 1.1.97-stage | awk -F-stage "{print }"
Whereas, when I use it this way:
script:
- echo $TagName | awk -F-stage '{print $1}'
then it shows 1.1.97. And that's what I want. Any pointers?
Related
I tried to use bash variable inside awk by creating a variable in awk command as below. but it does not work it seems
b=hi
$ echo "hihello" |awk -v myvar=$b -F"$0~myvar" '{print $2}'
Actual Output is :
<empty / nothing printed >
Expected output is :
hello
Why don't you do this:
b=hi ; echo "hihello" | awk -F"$b" '{print $2}'
hello
Try the below awk command. Put the Field Separator inside BEGIN block.
$ b=hi; echo "hihello" | awk -v myvar=$b 'BEGIN{FS=myvar}{print $2}'
hello
It sets the value of myvar variable to the Field Separator. Thus inturn printing the second column will give you the string hello
I can not use unix $variable in Fiexd search of awk command.
Please see below my commands.
a="NEW_TABLES NEW_INSERT"
b="NEW"
echo $a | awk -v myvar=$b -F'$0~myvar' '{print $2}'
is not returning any output
but if manually enter the $b value there , its working as below
echo $a | awk -v -F'NEW' '{print $2}'
outputs:
TABLES NEW_INSERT
This should make it:
$ a="NEW_TABLES NEW_INSERT"
$ echo $a | awk -F"NEW_" '{print $2}'
TABLES
$ b="NEW_"
$ echo $a | awk -F"$b" '{print $2}'
TABLES
Your quotings are all messed up and you can use your variable to split the line using split function:
a="NEW_TABLES NEW_INSERT"
b="NEW"
echo $a | awk -v myvar="$b" '{split($0,ary,myvar);print ary[2]}'
Outputs:
_TABLES
I am working on a linux bash script that is run and receives variables from from CSF Firewall.
The command that CSF Firewall issues is system($config{RT_ACTION},$ip,$check,$block,$cnt,$mails); where $config{RT_ACTION} is the path to my script.
$mails=2013-04-16 11:57:14 1US8Fq-0001VC-Uu <= from#domain.com H=reverse.trace.of.ipaddress (server) [xxx.xxx.xxx.xxx]:PORT I=[xxx.xxx.xxx.xxx]:PORT P=esmtp S=5964 id=SOMEIDHERE$#com T="EMAILSUBJECT" from <from#domain.com> for to#domain.com
The problem comes when I try to run this command to get from#domain.com.
DUMPED=$5
myvar=$(echo "$DUMPED" | awk '{print $5}')
If its not clear, $mails is passed to my script which translates to $5 and the information that I want to extract with awk is located in the 5th column which also translates to $5 so instead of $5 outputting from#domain.com it outputs the full content of $mails. What am I missing? Why won't awk set myvar to from#domain.com?
What about:
DUMPED=$5
myvar=`echo $DUMPED | cut -d" " -f5`
or, using AWK:
DUMPED=$5
myvar=`echo $DUMPED | awk '{print $5}'`
It worked for me...
Hopefully some of this is illustrative. Ultimately, I'm just showing that what you have already works, unless you're trying to define the variable literally with the dollar sign already in it.
$ # Define mails. Don't do $mails=something, that will fail.
$ mails='2013-04-16 11:57:14 1US8Fq-0001VC-Uu <= from#domain.com H=reverse.trace.of.ipaddress (server) [xxx.xxx.xxx.xxx]:PORT I=[xxx.xxx.xxx.xxx]:PORT P=esmtp S=5964 id=SOMEIDHERE$#com T="EMAILSUBJECT" from <from#domain.com> for to#domain.com'
$ # Direct the value of $mails to awk's standard input using a here string:
$ awk '{print $5}' <<< "$mails"
from#domain.com
$ # Direct the value of $mails to awk's standard input using echo and a pipe:
$ echo "$mails"| awk '{print $5}'
from#domain.com
$ # Assign the fifth word of "$mails" to the name "myvar" using read;
$ read _ _ _ _ myvar _ <<< "$mails"
$ echo "$myvar"
from#domain.com
i=0
while read line
do
echo "i is --- $i"
#echo $line "\n"
if (( $i > 0 ))
then
$Eda_package=$(echo $line | awk '{print $1}')
$well_bias=$(echo $line | awk '{print $2}')
$biasmap=$(echo $line | awk '{print $3}')
$unified=$(echo $line | awk '{print $4}')
echo "eda pack --$Eda_package wellbias is --$well_bias biasmap is --$biasmap unified- -- $unified"
fi
i=$((i+1))
done < config.list
In the above bash program I get an error:
./script.sh: line 9: =EDA_7p0: command not found
How do I fix this?
Lines of the form:
$xyzzy=plugh
will have xyzzy substituted before they're executed so that they look like:
=plugh
assuming they're not yet set. If they are set, you'll probably get different behaviour but still almost certainly not what you want.
You should change your lines from (for one example):
$Eda_package=$(echo $line | awk '{print $1}')
to:
Eda_package=$(echo $line | awk '{print $1}')
The $ is not part of the variable name, it's an indication that the following word is a variable that should be substituted.
Let var1=1 and var2=2 now if you simply write $var2=$var1 then it will give you error that 2=1 command not found
When you initialize any variable you have to do it without $ with variable name on left side
how to retrive every portion separately from following file name? DSA4020_frontcover_20346501_2011-05.doc
I want to retrieve informations as below;
name = DSA4020
type = frontcover
id = 20346501
date = 2011-05
is it possible to do with sed??
Yes, you can:
pax$ echo 'DSA4020_frontcover_20346501_2011-05.doc' | sed
-e 's/^/name=/'
-e 's/_/\ntype=/'
-e 's/_/\nid=/'
-e 's/_/\ndate=/'
-e 's/\..*//'
name=DSA4020
type=frontcover
id=20346501
date=2011-05
That's all on one line, I've just split it for readability.
You could also do it with awk if you wish:
pax$ echo 'DSA4020_frontcover_20346501_2011-05.doc'
| awk -F_ '{print "name="$1"\ntype="$2"\nid="$3"\ndate="substr($4,1,7)}'
name=DSA4020
type=frontcover
id=20346501
date=2011-05
awk may be a better choice
# f=DSA4020_frontcover_20346501_2011-05.doc
# name=$(echo $f | awk -F_ '{print $1}')
# echo $name
DSA4020
# type=$(echo $f | awk -F_ '{print $2}')
# echo $type
frontcover
In pure bash
FILE="DSA4020_frontcover_20346501_2011-05.doc"
eval $(echo $FILE |(IFS="_";read a b c d; echo "name=$a;type=$b;id=$c;date=${d%.doc}"))
echo Name:$name Type:$type ID:$id DATE:$date