Help in parsing a CSV file with Shell script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help in parsing a CSV file with Shell script
# 1  
Old 06-24-2007
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  
Old 06-24-2007
Code:
echo '079661/3' | awk -F'/' '{for(i=0; i<$2; i++) print $1+i}'

# 3  
Old 06-24-2007
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  
Old 06-24-2007
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  
Old 06-24-2007
thanks...

Thanks....u've been a gr8 help...
# 6  
Old 06-24-2007
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  
Old 06-24-2007
Tools

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

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help with Shell Scrip in Masking particular columns in .csv file or .txt file using shell script

Hello Unix Shell Script Experts, I have a script that would mask the columns in .csv file or .txt file. First the script will untar the .zip files from Archive folder and processes into work folder and finally pushes the masked .csv files into Feed folder. Two parameters are passed ... (5 Replies)
Discussion started by: Mahesh G
5 Replies

2. Shell Programming and Scripting

Help with Parsing a CSV File

Hello All, I have an input CSV file like below, where first row data can be in different position after every run of the tool, i.e. pzTest in below example is in column 1, but it can be also in 3 column and same for all the headers in the first row. pzTest, pzExtract, pxUpdate, pzInfo... (1 Reply)
Discussion started by: asirohi
1 Replies

3. Shell Programming and Scripting

Shell script not parsing complete file using AWK

Hi, I have shell script which will read single edi document and break data between ST & SE to separate files.Below example should create 3 separate files. I have written script with the below command and it is working fine for smaller files. awk -F\| -vt=`date +%m%d%y%H%M%S%s` \ ... (2 Replies)
Discussion started by: prasadm
2 Replies

4. Shell Programming and Scripting

Help required in parsing a csv file

Hi Members, I am stuck with the following problem. Request your kind help I have an csv file which contains, 1 header record, data records and 1 footer record. Sample is as below Contents of cm_update_file_101010.csv -------------------------------------------------- ... (6 Replies)
Discussion started by: ramakanth_burra
6 Replies

5. Shell Programming and Scripting

Parsing a CSV File

Hey guys, I'm in the process of learning PHP and BASH scripting. I'm getting there, slowly ;) I would like some help with parsing a CSV file. This file contains a list of hostnames, dates, and either Valid, Expired, or Expired Soon in the last column. Basically, I want to parse the file,... (12 Replies)
Discussion started by: dzl
12 Replies

6. Shell Programming and Scripting

2 problems: Mailing CSV file / parsing CSV for display

I have been trying to find a good solution for this seemingly simple task for 2 days, and I'm giving up and posting a thread. I hope someone can help me out! I'm on HPUX, using sqlplus, mailx, awk, have some other tools available, but can't install stuff that isn't already in place (without a... (6 Replies)
Discussion started by: soldstatic
6 Replies

7. Shell Programming and Scripting

CSV file parsing and validation

I have a CSV file that needs to through two seperate processes (in the end there will be 2 files (Dload.unl and Tload.unl and we'll say the input file name is mass.csv). I have a processfile() function that will call the process Dload funtion. In Dload I want to read mass.csv into Dload and then... (1 Reply)
Discussion started by: dolo21taf
1 Replies

8. Shell Programming and Scripting

Parsing a csv file

I am trying to parse a csv file in the below 'name-value pair' format and then use the values corresponding to the name. Type:G,Instance:instance1,FunctionalID:funcid,Env:dev,AppName:appname... (6 Replies)
Discussion started by: chiru_h
6 Replies

9. Shell Programming and Scripting

Shell script for parsing 300mb log file..

am relatively new to Shell scripting. I have written a script for parsing a big file. The logic is: Apart from lot of other useless stuffs, there are many occurances of <abc> and corresponding </abc> tags. (All of them are properly closed) My requirement is to find a particular tag (say... (3 Replies)
Discussion started by: gurpreet470
3 Replies

10. Shell Programming and Scripting

Parsing a file in Shell Script

Hi, I have a requirement. I have an application which can take a file as inputs. Now the file can contain any number of lines. The tool has to pick up the first uncommented line and begin processing it. For example the file could be like this: #MANI123|MANI1234 #MANI234|MANI247... (4 Replies)
Discussion started by: sendhilmani123
4 Replies
Login or Register to Ask a Question