Need help on If statement for comparison


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need help on If statement for comparison
# 1  
Old 11-26-2010
Need help on If statement for comparison

Hi,
We are using AIX and while using KSH Unix script, we are reading data in a file and then using the if statement for comparison. The code with if statement is as follows;

Code:
 
cat $FILETMP | while read a
do
        jobstr1=`echo $a | awk '{print $1}'`
        var1=`echo $jobstr1 | cut -c 9`
        var2=`echo $jobstr1 | cut -c 17`
        if [ $var1 = _ -a $var2 = _ ]
        then
                echo "$a" >> $outfile
        fi
done

The code is working fine and writing the data to the file "outfile", but the code gives errors. The errors are when the characters are "empty" or "blank" then while comparing the system is giving errors as follows;

ksh: _: unknown test operator
ksh: test: argument expected

Please help to have the code without having error messages. I would also like to know if there is a better way of doing this.
Thanks for the help.
# 2  
Old 11-26-2010
Try:
Code:
if [ "${var1}." = "_." -a "${var2}." = "_." ]

# 3  
Old 11-26-2010
Here is two alternative ways to do it:
Code:
while read line
do
  case $line in 
    ????????_???????_*) echo "$line"
  esac
done < "$FILETMP" > "$outfile"

Code:
awk -F "" '$9=="_"&&$17=="_"' "$FILETMP" > "$outfile"

# 4  
Old 11-26-2010
Thanks - it worked!!

Thanks to all,

All the options works fine.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Convert Update statement into Insert statement in UNIX using awk, sed....

Hi folks, I have a scenario to convert the update statements into insert statements using shell script (awk, sed...) or in database using regex. I have a bunch of update statements with all columns in a file which I need to convert into insert statements. UPDATE TABLE_A SET COL1=1 WHERE... (0 Replies)
Discussion started by: dev123
0 Replies

2. Shell Programming and Scripting

Help about comparison

Hello folks, I have two files, which have usernames, I want to see the contents of file1.txt which is missing in file2.txt and another comparison file2.txt contents which is missing in file1.txt. please suggest. file1.txt user u2 u8 a9 p9 p3 u4 z8 aaa ahe oktlo (7 Replies)
Discussion started by: learnbash
7 Replies

3. UNIX for Dummies Questions & Answers

comparison

hi guys i need a program that can compare a value read from a com-port and one from the terminal. can somebody help me??? using linux kernel 2.6.14-M5 can only use standard function in sh and bash... (5 Replies)
Discussion started by: metal005
5 Replies

4. Shell Programming and Scripting

$((...)) and $[...] comparison

Does $((mathematical expression)) and $ mean the same? (7 Replies)
Discussion started by: proactiveaditya
7 Replies

5. Shell Programming and Scripting

String comparison in if statement

Hi Just trying to compare the following variables as a condition in an 'if' statement. ZERODATA="unidentified 0 b (0%)" DATA=`sed -n "${dln} p" mpck2.out` ... if then ... The problem I'm getting is that the condition always comes out true and doesn't appear to compare them... (5 Replies)
Discussion started by: javathecat
5 Replies

6. Shell Programming and Scripting

How is use sselect statement o/p in insert statement.

Hi All, I am using Unix ksh script. I need to insert values to a table using the o/p from a slelect statement. Can anybody Help! My script looks like tihs. ---`sqlplus -s username/password@SID << EOF set heading off set feedback off set pages 0 insert into ${TB_NAME}_D... (2 Replies)
Discussion started by: nkosaraju
2 Replies

7. Shell Programming and Scripting

If statement - How to write a null statement

In my ksh script, if the conditions of a if statement are true, then do nothing; otherwise, execute some commands. How do I write the "do nothing" statement in the following example? Example: if (( "$x"="1" && "$y"="a" && "$z"="happy" )) then do nothing else command command fi... (3 Replies)
Discussion started by: april
3 Replies

8. Shell Programming and Scripting

need some help..Comparison

I need some help which would probably be for most of you a simple script. I need to read in the data from a .dat file and then compare avg to see who is the highest avg. Here is my script so far. #!/bin/ksh #reading in the data from lab3.dat filename=$1 while read name o1 o2 o3 o4 o5 o6... (0 Replies)
Discussion started by: bluesilo
0 Replies

9. Shell Programming and Scripting

Wildcard comparison

Just a quick question: if I want to do a comparison with a wildcard in a shell script, do i just use '*'? Heres what I have: elif ; then continue but that doesnt evaluate right. It tries to compare against the literal '/apps*' instead of anything that begins with '/apps' (2 Replies)
Discussion started by: rdudejr
2 Replies

10. Filesystems, Disks and Memory

comparison

can anyone point me to a comparison of *nix file systems ? i think i prefer a journalling fs but i would like to see a comparison between several fs's before i make up my mind (2 Replies)
Discussion started by: cnf
2 Replies
Login or Register to Ask a Question