How to extract data from csv file

Hello everybody,

Here is my problem, I don't know anything about shell programming and my boss is actually asking me to develop a shell script in order to get values in a csv file from a specific date.

Here is a sample of the csv file :

Date;Encha�nement;Titre;Libell� ;calendrier;Heure d�but;Heure fin;Dur�e estim�e;Dur�e r�elle;"Dur�e r�-�valu�e ou 
informations compl�mentaires";Contraintes, d�pendances et criticit�s;

and below the values :

01/12/2010;;;;;;;;;;;
;bp_101;"IE001 (4 UT)
BP_10101 � BP_10104"; INTERFACE ENTRANTE : TOTO => GAGA;SPF;;;;;;"bg00<nn><zz>.bp
sur bonne ou mauvaise fin de bp_102 (FORMP)";
;bp_209;"IE017 (3 UT)
BP_20901 � BP_20903";INTERFACE ENTRANTE :TOTO => GAGA;SPF;;;;;;"gesiaaaa.bp 
Exclusif";

02/12/2010;;;;;;;;;;
;bp_101;"IE001 (4 UT)
BP_10101 � BP_10104"; TOTO : GOGO => GAGA;SPF;;;;;;"bg00<nn><zz>.bp
sur bonne ou mauvaise fin de bp_102 (FORMP)"
;bp_209;"IE017 (3 UT)
BP_20901 � BP_20903";TOTO => GAGA;SPF;;;;;;"gesiaaaa.bp 
Exclusif"

As you can see each date corresponds to various actions

Would you know a script that would ask for a date and then parse the csv file in order to grab the different actions refering to this date ?

Thanx a lot for your help...

Something like this?

awk 'BEGIN{printf("Enter the date : "); getline date < "-"}
$0 ~ date {f=1}
f
/Exclusif/{f=0}
' file

Thanks a lot for your help. Now I have a script which is asking for a specific date.

But, the problem is that it reads only 2 lines (???) whereas I have many for a specific date :

01/12/2010;;;;;;;;;;;
;Action_1;; Comment;Comment;;;;;;"File Comment";
;Action_2;; Comment;Comment;;;;;;"Exclusif";
;Action_3;; Comment;Comment;;;;;;"Not exclusif";
;Action_4;; Comment;Comment;;;;;;"Another File Comment";
;Action_5;; Comment;Comment;;;;;;"File Comment";
;Action_6;; Comment;Comment;;;;;;"File Comment";

How to do to make it print every kind of action for a specific date ?

Thanx a lot for your help

awk 'BEGIN{printf("Enter the date : "); getline date < "-"} $0 ~ date{f=1}!NF{f=0}f' file
1 Like

Perhaps somrhing like this?

awk 'BEGIN{printf("Enter the date : "); getline date < "-"}
$0 ~ date {f=1}
f
/^[0-9]{2}\//{f=0}
' file

@Franklin52 & @danmero : I greatly appreciate your help

Now everything seems to be more or less OK but How can I redirect the output to a file ?

Thanx again

awk '..' infile > outfile

Thanks a lot for all your help.

I currently use cygwin and it seems that I cannot redirect anything to an external file (???) the script is blocked and doesn't ask for a date.

Anyway again thank you very much

---------- Post updated 11-24-10 at 02:46 AM ---------- Previous update was 11-23-10 at 11:28 AM ----------

Hello again,

Well I imported the script on a HP-UX environment and it's not0 executing the way it should :

#!/bin/sh
awk 'BEGIN{printf("Entrez la date : "); getline date < "-"}
$0 ~ date {f=1}
f
/^[0-9]{2}\//{f=0}
f' toto.csv

Results in :

$ ./extract.sh
Entrez la date : 02/12/2010
02/12/2010;;;;;;;;;;

=> and that's all

It doesn't give me the actions for a date :

02/12/2010;;;;;;;;;;;
;Action_1;; Comment;Comment;;;;;;"File Comment";
;Action_2;; Comment;Comment;;;;;;"Exclusif";
;Action_3;; Comment;Comment;;;;;;"Not exclusif";
;Action_4;; Comment;Comment;;;;;;"Another File Comment";
;Action_5;; Comment;Comment;;;;;;"File Comment";
;Action_6;; Comment;Comment;;;;;;"File Comment";

Help please

Try this:

awk 'BEGIN{printf("Enter the date : "); getline date < "-"}
$0 ~ date {f=1;print;next}
/^[0-9]{2}\//{f=0}
f
' toto.csv

Again thank you.

Now I have to find a way (by myself) to get everything a little bit cleaner because the output is not very sexy.

Can you tell me what is the signification of all this :

/^[0-9]{2}\//{f=0}
/^[0-9]{2}\//{f=0}

If the first 2 characters of the line are numeric and the third character is a slash (the next date) assign 0 to the variable f.


  1. 0-9 ↩︎

1 Like

Thanks a lot for all your help :slight_smile: :slight_smile: :slight_smile: :slight_smile: :slight_smile: :slight_smile: :slight_smile: