LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices



Reply
 
Search this Thread
Old 04-08-2005, 12:20 PM   #1
DaFrEQ
Member
 
Registered: Mar 2002
Location: Earth... for now
Distribution: SuSE9.2 AMD64; LFS; GentooAMD64; Ubuntu10.04; RHEL 5.5; Solaris10(SPARC)
Posts: 418

Rep: Reputation: 30
Parse --static string no delimiters


Hi all,

I've been working on a small program, and am trying to find the best/simplest way to parse a text file. I originally started this project in C, but moved to Perl/Tk as it's much smaller and swifter for what I'm trying to do.
However, what I need to do is parse a text file. The file has a record for every second of the hour, and no delimiters.
The string is:
04298030002+0003809-0017050+0061182-012005910400.5310401.03
The format is as follows:
XXX = JulianDay; XX = Year; XXXXXX = Time; +XXXXXXX = value#1; -XXXXXXX = value#2; +XXXXXXX = value#3; -XXXXXXX = value#4; XXXXX.XX = Value#5; XXXXX.XX = value#6
(the + and - change depending on the value being recorded, but the format is still the same thruout the file)

Now, since there are no delimiters (spaces/commas/colons...) I'm stumped as to how I should go about reading this line, and splitting it up in its proper format for output.
Before it's asked, no I cannot change the text file or the program that outputs the text file.

Basically, what I'm trying to do is have the user input a time: XXXXXX and allow the program to parse thru the file to find that specific time and capture the entire line.
Then it needs to take that same line (or string) and format it for proper output to STDOUT or a file. (whichever)
I cannot seem to find anything that remotely resembles a clear-cut easy way to search/find specific values in a text file in C or in Perl.
So I tried using the system function to call grep which is an amasingly accurate search tool. Matter of fact, grep does exactly what I'm trying to do in my program, which is why I'm trying to use it.

I initially tried:
Code:
system ("grep $TimeStr $FileA >>$FileOut");
which works, to print the exact string into a file, but I'm wondering if there is a way to have that same line of code print it to a $var instead of an output file.
I tried:
Code:
system ("grep $TimeStr $FileA $var");
but that doesn't seem to work.
I also know that:
Code:
$var = substr ($input_str, 0, 3);
would work for part of what I'm trying to do, but I need to first get the grep call to throw the line into a $var instead of a file.

Any ideas/thoughts are welcomed.
Also, I'm trying to limit the amount of external libs to use, to keep this as simple and compact as possible.

Tnx.
L8rz
 
Old 04-08-2005, 02:25 PM   #2
joecool118
LQ Newbie
 
Registered: Apr 2005
Posts: 4

Rep: Reputation: 0
NOTE: The below is based upon the following being true..
-- The format (including length is constant), always padding to a certain length.
-- You are using PERL..

$str = "04298030002+0003809-0017050+0061182-012005910400.5310401.03";

You can use substring to parse the info
NOTE: substr($str,0,2)

-$str is the value

-0 is starting value

-2 is how many things to snatch after the start
NOTE: Starts @ 0 not 1
--to parse
$julday = substr($str, 0, 3);
$year = substr($str, 3, 2);
$time = substr($str, 6, 6);
$val1 = substr($str, 11, 8);
etc...

or to find

if($str =~ /$pattern/)
{
print "FOUND: PAT: \"$pattern\" in STR: \"$str\"\n";
}

hope this helps..

Joe
 
Old 04-08-2005, 03:06 PM   #3
DaFrEQ
Member
 
Registered: Mar 2002
Location: Earth... for now
Distribution: SuSE9.2 AMD64; LFS; GentooAMD64; Ubuntu10.04; RHEL 5.5; Solaris10(SPARC)
Posts: 418

Original Poster
Rep: Reputation: 30
joecool118,

Tnx for the reply.

However, $str = "04298030002+0003809-0017050+0061182-012005910400.5310401.03"; will not work. At least I don't think so because the string will always be different.
The length of the entire string will always be the same, yes, but the actual values change by the second. Which is why they are recorded every second the to file I'm trying to parse.
Unless I'm mis-understanding what you are saying here:
Quote:
NOTE: The below is based upon the following being true..
-- The format (including length is constant), always padding to a certain length.
-- You are using PERL..

$str = "04298030002+0003809-0017050+0061182-012005910400.5310401.03";
Now, as far as the pattern matching goes, I have attempted it, but pattern matching doesn't work nearly as well as the grep command. For example, I'll give you a few lines from my file:
Code:
04298030001+0024592-0022500+0097345-004190010400.5310401.03

04298030002+0003809-0017050+0061182-012005910400.5310401.03
04298030003-0016145-0025500-0228662-006317010400.5310401.03
04298030004-0042996-0021450-0150229+005638610400.5310401.03
04298030005-0037184+0033600+0122455+008297210400.5310401.03
04298030006+0025032+0073650+0145788+004349710400.5310401.02
04298030007+0071996+0011450+0056462+002426510400.5310401.02
04298030008+0031692-0072600-0099351-001668710400.5310401.02
04298030009-0039390-0052200+0036634-007422810400.5310401.02
04298030010-0052154+0012700-0062741-004358010400.5310401.02
04298030011-0024616+0036100-0163092+003050910400.5310401.02
04298030012+0018888+0046550+0072192+003138410400.5310401.02
04298030013+0048280+0007250+0092348+000429310400.5310401.02
04298030014+0027643-0039450+0104219-001030310400.5310401.02
04298030015-0022192-0047500+0010739+000185710400.5310401.02
04298030016-0036889+0016850-0135474+000568710400.5310401.02
04298030017+0004272+0047900-0036726+003752110400.5310401.02
04298030018+0030828+0002100+0056763+001913310400.5310401.02
04298030019+0003293-0042150-0015630-006192310400.5310401.02
04298030020-0029315-0018200-0048827-004461910400.5310401.02
04298030021-0020226+0031250+0064181-000573110400.5310401.02
04298030022+0023923+0044000+0049582+001945310400.5310401.02
04298030023+0042041-0007650-0015256+002779110400.5310401.02
04298030024+0006357-0052850-0064053+003376210400.5310401.02
As you can see, the length is always the same, but so are most of the numbers, eventho the values themselves are different. (e.g. Pattern matching on a time stamp of 030014 will return every single line because they all consist of 03) I hope that all made sense
At any rate, this is why I excluded pattern matching alltogether because a majority of the file has similar #s that will not equal to my exact search string.

Tnx
L8rz
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Parse String in a Bash script jimwelc Linux - Newbie 8 11-09-2012 08:47 AM
c++ parse date string?? blizunt7 Programming 25 05-18-2006 04:12 PM
Parse RPM version string in Bash jimwelc Linux - Newbie 1 02-28-2005 06:22 PM
Parse a perl string djgerbavore Programming 3 10-31-2004 08:23 AM
C++ and C# Parse a string into a numerical type exodist Programming 8 02-23-2004 07:15 AM


All times are GMT -5. The time now is 01:36 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration