LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   retrieve data column wise from csv files (https://www.linuxquestions.org/questions/linux-newbie-8/retrieve-data-column-wise-from-csv-files-4175523865/)

srinureddy 10-31-2014 01:14 AM

retrieve data column wise from csv files
 
Hi.. i am new to the Linux.i need help.
i want to retrieve data column wise from csv files in shell scripting.pl z help me.
my csv file like this

"101","a","fs"
"102","b","ss"
"103","svs","fs,sd"
"104","ssvsd","ssSdvs"Sdvs"

thanks in advance.

eklavya 10-31-2014 01:57 AM

If every column is separated by comma (,), retrieve first column using
Code:

awk -F',' '{print $1}' /path/of/file.csv
Retrieve second column using
Code:

awk -F',' '{print $2}' /path/of/file.csv
and so on...

srinureddy 10-31-2014 02:08 AM

Thank u eklavya..
 
thank u.. but i want to display all columns data in the file. and in my file every column enclosed with double quotes and in that data we have a comma but i want to treat it same column. my file contain above 1 lack records.

eklavya 10-31-2014 02:23 AM

If every column is separated by a double quote and a comma (",)
To retrieve first column
Code:

awk -F"\"," '{print substr($1,2); }' file.csv
To retrieve second column
Code:

awk -F"\"," '{print substr($2,2); }' file.csv
and so on...
To retrieve last column
Code:

awk -F"\"," '{print substr($NF, 2, length($NF)-2)}' file.csv

AnanthaP 10-31-2014 02:34 AM

He wants to ignore commas between double quotes (third line of the sample) and doube quotes between double quotes (fourht line of the sample) for the purpose of field delimiters.

OK

srinureddy 10-31-2014 03:14 AM

Thank u eklavya and AnanthaP
 
for example my file like this.

"101","ddd","sss"
"102","ssa","ee"
"103","dsd,ds","dsd"
"104","ds"cz","dsda"

i want print like this
"101"
"ddd"
"sss"
"102"
"ssa"
"ee"
"103"
"dsd,ds"
"dsd"
"104"
"ds"cz"
"dsda"
pl z tell me the any command or script to display above way..

eklavya 10-31-2014 03:24 AM

Code:

awk -F"\"," '{for (i=1;i<=NF;i++) print $i"\""}' file.csv

grail 10-31-2014 03:26 AM

Yes this can be done with awk. Have a look for FPAT on the manual page

srinureddy 10-31-2014 03:41 AM

Thank u guys..
 
now i have another task..
it is
my file like this

"101","dsds","Dasds"
"102","dsdv","dsd,dsd"
"103","dsdfv"ds","dfsadf"
"104","dsds","dfgasd"Fvs"

i want modify my file like this

"101","dsds","Dasds"
"102","dsdv","dsd,dsd"
"103","dsdfv""ds","dfsadf"
"104","dsds","dfgasd""Fvs"


means in data where we have double quotes, we want to replace with 2 double quotes.

grail 10-31-2014 03:53 AM

And where is your attempt?

srinureddy 10-31-2014 03:57 AM

hi grail.
 
sample data is

"101","sds"dsd","dsf" i want modify like this "101","sds""dsd","dsf"

in my file i have number of columns like this i want to modify all the columns.

srinureddy 10-31-2014 04:06 AM

Thank u eklavya..
 
i am using this command:
awk -F"\"," '{for (i=1;i<=NF;i++) print $i"\""}' file.csv

but it will ignore my last column data of every row.

grail 10-31-2014 06:40 AM

Firstly, please use [code][/code] tags when displaying code or data

Thank you for the sample data. Now where is your attempt to solve the problem?

Others have given some suggestions, although I see the last example fails although I do not understand what you feel the error is.
Using your sample data and either a script of your own or an adjustment to those presented, how have you tried to solve your new issue?

rtmistler 10-31-2014 08:21 AM

Quote:

Originally Posted by srinureddy (Post 5262261)
Hi.. i am new to the Linux.i need help.
i want to retrieve data column wise from csv files in shell scripting.pl z help me.
my csv file like this

"101","a","fs"
"102","b","ss"
"103","svs","fs,sd"
"104","ssvsd","ssSdvs"Sdvs"

thanks in advance.

You realize that eventually this:

http://www.linuxquestions.org/questi...2/#post5262328
http://www.linuxquestions.org/questi...5/#post5262304
http://www.linuxquestions.org/questi...5/#post5262261

is going to make things not work out so well insofar as answers from the forum ...

John VV 10-31-2014 10:18 PM

see your other post on this topic
http://www.linuxquestions.org/questi...63#post5262763


All times are GMT -5. The time now is 03:58 PM.