Help answer threads with 0 replies.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
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.


  Search this Thread
Old 04-08-2005, 11:20 AM   #1
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:
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:
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:
system ("grep $TimeStr $FileA $var");
but that doesn't seem to work.
I also know that:
$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.

Old 04-08-2005, 01:25 PM   #2
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);

or to find

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

hope this helps..

Old 04-08-2005, 02:06 PM   #3
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

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

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.



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 07:47 AM
c++ parse date string?? blizunt7 Programming 25 05-18-2006 03:12 PM
Parse RPM version string in Bash jimwelc Linux - Newbie 1 02-28-2005 05:22 PM
Parse a perl string djgerbavore Programming 3 10-31-2004 07:23 AM
C++ and C# Parse a string into a numerical type exodist Programming 8 02-23-2004 06:15 AM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:20 PM.

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