CSH - check if variable is integer or not - unix

Have tried this, but then even though i put a number as $3, it still goes in the "not numeric" statement
if ($#argv == 3) then
if ( $3 !~ '^[0-9]+$' ) then
echo "Not numeric"
exit 1
else
echo "Numeric"
endif
endif

Obligatory "don't use csh" links - http://www-uxsup.csx.cam.ac.uk/misc/csh.html and http://www.grymoire.com/unix/CshTop10.txt
That said, your problem is that you have single quoted the regex pattern. Changing it to this should work:
if ( $3 !~ ^[0-9]+$ ) then

I am sure that there are better ways (the ($3 !~ ^[0-9]+$) does not work for me) but I use this:
echo "$3" | awk '{print $1+0}'
The "trick" is in $1+0 (no quotes!) where you force awk to interpret $1 as a number.
Note:
If yow want to know if a non-numeric value is inputed, then use this:
echo "$3" | awk '{if ($1 ~ /^[0]+$/) {print "0"} else {if ($1+0 == 0) {print "Not a number"} else {print $1+0} }}'

Related

Unix partial string comparison

I need to compare a string partially to check for a given condition.
Like my $1 will be checked if it has a part of a string BLR
while my file input has $1 entries as BLR21 BLR64 IND23
I only need a true condition when $1 is equal to BLR**
where these stars can be anything.
I used a simple if condition
if($1=="BLR21")
{print $2}
Now this only works when whole BLR21 is there in row.
I need to ckeck not for BLR21 but only BLR.
Please Help
Your question is not great, I hope I understood.
Quick and easy solution
grep BLR input.txt
This will output all the lines in which "BLR" is found, in file input.txt. It will match "BLR" with any prefix and suffix, whatever they might be (spaces, alphanumerical, tabs, ...).
"Complicated" solution
A bit more complicated. It does the same thing, but makes sure input.txt exists, and is in the form of a script.
Input file, input.txt:
BLR21 BLR64 IND23
Your script could be:
#!/bin/bash
#
# Arguments
inputfile="input.txt"
if [[ $# -ne 1 ]]
then
echo "Usage: myscript.bash <STRING>"
exit 1
else
string="$1"
fi
# Validation, and processing...
if [[ ! -f "$inputfile" ]]
then
echo "ERROR: file >>$inputfile<< does not exist."
exit 2
else
grep "$string" "$inputfile"
fi
And to call the script, you do:
./myscript.bash BLR
But really, a simple grep does the job here.
Taking it even further...
#!/bin/bash
#
# Arguments
inputfile="input.txt"
if [[ $# -ne 1 ]]
then
echo "Usage: check.bash <STRING>"
exit 1
else
string="$1"
fi
# Validation, and processing...
if [[ ! -f "$inputfile" ]]
then
echo "ERROR: file >>$inputfile<< does not exist."
exit 2
else
while read -r line
do
if [[ "$line" =~ $string ]]
then
echo "$line"
fi
done <"$inputfile"
fi
Now this one is like going to the moon via mars...
It reads each line of the file, one by one. Then it checks if that line contains the string, using the =~ operator inside the if.
But this is crazy, when a simple grep would do.

How to include multiple search string in single code line

I have a code which searches IN string in a file then puts those records in a different file for which I am using below:
cat ${ALL_CARDS} | grep -v ' IN ' | awk '{print $1}' > ${NO_EXEC_CARDS}
But now I have to include multiple search string along with IN like NE & ON so how to include those also in this code line
cat | grep | awk is an anti-pattern. Also, just using awk makes your question trivial:
awk '! /IN/ && ! /ON/ && ! /NE/ {print $1}' "$ALL_CARDS" > "$NO_EXEC_CARDS"
You have some additional whitespace in your grep, so perhaps you want:
awk '! / IN / && ! / ON / && ! / NE / {print $1}'
awk is well designed for this. It simply reads each line, and if the expression does not match " IN " or " ON " or " NE ", then the action is executed. You could also write this as:
awk '! (/ IN / || / ON / || / NE /) {print $1}'

awk change shell variable

I would like to modify several shell variables within awk:
echo "$LINE_IN" | awk '/pattern1/ {print $0; WRITTEN=1; REC=$REC+1}' >> $FILE1
I tried to put eval, but still does not work:
eval $( echo "$LINE_IN" | awk '/pattern1/ {print $0; WRITTEN=1; REC=$REC+1}' >> $FILE1 )
Any suggestion?
I would like to use k-shell script, thanks!
Count the hits when you are finished:
echo "${LINE_IN}" | grep -E 'pattern1' > "${FILE1}"
REC=$(wc -l < "${FILE1}")
if (( REC > 0 )); then
WRITTEN=1
fi
When you really want to use awk, you must let awk write the results to stdout and parse stdout:
echo "${LINE_IN}" | awk '/echo/ {print $0 > "x3"; WRITTEN=1; REC++}
END { print "WRITTEN=" WRITTEN; print "REC=" REC}'
WRITTEN=1
REC=6
And when you want the variables really set, wrap it:
source (echo "${LINE_IN}" | awk '/echo/ {print $0 > "x3"; WRITTEN=1; REC++}
END { print "WRITTEN=" WRITTEN; print "REC=" REC}')
Note: Get used to using lowercase variable names like written, file and rec.

awk passing a variable

I am struggling with an awk problem in my bash shell script. In the below snippet of code i am passing a variable var_awk for regular expression in awk. The idea is to get lines above a regular expression but the below echo is not displaying any data
echo `ls -ltr $date*$f* | /usr/xpg4/bin/awk -v reg=$var_awk '/reg/ {print $0}'`
I am unable to reg for regex though when i do print reg it is printing but when not doing regex as expected.
if [ $GE == "HBCA" ] || [ $GE == "HBUS" ] || [ $GE == "HBEU" ]; then
for f in `ls -ltr $date*GEN*REVAL*log|grep -v LPD | awk '{split($9,a,"_")}{print a[3]}'`; do
echo $f
var_awk="$date"_RESET_CALC_"$f"
echo $var_awk
echo `ls -ltr $date*$f* | /usr/xpg4/bin/awk -v reg=$var_awk '/reg/ {print $0}'`
You cannot use variable in regex that way. You need to do:
/usr/xpg4/bin/awk -v reg="$var_awk" '$0~reg{ print $0 }'
or simply
/usr/xpg4/bin/awk -v reg="$var_awk" '$0~reg'
Inside / / your variable reg will be used as a literal word.
Quote your shell variables.
try this:
...whatever you had already..|awk -v reg="$var_awk" '$0~reg'
it is better to wrap shell variable with quotes, e.g. if your var has spaces.
/pattern/ in awk is called regex constant. It cannot be used with variable, that's why it is called constant. We need to use dynamic regex here in this example.

awk command not working

My code gives me an error at line 3 with message:
syntax error near unexpected token ``ARRAY1=$(awk 'FNR == 1{print $2}' $file)
set -vx
for file in ls bowtie-0.12.7-win32/bowtie-0.12.7/Hits_635_25bp/*.txt
ARRAY1=$(awk 'FNR == 1{print $2}' $file) # stores the value
let ARRAY1=$ARRAY1/100+1
echo $ARRAY1
value1=$(awk 'FNR == '$ARRAY1'{print $1}' bowtie-0.12.7-win32/bowtie-0.12.7/list_for_635_fasta.list) # GETS THE VALUES FROM THE LIST
echo $value1
value2=$(awk 'FNR == 1{print $2,$3,$4,$5,$6,$7}' bwa-0.5.9/bwa-0.5.9/GENOMES/${file:0:13}) # GETS THE VALUES FROM THE LIST
echo $value2
done
I'd appreciate it if someone could point out the mistake. It seems to make sense, but I guess I'm missing something.
Try this:
set -vx
for file in bowtie-0.12.7-win32/bowtie-0.12.7/Hits_635_25bp/*.txt; do
ARRAY1=$( awk 'FNR == 1 { print $2 }' "$file" )
let ARRAY1=$ARRAY1/100+1
echo "$ARRAY1"
value1=$( awk -v a1="$ARRAY1" 'FNR == a1 { print $1 }' bowtie-0.12.7-win32/bowtie-0.12.7/list_for_635_fasta.list )
echo "$value1"
value2=$( awk 'FNR == 1 { print $2, $3, $4, $5, $6, $7 }' bwa-0.5.9/bwa-0.5.9/GENOMES/"${file:0:13}" )
echo "$value2"
done
If you tell us what shell you're using, we could improve the code.
for file in ls bowtie-0.12.7-win32/bowtie-0.12.7/Hits_635_25bp/*.txt
above line should be
for file in `ls bowtie-0.12.7-win32/bowtie-0.12.7/Hits_635_25bp/*.txt`
do

Resources