This question already has answers here:
KSH check if string starts with substring
(4 answers)
Closed 8 years ago.
VARIABLE=`grep PortNumber` testfile.txt | awk -F'"' '{print $2}'`
echo $VARIABLE
33111
I want to do a check to ensure the first 2 numbers of the variable are the digit '3' only.
How can I do this using a standard ksh script?
EDIT:
I think I have it in the following, does this look correct?
echo $VARIABLE | egrep -q '^[3]{2}'
You can do :
echo $VARIABLE | grep -E "^3+{2}"
Related
This question already has answers here:
How do I set a variable to the output of a command in Bash?
(15 answers)
Closed 3 years ago.
I've tried to write the following code in bash but I am not able to get the output to be echoed.
part1="blkid | grep -P 'CENTOS 7' | cut -c1-9"
echo "$part1"
Try this
part1=$(blkid | grep -P 'CENTOS 7' | cut -c1-9)
echo "$part1"
This question already has answers here:
Floating-point arithmetic in UNIX shell script
(4 answers)
Closed 3 years ago.
I am executing below as part of bash script on solaris 10.
MEM_USED_PC=`prstat -Z 1 1 | grep -i sz | awk '{print $5}' | sed 's/%//'`
MEM_TOTAL_PC=100
MEM_FREE_PC=$(($MEM_TOTAL_PC-$MEM_USED_PC))
but echo $MEM_FREE_PC gives below error:
100-6.5: syntax error: invalid arithmetic operator (error token is ".5")
What could be the problem?
You can use the calculator CLI, bc
MEM_FREE_PC=$(echo "$MEM_TOTAL_PC - $MEM_USED_PC" | bc)
echo $MEM_FREE_PC
Since bash doesn't support floating point, you need something like awk to compute the result:
$ MEM_TOTAL_PC=100
$ MEM_USED_PC=99.33
$ MEM_FREE_PC=$(awk -v MEM_TOTAL_PC=$MEM_TOTAL_PC -v MEM_USED_PC=$MEM_USED_PC 'BEGIN {print MEM_TOTAL_PC-MEM_USED_PC}')
$ echo $MEM_FREE_PC
0.67
This question already has answers here:
Remove a fixed prefix/suffix from a string in Bash
(9 answers)
Closed 2 years ago.
I have 2 variables one which is holding the prefix and other one the complete string.
For e.g
prefix="TEST_"
Str="TEST_FILENAME.xls"
I want the the Str to be compared against prefix and remove that common characters 'TEST_' and i want the output as FILENAME.xls. Please advise if it can be done with minimal lines of coding. Thanks a lot.
Using BASH you can do:
prefix="TEST_"
str="TEST_FILENAME.xls"
echo "${str#$prefix}"
FILENAME.xls
If not using BASH you can use sed:
sed "s/^$prefix//" <<< "$str"
FILENAME.xls
Try this:
$ Str=$(echo $Str | sed "s/^${prefix}//")
$ echo $Str
FILENAME.xls
Or using awk:
$ echo $Str | awk -F $prefix '{print $2}'
FILENAME.xls
This question already has answers here:
How can I remove the extension of a filename in a shell script?
(15 answers)
Closed 6 years ago.
Have a string as xxxxxxx.txt
Need to get the only xxxxxxx string without extension.
How to get this using korn shell?
s=abcdef.txt
s_base=${s%.txt}
...will assign abcdef to s_base.
STRING=xxxxxxxxxx.txt
echo $STRING | rev | cut -f 2- -d "." | rev
This question already has answers here:
How to split a string into an array in Bash?
(24 answers)
Closed 7 years ago.
Suppose I have a file with a name ABC_DE_FGHI_10_JK_LMN.csv. I want to extract the ID from the file-name i.e. 10 with the help of ID position and file-name separator. I have following two inputs
File-name_ID_Position=4; [since 10 is at fourth position in file-name]
File-name_Delimiter="_";
Here ID can be numeric or alpha-numeric. So how extract the 10 from above file with the help of above two inputs. How to achieve this in bash?
Instead of writing a regex in bash, I would do it with awk:
echo 'ABC_DE_FGHI_10_JK_LMN.csv' | awk -F_ -v pos=4 '{print $pos}'
or if you want the dot to also be a delimiter (requires GNU awk):
echo 'ABC_DE_FGHI_10_JK_LMN.csv' | awk -F'[_.]' -v pos=4 '{print $pos}'