UNIX Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsOperating SystemsUNIX Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old October 15th, 2012, 06:13 AM
dileep_d10 dileep_d10 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2012
Posts: 3 dileep_d10 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 25 m 22 sec
Reputation Power: 0
CSV file Columns check and delete

Hi All,

i have a CSV file like below,

col1 col2 col3 col4 col5 col6 col7
a1 a2 a3 a4 a5 a6 a7
b1 b2 b3 b4 b5 b6 b7
c1 c2 c3 c4 c5 c6 c7
d1 d2 d3 d4 d5 d6 d7

Col1,col2.. are the column names and a1,a2,b1,b2.. are the data of the file.

i need to write a shell script, for here i need a check like which ever the unnecessary columns from the CSVfile have to be deleted, and mandatory columns sholud be specified in the scripts itself.

can any body help me on this for writing the shell script.

thanks in advance.

Reply With Quote
  #2  
Old October 15th, 2012, 03:15 PM
spacebar208's Avatar
spacebar208 spacebar208 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2012
Location: spaceBAR Central
Posts: 191 spacebar208 User rank is Sergeant Major (2000 - 5000 Reputation Level)spacebar208 User rank is Sergeant Major (2000 - 5000 Reputation Level)spacebar208 User rank is Sergeant Major (2000 - 5000 Reputation Level)spacebar208 User rank is Sergeant Major (2000 - 5000 Reputation Level)spacebar208 User rank is Sergeant Major (2000 - 5000 Reputation Level)spacebar208 User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 2 Days 9 h 55 m 30 sec
Reputation Power: 41
If your file is TAB delimited, This is an example of deleting the 2nd column
Code:
$ cat t2
col1    col2    col3    col4    col5    col6    col7
a1      a2      a3      a4      a5      a6      a7
b1      b2      b3      b4      b5      b6      b7
c1      c2      c3      c4      c5      c6      c7
d1      d2      d3      d4      d5      d6      d7

$ sed 's/^\([0-9a-zA-Z]*\t\)[0-9a-zA-Z]*\t/\1/' t2
col1    col3    col4    col5    col6    col7
a1      a3      a4      a5      a6      a7
b1      b3      b4      b5      b6      b7
c1      c3      c4      c5      c6      c7
d1      d3      d4      d5      d6      d7


If your file is COMMA delimited, This is an example of deleting the 2nd column
Code:
$ cat t2
col1,col2,col3,col4,col5,col6,col7
a1,a2,a3,a4,a5,a6,a7
b1,b2,b3,b4,b5,b6,b7
c1,c2,c3,c4,c5,c6,c7
d1,d2,d3,d4,d5,d6,d7

$ sed 's/^\([0-9a-zA-Z]*,\)[0-9a-zA-Z]*,/\1/' t2
col1,col3,col4,col5,col6,col7
a1,a3,a4,a5,a6,a7
b1,b3,b4,b5,b6,b7
c1,c3,c4,c5,c6,c7
d1,d3,d4,d5,d6,d7

Reply With Quote
  #3  
Old October 16th, 2012, 02:11 AM
dileep_d10 dileep_d10 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2012
Posts: 3 dileep_d10 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 25 m 22 sec
Reputation Power: 0
Actually i need it like a shell script. which are the necessary columns i have to specify in the script itself, based on that remaining columns should need to delete.

Reply With Quote
  #4  
Old October 16th, 2012, 03:29 AM
salem's Avatar
salem salem is offline
Contributed User
Click here for more information
 
Join Date: Jun 2005
Posts: 3,839 salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)  Folding Points: 153 Folding Title: Novice Folder
Time spent in forums: 2 Months 3 Weeks 2 Days 19 h 1 m 4 sec
Reputation Power: 1774
Perhaps the cut tool is more appropriate.
Code:
$ cat file1.txt
col1,col2,col3,col4,col5,col6,col7
a1,a2,a3,a4,a5,a6,a7
b1,b2,b3,b4,b5,b6,b7
c1,c2,c3,c4,c5,c6,c7
d1,d2,d3,d4,d5,d6,d7
$ cut -d',' -f2,4,6 file1.txt
col2,col4,col6
a2,a4,a6
b2,b4,b6
c2,c4,c6
d2,d4,d6
$ cut --complement -d',' -f2,4,6 file1.txt
col1,col3,col5,col7
a1,a3,a5,a7
b1,b3,b5,b7
c1,c3,c5,c7
d1,d3,d5,d7
__________________
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper

Reply With Quote
  #5  
Old October 16th, 2012, 09:29 AM
dileep_d10 dileep_d10 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2012
Posts: 3 dileep_d10 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 25 m 22 sec
Reputation Power: 0
No i have taken an array like

ArrayName=("a1" "a3" "a4" "a5" "a6" "a7" "a8")

these are the fiels names in CSV file, and along with these field name there are so many number of Field Names in CSV file. now i want to delete the Fields which are not present in the ArrayName.

can any one help me on this for writing a shell scripts.

Thanks in advance.

Reply With Quote
  #6  
Old October 16th, 2012, 11:58 AM
salem's Avatar
salem salem is offline
Contributed User
Click here for more information
 
Join Date: Jun 2005
Posts: 3,839 salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)  Folding Points: 153 Folding Title: Novice Folder
Time spent in forums: 2 Months 3 Weeks 2 Days 19 h 1 m 4 sec
Reputation Power: 1774
> No i have taken an array like
> ArrayName=("a1" "a3" "a4" "a5" "a6" "a7" "a8")
Wait a minute - a1 etc were fields in the CSV, and col1 etc were column names in post #1.

Reply With Quote
  #7  
Old October 18th, 2012, 08:04 AM
SimonJM SimonJM is offline
Contributing User
Dev Shed Regular (2000 - 2499 posts)
 
Join Date: Mar 2006
Posts: 2,108 SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 1 Week 1 Day 4 h 41 m
Reputation Power: 1485
If you could get that array into a plain text/string then:

Code:
echo \("a1" "a3" "a4" "a5" "a6" "a7" "a8"\) | tr -d "[:alpha:]|[:punct:]" | tr -s " " ","

Would give you the column numbers, comma-delimited suitable for use in a cut -f command as mentioned by salem, above.
__________________
The moon on the one hand, the dawn on the other:
The moon is my sister, the dawn is my brother.
The moon on my left and the dawn on my right.
My brother, good morning: my sister, good night.
-- Hilaire Belloc

Reply With Quote
Reply

Viewing: Dev Shed ForumsOperating SystemsUNIX Help > CSV file Columns check and delete

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap