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.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
|
|
11-17-2004, 04:34 PM
|
#1
|
LQ Newbie
Registered: Nov 2004
Posts: 13
Rep:
|
Getting info from text file
Warning, REAL newb here!
I have a script that, as of now, reads in a .txt file, takes all the lines (all the lines are 8 characters long always), writes and chmods a new, blank, .txt file with the same name and passes this info on to another script. It works perfectly.
I now want to alter this script so that it takes only the first line of the .txt file, deletes that line from the file, writes the file back (minus that one line of course) and passes that one line on to my other script.
I am doing this so multiple computers can access and process the lines in order without affecting the rest of the lines in the .txt file and without processing any line multiple times. Please excuse the dirty(?) coding of the script . . . I am brand new:
(The "sUpdate.que" is the said .txt file used)
while read shotIn; do
shots[${#shots[@]}]=$shotIn
rm /Volumes/Projects01/ShellScripts/sUpdate.que
touch /Volumes/Projects01/ShellScripts/sUpdate.que
chmod 777 /Volumes/Projects01/ShellScripts/sUpdate.que
done < /Volumes/Projects01/ShellScripts/sUpdate.que
for shot in "${shots[@]}" ; do
echo $shot
/Volumes/Projects01/ShellScripts/shakeRender $shot
echo `date` "$shot" >> /Volumes/Projects01/ShellScripts/sUpdate.log
done
Thanks for any help!
alts
|
|
|
11-17-2004, 05:10 PM
|
#2
|
Moderator
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
|
Moved: This thread is more suitable in "Programming", and has been moved accordingly to help your thread/question get the exposure it deserves.
|
|
|
11-17-2004, 08:00 PM
|
#3
|
Member
Registered: Sep 2004
Location: south texas
Distribution: fedora core 3,4; gentoo
Posts: 192
Rep:
|
well, i dont know the language your using very well, im assuming bash. but a quick fix would be to just keep track of what line your reading and writing by having some index variable start at zero and increase everytime you read a line. then you can check with an if statment to see if you need to write that line.
i = 1
while shot in; do
{
if($i != 1)
{ write to your file}
$i++
}
sorry for the poor syntax, im not sure what it will look like, but you get the idea
|
|
|
11-17-2004, 08:09 PM
|
#4
|
LQ Newbie
Registered: Nov 2004
Posts: 13
Original Poster
Rep:
|
It is bash, sorry for not stating earlier.
Thanks for the reply. Because the amount of lines in this file may vary greatly, isn't it easier, and cleaner, to just remove the the line and always have script read line 1? Otherwise the script has to check what lines have been read and find the next one that has not been read. With the method I am thinking of, it's also easier to spot where problematic lines are as they will be at the top of the file if the script fails. Again, I could be wrong about the best way to go about this but that's why I am here. I've been pouring over books and I just can't get the syntax right. I'll buy beers if someone can help me! Thanks again!
alts
|
|
|
11-18-2004, 08:49 AM
|
#5
|
Member
Registered: Sep 2004
Location: south texas
Distribution: fedora core 3,4; gentoo
Posts: 192
Rep:
|
oh, im sorry, i misunderstood your problem. you want the script to just take the first line of the text file, and remove that line so that if another script opens the file, it wont have to worry about weather or not that line has been processed. is that about it? your right, my solution is not very efficient for that.
well, you could easily read the first line of the file into an variable using the head command.
VARIABLE = `head -n 1 filename.txt`
but im not sure how to just remove the first line without actually reading and writing each line in the file. ill think about it
|
|
|
11-18-2004, 09:12 AM
|
#6
|
Member
Registered: May 2004
Location: UK
Distribution: Gentoo
Posts: 293
Rep:
|
You can use
Code:
tail -n +2 file.txt > temp.txt; mv temp.txt file.txt;
to replace the file with one that has the first line removed.
|
|
|
11-18-2004, 09:34 AM
|
#7
|
Member
Registered: Sep 2004
Location: south texas
Distribution: fedora core 3,4; gentoo
Posts: 192
Rep:
|
of course, good idea
|
|
|
11-18-2004, 11:06 AM
|
#8
|
Moderator
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
|
Quote:
Originally posted by sirclif
oh, im sorry, i misunderstood your problem. you want the script to just take the first line of the text file, and remove that line so that if another script opens the file, it wont have to worry about weather or not that line has been processed. is that about it? your right, my solution is not very efficient for that.
well, you could easily read the first line of the file into an variable using the head command.
VARIABLE = `head -n 1 filename.txt`
but im not sure how to just remove the first line without actually reading and writing each line in the file. ill think about it
|
sed -i '1d' filename.txt
That will remove the first line in-place (if your sed is version 3.x)
Btw - in this context it's whether, not weather ;)
Cheers,
Tink
|
|
|
11-18-2004, 11:17 AM
|
#9
|
Member
Registered: Sep 2004
Location: south texas
Distribution: fedora core 3,4; gentoo
Posts: 192
Rep:
|
yes, sorry about that. I just think of the word and my fingers decide how to spell it
|
|
|
11-18-2004, 11:44 AM
|
#10
|
LQ Newbie
Registered: Nov 2004
Posts: 13
Original Poster
Rep:
|
Thanks for all the help. This is exactly what I was looking to do! One question though 'ahh' - I need to flag the 'tail' command to write back ALL lines other than the one we removed and the amount of lines could vary significantly (say 5 -200). Maybe I am not quite understandign the 'tail' flags yet but does the way you've written it only write back two lines?
Thanks again for the help.
|
|
|
11-18-2004, 11:49 AM
|
#11
|
Moderator
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
|
sirclif's signature
If you don't care how it's spellt maybe
you should stick to talking ;)
Cheers,
Tink
|
|
|
11-18-2004, 12:04 PM
|
#12
|
Member
Registered: May 2004
Location: UK
Distribution: Gentoo
Posts: 293
Rep:
|
If you use the format
where x is a number, it will read the entire file starting from the line x lines from the start of the file.
May I suggest
or
if you are interested in the various ways tail can be used?
|
|
|
11-18-2004, 01:26 PM
|
#13
|
LQ Newbie
Registered: Nov 2004
Posts: 13
Original Poster
Rep:
|
Sorry to bother you guys again. In the declaration of the variable to get line 1 from my text file:
line1='head -n 1 /path/path/filename.txt'
I am getting an error that during "read $line1" command tha:
" '-n': not a valid identifier "
What am I doing wrong?
|
|
|
11-18-2004, 03:06 PM
|
#14
|
LQ Newbie
Registered: Nov 2004
Posts: 13
Original Poster
Rep:
|
I got it working perfectly! I cannot thank all of you enough for your help. Like I said, I'm buying beers (when you come to the States)!
Cheers,
alts
|
|
|
11-18-2004, 03:20 PM
|
#15
|
LQ Newbie
Registered: Nov 2004
Posts: 13
Original Poster
Rep:
|
. . . well, almost . . .
One more thing I need is this script to query the text file to see if there's anything in it and then run once again if there is, in fact, any more info in the text file.
I know this will be using a "while" loop but again I am stuck on the proper syntax . . .
|
|
|
All times are GMT -5. The time now is 12:07 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
|
|