473,406 Members | 2,867 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Help in parsing the CSV file

3
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
Jun 24 '07 #1
5 2475
arne
315 Expert 100+
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

What does the syntax x/y mean?

- the (y-1) next higher numbers, e.g.
123,4
123
124
125
126

- the next numbers until the numbers end with the same digit
123,4
123
124

- can y have multiple digits, like

123,30
123
124
125
...
129
130
?

You'll have to describe the problem a little bitmore detailed in order to get accurate solutions, I guess.

And: this must be written in bash or can you use other tools like perl?

arne
Jun 24 '07 #2
mihirk
3
Hi arne,

Following is the clarification sought by you:-

x/y means:

the next numbers until the numbers end with the same digit
123,4
123
124

Also there will not be multiple digits. Even perl can be used.

Thanks,
Mihir
Jun 24 '07 #3
arne
315 Expert 100+
Hi arne,

Following is the clarification sought by you:-

x/y means:

the next numbers until the numbers end with the same digit
123,4
123
124

Also there will not be multiple digits. Even perl can be used.

Thanks,
Mihir
This should do more or less what you want:

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2.  
  3. if (2 != ($#ARGV+1)) {
  4.  
  5.     print "Usage: $0 <infile> <outfile>\n";
  6.     exit 1;
  7. }
  8.  
  9. open INFILE,  "<$ARGV[0]" || die "unable to open INFILE";
  10. open OUTFILE, ">$ARGV[1]" || die "unable to open OUTFILE";
  11.  
  12. while (<INFILE>) {
  13.  
  14.     if (/(.*)(\d)\/(\d)/) {
  15.  
  16.     my $stub  = $1;
  17.     my $start = $2;
  18.     my $end   = $3;
  19.  
  20.     for $i ($start..$end) {
  21.  
  22.         print OUTFILE "$stub$i\n";
  23.     }
  24.  
  25.     } else {
  26.  
  27.       print OUTFILE "$_";
  28.     }
  29. }
  30.  
  31. close INFILE;
  32. close OUTFILE;
  33.  
HTH,
arne
Jun 24 '07 #4
mihirk
3
Thanks a lot...the script does more or less what I want....

But am a newbie to perl...Can you please explain what

(/(.*)(\d)\/(\d)/ does and how are you seperating the string into 3 variables by using the regular expression??

Thanks again..
Mihir
Jun 25 '07 #5
arne
315 Expert 100+
Thanks a lot...the script does more or less what I want....

But am a newbie to perl...Can you please explain what

(/(.*)(\d)\/(\d)/ does and how are you seperating the string into 3 variables by using the regular expression??

Thanks again..
Mihir
The regex is enclosed by forward slashes, '.' is an arbitrary symbol, '*' means an arbitrary number, so we have an arbitrary number of arbitrary symbols in the first pair of brackets. \d is a digit, the \ is an escape character for the /, which is taken as a symbol (and not the end of the regex!), then we have another digit. The brackets group the single elements and can be referred to by $n, where n denotes the number of the bracket pair. So, if a line contains 2 digits separated by a / and preceeded by something else, we expand it.

HTH,
arne
Jun 26 '07 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: burn_hall | last post by:
Hi, I have a problem and can't figure it out and need your help, please look at the following code and the output also a xml file snippet is down there too. Looking at the output I don't know why...
2
by: Cigdem | last post by:
Hello, I am trying to parse the XML files that the user selects(XML files are on anoher OS400 system called "wkdis3"). But i am permenantly getting that error: Directory0: \\wkdis3\ROOT\home...
6
by: wukexin | last post by:
Help me, good men. I find mang books that introduce bit "mang header files",they talk too bit,in fact it is my too fool, I don't learn it, I have do a test program, but I have no correct doing...
4
by: ralphNOSPAM | last post by:
Is there a function or otherwise some way to pull out the target text within an XML tag? For example, in the XML tag below, I want to pull out 'CALIFORNIA'. ...
1
by: sommarlov | last post by:
Hi everyone >From one of our systems an xml file is produced. I need to validate this file before we send it to an external system for a very lenghty process. I cannot change the xml file layout....
1
by: syhzaidi | last post by:
How can we do Parsing of Hexdecimel in C# reading string from stream file for eg.. i have a file like.......... 0f 2f 12 2d 3a.......in hexa decimal save in a file.txt and i m reading it from...
9
by: Paulers | last post by:
Hello, I have a log file that contains many multi-line messages. What is the best approach to take for extracting data out of each message and populating object properties to be stored in an...
0
by: savj14 | last post by:
I have been driving myself crazy the past few days trying to figure this out. I have tried different Parsing Scripts and have read and searched various things trying to find a solution. I am...
0
by: Ahmed, Shakir | last post by:
Thanks everyone who tried to help me to parse incoming email from an exchange server: Now, I am getting following error; I am not sure where I am doing wrong. I appreciate any help how to resolve...
6
by: Damien87 | last post by:
Hi, I have a .data file which is essentially just a text file with a bunch of numbers. It's in the format "xx yy zzz abc ccf fffd sss xxx xx sss qq" Basically it is a random amount of numbers...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.