Need help on how to append on the filename when condition met.

Hi All,

Seeking for your assistance on how to append the specific string when $3 condion met.

ex. file1.txt
ar0050046b16,5,888,0,0,0,0.00,0.00,0.00,0.00,25689.55
ar0050046b16,5,0,0,0,0,0.00,0.00,0.00,0.00,25689.55
ar0050046b16,5,0,0,0,0,0.00,0.00,0.00,0.00,25689.55

expected output: file1.txt_Area_by_1


file2.txt
ar0050046b16,5,888,0,0,0,0.00,0.00,0.00,0.00,25689.55
ar0050046b16,5,0,0,0,0,0.00,0.00,0.00,0.00,25689.55
ar0050046b16,5,0,0,0,0,0.00,0.00,0.00,0.00,25689.55
ar0050046b16,5,100,0,0,0,0.00,0.00,0.00,0.00,25689.55

expected output file2.txt_Area_by_2 since $3 has the records of 100

Condition: If $3 contains 888 or 0 it will append the filename into file1.txt_Area_by_1
else if $3 not contains to 888 or 0 then the filename will append file2.txt_Area_by_2

What i did was i use for loop:

#!/bin/sh

for i in `ls *.txt`
do
DEPT=`awk -F "," '{print $3}' $i`
echo "${DEPT}"
if [ "${DEPT}" == "888" ] || [ "${DEPT}" == "0" ]
then
flag=0
else
flag=1
fi

if [ $flag == "0" ]
then
echo "Area_by_DEPT"
else
echo "Area_by_CLASS"
fi

done

but the problem it's not working the way i want.

Please advise,

Thanks,

Not clear. WHAT does not work? What is the action to be done if the condition is met/not met?

Aside: If using awk to extract the third field (btw, above extracts ALL third fields into the variable), why not do the entire thing in awk ?

Hi Sir RudiC,

i just need to find if there's a 888 or 0 in $3. if yes the Area_by_1 will append on the file
if none Area_by_2 will append on the file.

Thanks,

What does "append on the file" mean?

1 Like

append meaning add. for ex.

from: file1.txt
To: file1.txt_Area_by_class

I already solve my problem. i'm now closing this thread. thx for the help.

BR,

Please post your solution for the benefit of others looking for inspirational hints for their problems.