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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
11-15-2007, 08:54 PM
|
#1
|
|
LQ Newbie
Registered: Mar 2006
Location: Phoenix, Arizona
Distribution: Mandriva 2007
Posts: 8
Rep:
|
Bash read in variable length text records
I need to be able to read in variable length text records on like a daily basis with out loosing track I know to use date and a file to store what record im on because the program will execute every day and need to process the next record in the file but how to determine what record? the field between records is going to be an extra line so what I have is a long text file with several pages worth of text and I have an additional line of white space separator so as to delineate records I would use while but the time frame for record processing needs to be a day and the variable record length creates a problem? Thanks in advance.
Last edited by lynx81191; 11-15-2007 at 08:55 PM.
|
|
|
|
11-15-2007, 10:10 PM
|
#2
|
|
Guru
Registered: Aug 2004
Location: Brisbane
Distribution: Centos 6.4, Centos 5.9
Posts: 15,021
|
It's a little unclear what you are asking for, especially as you have no punctuation.
IUIC, each data rec is in fact 2 recs, one with data, one empty.
All you need to do is count which rec you have reached, and as you have noted, save that value for next time in a temp file.
Use cron to run the prog each day, which then reads the temp file and uses the rec cnt therein to read to that rec in the data file and extract the next data rec, incrementing the rec cnt and saving it.
HTH
|
|
|
|
11-15-2007, 11:06 PM
|
#3
|
|
LQ Newbie
Registered: Mar 2006
Location: Phoenix, Arizona
Distribution: Mandriva 2007
Posts: 8
Original Poster
Rep:
|
clarification
I apologize. Let me clarify. I have a 14 page long text flle spaced with an extra flle between records. The records consist of words of text separated by spaces. I need to pull records one at a time 1 per day and the only field delimiter I have to use is an extra blank line. I can write bash scripts but I have no idea how to process the records using the field delimiter that I have to work with. I've looked into regular expressions but am unsure what command to use and the exact structure of the regular expression that I would need. If use a 'for' loop with 'cut' I cannot use a multiple character field delimiter (there are spaces between the words in the records). And I can't use various other commands because of this so I am looking to sed and awk but have no knowledge of how to use a regular expression to accomplish this.
|
|
|
|
11-15-2007, 11:31 PM
|
#4
|
|
LQ Newbie
Registered: Mar 2006
Location: Phoenix, Arizona
Distribution: Mandriva 2007
Posts: 8
Original Poster
Rep:
|
Quote:
Originally Posted by chrism01
It's a little unclear what you are asking for, especially as you have no punctuation.
IUIC, each data rec is in fact 2 recs, one with data, one empty.
All you need to do is count which rec you have reached, and as you have noted, save that value for next time in a temp file.
Use cron to run the prog each day, which then reads the temp file and uses the rec cnt therein to read to that rec in the data file and extract the next data rec, incrementing the rec cnt and saving it.
HTH
|
#!/bin/bash
while read text
do
cat text | mail -s record user@domain.com
done < data
will not work because of delimiting fields ?
I apologize. Let me clarify. I have a 14 page long text file spaced with an extra blank line between records. The records consist of words of text separated by spaces. I need to pull records one at a time 1 per day and the only field delimiter I have to use is an extra blank line. I can write bash scripts but I have no idea how to process the records using the field delimiter that I have to work with. I've looked into regular expressions but am unsure what command to use and the exact structure of the regular expression that I would need. If use a 'for' loop with 'cut' I cannot use a multiple character field delimiter (there are spaces between the words in the records). And I can't use various other commands because of this so I am looking to sed and awk but have no knowledge of how to use a regular expression to accomplish this.
Last edited by lynx81191; 11-15-2007 at 11:40 PM.
|
|
|
|
11-17-2007, 08:53 PM
|
#5
|
|
Guru
Registered: Aug 2004
Location: Brisbane
Distribution: Centos 6.4, Centos 5.9
Posts: 15,021
|
Here's the basics
Code:
file=yourfile
#you want eg rec 3 from yourfile - see my previous
num1=3
#Get rec
rec=`cat $file|head -$num1|tail -1`
#awk will use/accept any amt of whitespace
field=`echo $rec|awk '{print $3}'`
echo $field
|
|
|
|
| Thread Tools |
Search 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
HTML code is Off
|
|
|
All times are GMT -5. The time now is 03:10 AM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|