I am trying to detect 'nan' (not a number) string in my large txt file filled with lots of numbers. A file may have no or many 'nan'. It should be simple but getting errors. Any help appreciated. Here is what I have:
#!/bin/tcsh
set case1 = test0.txt
set chk = `grep 'nan' $case1`
set op = `$?`
if ($op == 0) then
echo "no nan found"
else
echo "nan found"
endif
Whatever is the file containing 'nan' or not, it always outputs "nan found"
#!/bin/tcsh
set case1 = test0.txt
set search = nan
# Search value & set retval
grep -q "$search" "$case1"
set RET = $?
# Print result
if ( 0 == $RET ) then
echo "$RET - $search found"
else
echo "$RET - $search not found"
endif
hth
EDIT:
By regular return value i mean:
EXPRESION || set nan=0
This will change the variable to zero (success) on fail, which is quite irritating when someone later has/will edit the script.
grep -q "$search" "$case1"
set RET = $?
This will set the return code always to what it was.
Also this way you could catch the error of a non existing file..
Example:
~ $ tcsh
grep: test0.txt: No such file or directory
2 - nan not found
2 ~ $ touch test0.txt
:) ~ $ tcsh
1 - nan not found
~ $ echo nan > test0.txt
+ ~ $ tcsh
0 - nan found
~ $