ProgrammingThis 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.
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.
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:
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
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!
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
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 ;)
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?
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 . . .
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.