The UNIX and Linux Forums  


Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 08-17-2007
anypager anypager is offline
Registered User
  
 

Join Date: Jul 2005
Posts: 10
awk or cut

select some fields from data file (source.csv)
The data in file(source.csv) is like
"x1,2",,"y",,"z"

How to get the 1st, 2nd and 3rd field from the file. Using awk or cut?

Note: "x1,2" is one field.

thanks,
  #2 (permalink)  
Old 08-17-2007
ranj@chn ranj@chn is offline Forum Advisor  
Playing with Ubuntu Now!
  
 

Join Date: Oct 2005
Location: Chennai
Posts: 365
awk's better

Code:
echo "x1,2",,"y",,"z" | awk -F',,' '{print $1,$2,$3}'
  #3 (permalink)  
Old 08-17-2007
rexf rexf is offline
Registered User
  
 

Join Date: Aug 2007
Location: 127.0.0.1
Posts: 13
Note: "x1,2" is one field.

Thus your solution is incorrect.
  #4 (permalink)  
Old 08-17-2007
namishtiwari namishtiwari is offline Forum Advisor  
Registered User
  
 

Join Date: Aug 2007
Location: Bangalore
Posts: 377
Thumbs up

Quote:
Originally Posted by ranj@chn View Post
Code:
echo "x1,2",,"y",,"z" | awk -F',,' '{print $1,$2,$3}'
This is only going to print x1,2 not the desired output.
I tried this with cut also but the delimeter ,, its not taking here.
  #5 (permalink)  
Old 08-17-2007
robotronic's Avatar
robotronic robotronic is offline Forum Advisor  
Can I play with madness?
  
 

Join Date: Apr 2002
Location: Italy
Posts: 370
ranj's solution is correct, you get an incorrect result maybe because of your awk version. Try using "nawk" on solaris or "gawk":

Code:
echo "\"x1,2\",,\"y\",,\"z\"" | nawk -F',,' '{print $1,$2,$3}'

Output:
"x1,2" "y" "z"
  #6 (permalink)  
Old 08-17-2007
dennis.jacob dennis.jacob is offline Forum Advisor  
dj - the student
  
 

Join Date: Feb 2007
Location: Singapore/Bangalore/Cochin
Posts: 607
If "x1,2",,"y",,"z" is the kind of format of all records in csv file and ",," is considered as delim, the below one should work.


Code:
sed 's/\(.*\),,\(.*\),,\(.*\)/\1 \2 \3/' file
  #7 (permalink)  
Old 08-19-2007
anypager anypager is offline
Registered User
  
 

Join Date: Jul 2005
Posts: 10
"x1,2",,"y",,"z" is just an example of the format. what if "x1,2",,"y",,,"z",,,?
Closed Thread

Bookmarks

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 07:22 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0