Condition problem

Hi All,

Seeking for your assistance on how to condition it correctly.

cat file1.txt
290,1663,43,888,0,0.00,86.91,0.00,26.98,0.00
290,1663,52,0,0,0.00,0.00,0.00,0.00,0.00
290,1663,52,888,0,0.00,34.60,0.00,9.00,0.00
1st scenario:
if the fourth column contains 888s and 0s it is by DEPT and "DEPT" will appended into the end of file.
here's what i did and got successfully condition

for i in `ls *.txt`; do
ONE=${i}
flag="1"
while read line
do
DEPT=`echo $line | awk -F "," '{print $3}'`
SDEPT=`echo $line | awk -F "," '{print $4}'`
if [ "$SDEPT" == 888 ] || [ "$SDEPT" == 0 ]; then
flag="1"
else
flag="0"
break
fi
done <$i
if [ $flag == "1" ]
then
TWO="Area_by_DEPT"
else
TWO="Area_by_CLASS"
fi
uploadfile=`echo ${ONE} | awk -F'.' '{print $1}'`
sed -i -e "s/^/$DT_NOW_YYYYMMDD,${uploadfile},${TWO},/" ${ONE}
mv ${ONE} ${ONE}_${TWO}
done

My output is: file1.txt_Area_by_DEPT
i got the correct output.
cat file1.txt
290,1663,76,5,0,0.00,86.91,0.00,26.98,0.00
290,1663,52,2,0,0.00,0.00,0.00,0.00,0.00
290,1663,52,999,0,0.00,34.60,0.00,9.00,0.00
290,1663,16,705,0,0.00,34.60,0.00,9.00,0.00
2nd scenario:(including the 1st condition)
if the fourth column does not contains 888s and 0s it is by CLASS and "CLASS" will appended into the end of file.

here's what i've done.

for i in `ls *.txt`; do
ONE=${i}
flag="1"
while read line
do
DEPT=`echo $line | awk -F "," '{print $3}'`
SDEPT=`echo $line | awk -F "," '{print $4}'`
if [ "$SDEPT" != 888 ] || [ "$SDEPT" != 0 ]; then
flag="1"
else
flag="0"
break
fi
done <$i
if [ $flag == "1" ]
then
TWO="Area_by_DEPT"
else
TWO="Area_by_CLASS"
fi
uploadfile=`echo ${ONE} | awk -F'.' '{print $1}'`
sed -i -e "s/^/$DT_NOW_YYYYMMDD,${uploadfile},${TWO},/" ${ONE}
mv ${ONE} ${ONE}_${TWO}
done

My output: file1.txt_Area_by_CLASS
i got the successfull output because of the else statement 
(else
flag="0"
break
fi
0
cat file1.txt
290,1663,76,888,0,0.00,86.91,0.00,26.98,0.00
290,1663,55,0,0,0.00,0.00,0.00,0.00,0.00
290,1663,52,888,0,0.00,34.60,0.00,9.00,0.00
290,1663,16,888,0,0.00,34.60,0.00,9.00,0.00
290,1663,1,888,0,0.00,34.60,0.00,9.00,0.00
3rd scenario: the condition 1, 2 will be included on this 3rd condition. here i'm stuck i don't know what to do.

if the 3rd column
contains 55, 16, 76 and the fourth column is equal to 888 and 0 it's by CLASS and "CLASS" will appended into the end of file.

Expected output will be file1.txt_Area_by_CLASS
Summary Condition:
if the 4th column = 888 or 0 then it's by DEPT
if the 4th column not equal 888 or 0 then it's by CLASS
if the 3rd column contains 55, 16, 76 and 4th column = 888 or 0 then it's the same as by CLASS
if the 3rd column contains 55, 16, 76 and 4th column not equal 888 or 0 then it's the same as by CLASS

Hope you understand.

Need your expertise guys.

Please advise,

Sorry, WHERE are you stuck?

And, are cond #1 and #3 contradictory or should the line be appended to two files?

I'm stuck on how to put all that condition. all my testing is wrong. my output is always file1.class although the 4th column is only 888 and 0. it's contradictory

Please take a step back and rephrase the logics/conditions. And, an input sample containing ALL cases plus an indication what should go where might be of invaluable help - for you as well.

Done on rephrasing Sir RudiC. please let me know if you have question. my head hurt because of this. i don't know what to do T_T

---------- Post updated at 12:26 PM ---------- Previous update was at 10:00 AM ----------

following this up guys.. need your expertise :slight_smile:

TIA

Please DON'T edit a post that has been reacted/commented on, pulling the rug from under someone else's feet!

So - the one single condition would be

($4 == 888 || $4 == 0) && !($3 == 55 || $3 == 16 || $3 == 76) ---> DEPT
otherwise CLASS

?

Apologies Sir Rudic. How do i run it on if statement? i'm encountering command not found.

#!/bin/sh

for i in `ls *.txt`; do
while read line
do
DEPT=`echo $line | awk -F "," '{print $3}'`
SDEPT=`echo $line | awk -F "," '{print $4}'`
if ($SDEPT == 888 || $SDEPT == 0) && !($DEPT == 55 || $DEPT == 16 || $DEPT == 76); then
echo dept
else
class
fi
done <$i
done
output
test.sh: line 8: 888: command not found
test.sh: line 8: 888: command not found
test.sh: line 11: class: command not found
test.sh: line 8: 0: command not found
test.sh: line 8: 0: command not found
test.sh: line 11: class: command not found
test.sh: line 8: 888: command not found
test.sh: line 8: 888: command not found
test.sh: line 11: class: command not found

Wheres the input sample requested?

In shell syntax you'll have to use square brackets like if [ $SDEPT == ... ]

here's my initial input sample
file1.txt
290,1663,43,888,0,0.00,86.91,0.00,26.98,0.00
290,1663,52,0,0,0.00,0.00,0.00,0.00,0.00
290,1663,52,888,0,0.00,34.60,0.00,9.00,0.00
290,1663,52,9,0,0.00,34.60,0.00,9.00,0.00

i tried it using awk -F "," '($4 == 888 || $4 == 0) && !($3 == 55 || $3 == 16 || $3 == 76)' file1.txt >> DEPT

how do i put it on if statement?

TIA

Agglomerating all your samples into on file, I got

awk -F, '
($4 == 888 || $4 == 0) && !($3 == 55 || $3 == 16 || $3 == 76)   {print "DEPT ", $3, $4, $0; next}
        {print "CLASS", $3, $4, $0}
' OFS="\t"  file
DEPT     43    888    290,1663,43,888,0,0.00,86.91,0.00,26.98,0.00
DEPT     52    0    290,1663,52,0,0,0.00,0.00,0.00,0.00,0.00
DEPT     52    888    290,1663,52,888,0,0.00,34.60,0.00,9.00,0.00
CLASS    76    5    290,1663,76,5,0,0.00,86.91,0.00,26.98,0.00
CLASS    52    2    290,1663,52,2,0,0.00,0.00,0.00,0.00,0.00
CLASS    52    999    290,1663,52,999,0,0.00,34.60,0.00,9.00,0.00
CLASS    16    705    290,1663,16,705,0,0.00,34.60,0.00,9.00,0.00
CLASS    76    888    290,1663,76,888,0,0.00,86.91,0.00,26.98,0.00
CLASS    55    0    290,1663,55,0,0,0.00,0.00,0.00,0.00,0.00
DEPT     52    888    290,1663,52,888,0,0.00,34.60,0.00,9.00,0.00
CLASS    16    888    290,1663,16,888,0,0.00,34.60,0.00,9.00,0.00
DEPT     1    888    290,1663,1,888,0,0.00,34.60,0.00,9.00,0.00

Would this result come close to what you need?

if there's a CLASS found on output how to change the filename from file1.txt to file1.txt_AREA_BY_CLASS?

TIA

---------- Post updated at 09:57 PM ---------- Previous update was at 09:50 PM ----------

i think it's covered Sir RudiC. the problem is how can i code it :slight_smile:

TIA

Try

awk -F, '
($4 == 888 || $4 == 0) && !($3 == 55 || $3 == 16 || $3 == 76)   {print > (FN "DEPT"); next}
                                                                {print > (FN "CLASS")}
' FN="file.txt_Area_by_"  file

Hi Sir RudiC,

the output should be 1 only per file. On every file, if there's a record contains class it will automatically change the filename into file1.txt_area_by_class. if there's no class on the record it's file1.txt_area_by_dept.

TIA

Maybe this example helps use to make comparing little easier in shell. You can add breaks as you like.

for file in *.txt
do
    stat="DEPTH"
    while IFS="," read f1 f2 DEPT SDEPT rest
    do
        flag="DEPTH"
        ## between ((   and ))   you can use syntax same as like in C or ...
        ((   ( DEPT != 888 )  && ( DEPT !=0 )   ))  && flag="C1"    # && break # if like to break
       # if the 3rd column contains 55, 16, 76 and 4th column = 888 or 0 then it's the same as by CLASS
       # so write rule as it has been written:
         ((  ( DEPT == 55 || DEPT == 16 || DEPT == 76 ) && ( SDEPT == 888 || SDEPT == 0 )  )) && flag="C2"
       # if the 3rd column contains 55, 16, 76 and 4th column not equal 888 or 0 then it's the same as by CLASS
       # so write rule as it has been written:
         ((  ( DEPT == 55 || DEPT == 16 || DEPT == 76 ) && ( SDEPT != 888 && SDEPT != 0 )  )) && flag="C3"
        echo "      $flag - $f1 $f2 d:$DEPT sd:$SDEPT $rest"
        [ "$flag" != "DEPTH" ] && stat="CLASS"
    done < $file
    echo "$file - $stat"
done

(( ( DEPT != 888 ) && ( DEPT !=0 ) )) && flag="CLASS"
is same as

if ((   ( DEPT != 888 )  && ( DEPT !=0)   ))  
then
   flag="CLASS"
fi
1 Like

hi kshji,

how about the 3 condition? 55,76 and 15?

In reaction to a recent PM:

Sorry but I'm stuck.

I'm afraid I can't help any further unless you give a clear picture of the overall problem. Appreciating that English might not be your first language (it's not mine either) and all the possible problems herewith, I do not understand what you need and where you're stuck.

In my post#4 I asked you to rephrase the problem AND supply decent, meaningful samples, which you did reluctantly only, and in pieces. Why can't you show the entire picture and the underlying structures?

  • OS and shell used?
  • What tools would be preferred (shell? awk/sed/perl? other?) or excluded?
  • How many input files (you supplied several but different "file1.txt")?
  • How many output files per input file?
  • What's the logics/algorithms connecting the two?
  • Attempts at solving the problem?
  • Errors/messages/behaviour encountered when running either attempt?

Without a clear understanding of the situation, everybody (not only) in here will be shooting in the dark (which I did multiple times), and coming up with a fit solution would be sheer coincidence.

Did you even consider adapting the proposals supplied?