Click here to Skip to main content
15,880,891 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm trying to create 3 csv files based on the contract type. The Month_to_month.csv has gotten created but why aren't year 1 and 2 working? Please help!

What I have tried:

Shell
%%!
contract_month=0
contract_1year=0
contract_2year=0

for line in $(cat data_v3.csv)
do
IFS=','
read customerID gender PhoneService InternetService StreamingTV Contract TotalCharges Churn <<< $line
        if [ "$Contract" = "Month-to-month" ]
        then
          echo ${line}>>Month_to_month.csv          
            
        elif [[ "$Contract" = "One year" ]]
        then
          echo ${line}>>One_year.csv
            
            
        elif  [ "$Contract" = "Two year" ]
        then
            echo ${line}>>Two_year.csv
        fi
        
done

cat Two_year.csv
cat One_year.csv
cat Month_to_month.csv


The output it shows is:
['cat: Two_year.csv: No such file or directory',
 'cat: One_year.csv: No such file or directory',
 '7590-VHVEG,Female,No,DSL,No,Month-to-month,29.85,No',
 '3668-QPYBK,Male,Yes,DSL,No,Month-to-month,108.15,Yes',
 '6713-OKOMC,Female,No,DSL,No,Month-to-month,301.9,No',
 '9763-GRSKD,Male,Yes,DSL,No,Month-to-month,587.45,No',
 '4190-MFLUW,Female,Yes,DSL,No,Month-to-month,528.35,Yes',
 '8779-QRDMV,Male,No,DSL,No,Month-to-month,39.65,Yes',
 '6322-HRPFA,Male,Yes,DSL,No,Month-to-month,2970.3,No',
 '6865-JZNKO,Female,Yes,DSL,No,Month-to-month,1530.6,No',
 '8665-UTDHZ,Male,No,DSL,No,Month-to-month,30.2,Yes',
 '8773-HHUOZ,Female,Yes,DSL,Yes,Month-to-month,1093.1,Yes',
 '3413-BMNZE,Male,Yes,DSL,No,Month-to-month,45.25,No',
 '4080-IIARD,Female,Yes,DSL,Yes,Month-to-month,981.45,No',
 '5948-UJZLF,Male,Yes,DSL,No,Month-to-month,....


the
Posted
Updated 8-Feb-23 22:46pm
v2
Comments
Member 15627495 9-Feb-23 1:49am    
if [ "$Contract" = "Month-to-month" ]
        then
          echo ${line}>>Month_to_month.csv          
        fi
  
        if [[ "$Contract" = "One year" ]]
        then
          echo ${line}>>One_year.csv
            
        fi
    
        if  [ "$Contract" = "Two year" ]
        then
            echo ${line}>>Two_year.csv
        fi



for a working result, stay as simple as possible.

nested 'if elif elif' are hard to write.

I just tried this on my Linux system, and the tests for "One year" and "Two year" fail, because the read command splits them into two fields. I have tried putting them in quotes, but it still splits them at the space character. I guess you have to add some code to re-read the data when $Contract equals "One" or "Two". Adding some echo commands after the read I get the following output:
1 ,  Month-to-month
2 ,  Month-to-month
3 ,  "One
year" ,  250
4 ,  Month-to-month
5 ,  Month-to-month

So it clearly reads the space in "One year" as a line terminator.

[EDIT]
Further testing reveals that the for statement that gets the lines of text is removing the commas separating them. So when the read command splits the line it only has space characters to separate the words.

I think you may need to use a programming language (e.g. Python) to process this data, rather than shell commands.
[/EDIT]
 
Share this answer
 
v2
Comments
Neha Thachil 9-Feb-23 10:17am    
Thanks Richard, that was actually really helpful but the surprising thing is that in the previous question I had to use the data file "data_v3.csv", to find the number of Female vs male in each contract type and it seemed to work and interpret both the other contract types.

See below:

%%!
IFS=$'\n'
contract_month_female=0
contract_month_male=0
contract_1year_female=0
contract_1year_male=0
contract_2year_female=0
contract_2year_male=0

for line in $(cat data_v3.csv)
do
IFS=','
read customerID gender PhoneService InternetService StreamingTV Contract TotalCharges Churn <<< $line
if [[ "$Contract" = "Month-to-month" ]]
then
if [ "$gender" = "Female" ]
then

contract_month_female=$((contract_month_female+1))

else
contract_month_male=$((contract_month_male+1))
fi

elif [[ "$Contract" = "One year" ]]
then
if [ "$gender" = "Female" ]
then

contract_1year_female=$((contract_1year_female+1))

else
contract_1year_male=$((contract_1year_male+1))
fi

elif [ "$Contract" = "Two year" ]
then
if [ "$gender" = "Female" ]
then

contract_2year_female=$((contract_2year_female+1))

else
contract_2year_male=$((contract_2year_male+1))
fi
fi
done

echo $contract_month_female
echo $contract_month_male
echo $contract_1year_female
echo $contract_1year_male
echo $contract_2year_female
echo $contract_2year_male

This was the output which was correct when I checked on excel too:

['1925', '1950', '718', '755', '845', '850']

So I wonder why it's not working here
Neha Thachil 9-Feb-23 10:22am    
I just realized it was because I was missing the "IFS=$'\n'" in the beginning that's why it wasn't working! I couldn't have figured it out without your explanation about the read command so thanks again!!
Richard MacCutchan 9-Feb-23 10:29am    
I just tried that on my sample, and it appears to work now. Well spotted by you. Also, it is a good idea to add some debugging statements in your code so that you can actually ss the data that is being processed as the code runs.
Richard MacCutchan 9-Feb-23 10:25am    
I do not think it did work, especially as you have not shown the contents of the input file, and that output does not provide any meaningful information.
You are using shell to reference the path to the file and the name of the file. The error states that either the path or the file name OR both is incorrect.

Looks like you are using Python, see Find path to the given file using Python[^] with 3 methods -

1. Using Path.cwd()
from pathlib import Path
print(Path.cwd())


2. Using os.getcwd()
import os
print('Get current working directory : ', os.getcwd())


3. Using pathlib.Path().absolute()
import pathlib
 
# current working directory
print(pathlib.Path().absolute())


4. Using os.path.basename[^]

Notes - You can get the file name and the directory name of the running file in the below way. The key thing to understand about __file__ is that the interpreter adjusts it at runtime so that Python knows which file it is dealing with when the script uses several modules. The benefit of calling Path( file__) is that it returns a string containing the path and the file you are currently working on.
import os
 
print('File name :    ', os.path.basename(__file__))
print('Directory Name:     ', os.path.dirname(__file__))


Output of this -
File name: Two_year.csv
Directory Name: C:\path\to\your\file


See the above link for more examples.
 
Share this answer
 
Comments
Richard MacCutchan 9-Feb-23 4:16am    
There is no Python in that question.
Andre Oosthuizen 9-Feb-23 12:17pm    
Before edit, there were a heading at the top that said Python?? My bad, I will delete.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900