LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
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 11-17-2004, 04:34 PM   #1
alts
LQ Newbie
 
Registered: Nov 2004
Posts: 13

Rep: Reputation: 0
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
 
Old 11-17-2004, 05:10 PM   #2
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
Moved: This thread is more suitable in "Programming", and has been moved accordingly to help your thread/question get the exposure it deserves.
 
Old 11-17-2004, 08:00 PM   #3
sirclif
Member
 
Registered: Sep 2004
Location: south texas
Distribution: fedora core 3,4; gentoo
Posts: 192

Rep: Reputation: 30
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
 
Old 11-17-2004, 08:09 PM   #4
alts
LQ Newbie
 
Registered: Nov 2004
Posts: 13

Original Poster
Rep: Reputation: 0
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
 
Old 11-18-2004, 08:49 AM   #5
sirclif
Member
 
Registered: Sep 2004
Location: south texas
Distribution: fedora core 3,4; gentoo
Posts: 192

Rep: Reputation: 30
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
 
Old 11-18-2004, 09:12 AM   #6
ahh
Member
 
Registered: May 2004
Location: UK
Distribution: Gentoo
Posts: 293

Rep: Reputation: 31
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.
 
Old 11-18-2004, 09:34 AM   #7
sirclif
Member
 
Registered: Sep 2004
Location: south texas
Distribution: fedora core 3,4; gentoo
Posts: 192

Rep: Reputation: 30
of course, good idea
 
Old 11-18-2004, 11:06 AM   #8
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
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
 
Old 11-18-2004, 11:17 AM   #9
sirclif
Member
 
Registered: Sep 2004
Location: south texas
Distribution: fedora core 3,4; gentoo
Posts: 192

Rep: Reputation: 30
yes, sorry about that. I just think of the word and my fingers decide how to spell it
 
Old 11-18-2004, 11:44 AM   #10
alts
LQ Newbie
 
Registered: Nov 2004
Posts: 13

Original Poster
Rep: Reputation: 0
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.
 
Old 11-18-2004, 11:49 AM   #11
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
sirclif's signature

If you don't care how it's spellt maybe
you should stick to talking ;)


Cheers,
Tink
 
Old 11-18-2004, 12:04 PM   #12
ahh
Member
 
Registered: May 2004
Location: UK
Distribution: Gentoo
Posts: 293

Rep: Reputation: 31
If you use the format
Code:
tail -n +x
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
Code:
man tail
or
Code:
info tail
if you are interested in the various ways tail can be used?
 
Old 11-18-2004, 01:26 PM   #13
alts
LQ Newbie
 
Registered: Nov 2004
Posts: 13

Original Poster
Rep: Reputation: 0
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?
 
Old 11-18-2004, 03:06 PM   #14
alts
LQ Newbie
 
Registered: Nov 2004
Posts: 13

Original Poster
Rep: Reputation: 0
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
 
Old 11-18-2004, 03:20 PM   #15
alts
LQ Newbie
 
Registered: Nov 2004
Posts: 13

Original Poster
Rep: Reputation: 0
. . . 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 . . .
 
  


Reply



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
Convert an info file(bash.info.gz) to a single html file Darwish Linux - Software 2 09-24-2005 06:51 AM
gxine info text cragwolf Linux - Software 1 07-23-2005 04:53 AM
text mode terminal info AbhishekSamuel Linux - General 1 05-03-2005 12:23 PM
emacs and info and rpm in text mode pleinair Mandriva 0 08-03-2004 06:43 PM
PHP & MySQL getting info from text file neon Programming 1 10-15-2003 12:34 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:32 PM.

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
Open Source Consulting | Domain Registration