First character of variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting First character of variable
# 1  
Old 05-06-2014
First character of variable

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:

Code:
#!/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"

Thank you.
# 2  
Old 05-06-2014
grep command returns zero when it match the pattern and non zero when it does not match the pattern:
Code:
EXIT STATUS

The following exit values are returned:
0         One or more lines were selected.
1         No lines were selected.
>1        An error occurred.


Last edited by Yoda; 05-06-2014 at 07:24 PM..
This User Gave Thanks to Yoda For This Post:
# 3  
Old 05-06-2014
try something like:
Code:
#!/bin/tcsh
 set case1 = test0.txt
set nan=1
grep -q "nan" $case1 || set nan=0
if ( $nan == 0 ) then
     echo "no nan found"
else
    echo "nan found"
endif

This User Gave Thanks to rdrtx1 For This Post:
# 4  
Old 05-07-2014
Why not use 'regular' return codes?
Code:
#!/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:
Code:
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.

Code:
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:
Code:
✔ ~ $ 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
✔ ~ $


Last edited by sea; 05-07-2014 at 10:18 AM..
This User Gave Thanks to sea For This Post:
# 5  
Old 05-07-2014
First character of variable

Thank you all of the replies. Very helpful. Ended up using rdrtx1's solution.

Thank you.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Store ^M character in variable

Hi, I want to store ^M character in a variable to be later written to a file. Can you please help. TempOut="$_var1 `print '\x0D'` $_var1" ..... .... echo $TempOut >> logfile TempOut="$_var1 `echo -e '\x0D'` $_var1" ..... .... echo $TempOut >> logfile But both ways I am... (2 Replies)
Discussion started by: tostay2003
2 Replies

2. Shell Programming and Scripting

How to store a escape character in a Variable.?

How to store escape character in the variable. Var=abc,def,ghi,jkl echo ${Var} | sed -e "s/,/|\\\\./g;s/^/\\\\./g" \.abc|\.def|\.ghi|\.hjk Var1=`echo ${Var} | sed -e "s/,/|\\\./g;s/^/\\\./g"` Actual: ------- echo $Var1 .abc|.def|.ghi|.jkl Expected: --------- echo $Var1... (4 Replies)
Discussion started by: deepakwins
4 Replies

3. UNIX for Dummies Questions & Answers

newline character in a variable

variable="unix\nlinux" echo $variable expected output: unix linux :wall: can i do that ?? thanks in advance!! (3 Replies)
Discussion started by: sathish92
3 Replies

4. Shell Programming and Scripting

Get character position within variable

Hi all let's say i have a file named 1234_v2_abcd i need to find the position of the character located after _v, in above case this would be character number 2 the count of the characters before _v can change, but i always have a number after _v can anybody help :) (4 Replies)
Discussion started by: gigagigosu
4 Replies

5. Shell Programming and Scripting

Variable with hash character

Hi Experts, I need to have a variable with value initialised to it.The value will possess a hash character with it. ex: var=reports#2 so how I am suppose to do this? (1 Reply)
Discussion started by: cnraja
1 Replies

6. Shell Programming and Scripting

Removing a character from a variable and assigning it to another variable?

Hi folks. I have this variable called FirstIN that contains something like this: 001,002,003,004... I am trying to assign the content of this variable into ModifiedIN but with the following format : 001 002 003 004...(changing the commas for spaces) I thought about using sed but i am not... (17 Replies)
Discussion started by: Stephan
17 Replies

7. UNIX for Dummies Questions & Answers

read a variable character by character, substitute characters with something else

im having trouble doing this: i have a variable with 2 characters repeating e.g. aababbbaaaababaabbaabbba is there a way i can search the variable for a's and b's and then change a's to b's and b's to a's? im guessing its like getting the 1's compliment of the string im doing this in... (2 Replies)
Discussion started by: vipervenom25
2 Replies

8. Shell Programming and Scripting

Removing character ' from a variable

Hello there, I have a variable in the form of '/example/file.txt' . I want to remove the ' characters from the beginning and the end so that the my new variable becomes /example/file.txt . How can I do it in a script? I know this is a fairly easy question, but i wasn't able to implement it. (3 Replies)
Discussion started by: sertansenturk
3 Replies

9. Shell Programming and Scripting

Sed a variable which has - character in it

Hi Because my GR_RESOURCE_DIR variable has - character in it the sed doesn't work. Could you please advise ? # cat globalrelay.ini ResourceDIR=/app-nfs/meta/relay # cat xac.sh #!/bin/bash #XAC_INSTALL_PATH=/usr/XenonAdministrationCenter GR_RESOURCE_DIR=new-app sed -e... (6 Replies)
Discussion started by: potro
6 Replies

10. Shell Programming and Scripting

Search in variable for character

qwertyuioplkjhgfdsa (1 Reply)
Discussion started by: rorey_breaker
1 Replies
Login or Register to Ask a Question