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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need help on how to append on the filename when condition met.
# 1  
Old 04-14-2016
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.

Code:
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

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

Last edited by znesotomayor; 04-14-2016 at 03:45 AM..
# 2  
Old 04-14-2016
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?
# 3  
Old 04-14-2016
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,
# 4  
Old 04-14-2016
What does "append on the file" mean?
This User Gave Thanks to RudiC For This Post:
# 5  
Old 04-14-2016
append meaning add. for ex.
Code:
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,
# 6  
Old 04-14-2016
Please post your solution for the benefit of others looking for inspirational hints for their problems.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

awk - print when condition is met

I have a file.txt containing the following: Query= HWI-ST863:386:C5Y8UACXX:3:2302:16454:89688 1:N:0:ACACGAAT Length=100 Score E Sequences producing significant alignments: (Bits) Value ... (2 Replies)
Discussion started by: tons92
2 Replies

2. Shell Programming and Scripting

Add another condition to bash for when not met

In the below I can not seem to add a line that will add Not low if the statement in bold is not true or meet. I guess when the first if statement is true/meet then print low, otherwise print Not low in $(NF + 1). I am not sure how to correctly add this. Thank you :). if(low <= $2 && $2 <=... (5 Replies)
Discussion started by: cmccabe
5 Replies

3. Shell Programming and Scripting

Getting the records once condition met

Hi All, Seeking for your assistance to get the records once the $2 met the condition. Ex. file 1.txt 123455,10-Aug-2020 07:33:37 AM,2335235,1323534,12343 123232,11-Aug-2015 08:33:37 PM,4234324,1321432,34364 Output: 123455,10-Aug-2020 07:33:37 AM,2335235,1323534,12343 What i did... (5 Replies)
Discussion started by: znesotomayor
5 Replies

4. Shell Programming and Scripting

Print specific field when condition met

Hi All, Seeking for your assistance to print all the specific field when the condition met. Ex: file1.txt 1|203|3|31243|5341|6452|623|22|00|01 3|45345|123214|6534|3423|6565|643|343|232|10 if field 1 = 1 and field 3 = 3 and field 5 = 5341 and field 6 = 6452 it will print from $1 to $10.... (2 Replies)
Discussion started by: znesotomayor
2 Replies

5. Shell Programming and Scripting

Awk. Abort script if condition was met.

I want to abort script if input variable matched first field in any line of a file. #!/bin/sh read INPUTVAR1 awk "{if(\$INPUTVAR1 == $1) x = 1} END {if(x==1) print \"I want to abort script here\"; else print \"OK\"}" /etc/some.conf I tried "exit" and system("exit") but no luck. (1 Reply)
Discussion started by: urello
1 Replies

6. Shell Programming and Scripting

Delete if condition met in a column

i have a table like this: id, senderNumber, blacklist ----------------------------- 1 0835636326 Y 2 0373562343 Y 3 0273646833 Y and I want to delete automatically if a new inserted row on another table consist anything on senderNumber column above using a BASH Script I... (9 Replies)
Discussion started by: jazzyzha
9 Replies

7. UNIX for Advanced & Expert Users

While loop only if a condition is met

All, I wrote the following section of code (which logically in PHP would of worked): tmpPATH=${1} tmpTAG=${2} if then while read tmpTAG tmpPATH do fi echo $tmpTAG echo $tmpPATH if then done < ./config.cfg fi (4 Replies)
Discussion started by: Cranie
4 Replies

8. Shell Programming and Scripting

do nothing if condition is not met but not exit

Hello all, I created the below script....and it seemed to be working fine. My problem is i want the script to ignore rest of the things if my condition is not met but do not exit.... #!/bin/ksh ########################### ########################### # Set name of the listener, this... (2 Replies)
Discussion started by: abdul.irfan2
2 Replies

9. Shell Programming and Scripting

How to break a loop if condition is met

I am having trouble figuring this code I want to grep a text from a file and if it match certain text it break out of the loop or it should continue searching for the text Here is what I have written but it isn't working while true f=`grep 'END OF STATUS REPORT' filename` do if ... (9 Replies)
Discussion started by: Issemael
9 Replies

10. Shell Programming and Scripting

Send email to user when condition met

Hi all, I plan to write a shell script to inform users on their task when certain condition met. example: If a then email user on action a else email user on action b. I'm pretty new in scripting, appreciate any suggestions. Thanks. (4 Replies)
Discussion started by: *Jess*
4 Replies
Login or Register to Ask a Question