LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 10-31-2014, 02:14 AM   #1
srinureddy
LQ Newbie
 
Registered: Oct 2014
Posts: 11

Rep: Reputation: Disabled
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.
 
Old 10-31-2014, 02:57 AM   #2
eklavya
Member
 
Registered: Mar 2013
Posts: 619

Rep: Reputation: 136Reputation: 136
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...
 
Old 10-31-2014, 03:08 AM   #3
srinureddy
LQ Newbie
 
Registered: Oct 2014
Posts: 11

Original Poster
Rep: Reputation: Disabled
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.
 
Old 10-31-2014, 03:23 AM   #4
eklavya
Member
 
Registered: Mar 2013
Posts: 619

Rep: Reputation: 136Reputation: 136
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

Last edited by eklavya; 10-31-2014 at 03:55 AM.
 
Old 10-31-2014, 03:34 AM   #5
AnanthaP
Member
 
Registered: Jul 2004
Location: Chennai, India
Distribution: UBUNTU 5.10 since Jul-18,2006 on Intel 820 DC
Posts: 813

Rep: Reputation: 195Reputation: 195
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
 
Old 10-31-2014, 04:14 AM   #6
srinureddy
LQ Newbie
 
Registered: Oct 2014
Posts: 11

Original Poster
Rep: Reputation: Disabled
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..
 
Old 10-31-2014, 04:24 AM   #7
eklavya
Member
 
Registered: Mar 2013
Posts: 619

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

Last edited by eklavya; 10-31-2014 at 04:54 AM.
 
1 members found this post helpful.
Old 10-31-2014, 04:26 AM   #8
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,424

Rep: Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823
Yes this can be done with awk. Have a look for FPAT on the manual page
 
Old 10-31-2014, 04:41 AM   #9
srinureddy
LQ Newbie
 
Registered: Oct 2014
Posts: 11

Original Poster
Rep: Reputation: Disabled
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.
 
Old 10-31-2014, 04:53 AM   #10
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,424

Rep: Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823
And where is your attempt?
 
Old 10-31-2014, 04:57 AM   #11
srinureddy
LQ Newbie
 
Registered: Oct 2014
Posts: 11

Original Poster
Rep: Reputation: Disabled
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.
 
Old 10-31-2014, 05:06 AM   #12
srinureddy
LQ Newbie
 
Registered: Oct 2014
Posts: 11

Original Poster
Rep: Reputation: Disabled
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.
 
Old 10-31-2014, 07:40 AM   #13
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,424

Rep: Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823
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?
 
Old 10-31-2014, 09:21 AM   #14
rtmistler
Moderator
 
Registered: Mar 2011
Location: Sutton, MA. USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu
Posts: 4,484
Blog Entries: 10

Rep: Reputation: 1643Reputation: 1643Reputation: 1643Reputation: 1643Reputation: 1643Reputation: 1643Reputation: 1643Reputation: 1643Reputation: 1643Reputation: 1643Reputation: 1643
Quote:
Originally Posted by srinureddy View Post
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 ...
 
Old 10-31-2014, 11:18 PM   #15
John VV
LQ Muse
 
Registered: Aug 2005
Location: A2 area Mi.
Posts: 16,988

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


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] changing row data files into column data files tabbygirl1990 Linux - Newbie 18 01-29-2014 10:33 AM
[SOLVED] HELP! How to merge two files as column wise?? SAAAD Linux - Newbie 9 03-24-2013 02:31 AM
How to read CSV data and compare the column values and then write them in new file VijayaRaghavanLakshman Linux - Newbie 9 01-26-2012 10:02 PM
awk - rearrange column data in csv file to match columns wolverene13 Programming 9 12-21-2011 05:55 AM
printing column wise bharatbsharma Programming 4 09-11-2008 12:00 PM


All times are GMT -5. The time now is 10:52 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration