Domanda

I'm extremely new to Unix and have been looking into this issue for days.

I have the below set of information in a csv file that I need to manipulate through Unix.

BEIGE ,   19.50 ,   STYLE ,   05/05/14
BEIGE ,   19.50 ,   STYLE ,   05/06/14
BEIGE ,   19.50 ,   STYLE ,   05/07/14

Using an awk command like below I am successfully grabbing the first field of the first line only;

l_colour=$(awk 'BEGIN{FS=","} END {print $1}' file)

I need to store each field as a variable (l_colour, l_price, l_type, l_in_date) on line one, import into a database then do the same for line two and so on.

Is it possible for me to use FS and RS in the same awk statement?

Please go easy on me.

Thanks,

È stato utile?

Soluzione

I know this is not exactly answer to PO's question but inserting data from cvs to database could be done by something like this:

cat file | tr -s ' ' | sed 's/^\(.*\)$/INSERT INTO table(l_colour, l_price, l_type, l_in_date) VALUES(\1);/'

output :

INSERT INTO table(l_colour, l_price, l_type, l_in_date) VALUES(BEIGE , 19.50 , STYLE , 05/05/14);
INSERT INTO table(l_colour, l_price, l_type, l_in_date) VALUES(BEIGE , 19.50 , STYLE , 05/06/14);
INSERT INTO table(l_colour, l_price, l_type, l_in_date) VALUES(BEIGE , 19.50 , STYLE , 05/07/14);

Altri suggerimenti

It's not clear what you're hoping to get out of using awk for part of this. For the input you posted you can just do:

$ while read -r l_colour c l_price c l_type c l_in_date
do
    echo "now insert \"$l_colour\", \"$l_price\", \"$l_type\", \"$l_in_date\""
done < file
now insert "BEIGE", "19.50", "STYLE", "05/05/14"
now insert "BEIGE", "19.50", "STYLE", "05/06/14"
now insert "BEIGE", "19.50", "STYLE", "05/07/14"

Change the echo line to whatever your database insertion command is.

If there's more to this question than you've told us so far, google how to store awk output in a bash array.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top