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.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
parsing a string in a shell script asutoshch Shell Programming and Scripting 19 05-26-2008 10:18 PM
shell script parsing with sed jjamd64 UNIX for Dummies Questions & Answers 5 12-11-2007 04:51 PM
Parsing a line in Shell Script unishiva Shell Programming and Scripting 3 11-01-2007 04:30 PM
Shell script for parsing 300mb log file.. gurpreet470 Shell Programming and Scripting 3 02-09-2007 04:38 AM
Parsing a file in Shell Script sendhilmani123 Shell Programming and Scripting 4 11-30-2006 02:29 AM

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 06-24-2007
mihirk mihirk is offline
Registered User
  
 

Join Date: Jun 2007
Posts: 5
Help in parsing a CSV file with Shell script

I have a CSV file which contains number series as one of the fields. Some of the records of the field look like :

079661/3

I have to convert the above series as
079661
079662
079663
and store it as 3 different records.

Looking for help on how to achieve this. Am a newbie at Shell scripting.

Thanking all in advance for the help..

Mihir
  #2 (permalink)  
Old 06-24-2007
vgersh99's Avatar
vgersh99 vgersh99 is offline Forum Staff  
Moderator
  
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,131

Code:
echo '079661/3' | awk -F'/' '{for(i=0; i<$2; i++) print $1+i}'

  #3 (permalink)  
Old 06-24-2007
mihirk mihirk is offline
Registered User
  
 

Join Date: Jun 2007
Posts: 5
thanks..but it ignores '0' in the beginning

echo '079661/3' | awk -F'/' '{for(i=0; i<$2; i++) print $1+i}'

79661
79662
79663

Since this is a phone number series, I don't want the 0's in the beginning to be ignored.

Thanks a lot for your response.

Mihir
  #4 (permalink)  
Old 06-24-2007
vgersh99's Avatar
vgersh99 vgersh99 is offline Forum Staff  
Moderator
  
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,131

Code:
echo '079661/3' | awk -F'/' '{for(i=0; i<$2; i++) { match($1, /^[^0]*/); print substr($1, 1, RSTART) substr($1, RSTART)+i}}'

  #5 (permalink)  
Old 06-24-2007
mihirk mihirk is offline
Registered User
  
 

Join Date: Jun 2007
Posts: 5
thanks...

Thanks....u've been a gr8 help...
  #6 (permalink)  
Old 06-24-2007
vgersh99's Avatar
vgersh99 vgersh99 is offline Forum Staff  
Moderator
  
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,131
a fix in regex:

Code:
echo '079661/3' | nawk -F'/' '{for(i=0; i<$2; i++) { match($1, "^[0]*[0]*[^0]"); print substr($1, RSTART, RLENGTH-1) substr($1, RSTART+RLEGTH)+i}}'

  #7 (permalink)  
Old 06-24-2007
RishiPahuja's Avatar
RishiPahuja RishiPahuja is offline
Registered User
  
 

Join Date: Apr 2005
Location: Bangalore, India
Posts: 203
Cool

Try something like


Code:
while read line
do
#assuming the field is column X in each record with total N records
precolumns=`echo $line | cut -d',' f1,2,3,4,....X-1`
postcolumns=`echo $line | cut -d','  fX+1,X+2.....N`
echo $line | cut -d',' -fX | awk -F'/' '{for(i=0; i<$2; i++) print $precolumns "," $1+i  "," $postcolumns }' > $newfile
done < $filename

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 04:33 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