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.
Okay this will probably have multiple parts to it but I don't really want to trouble you guys with more help because I'm a total noob so I can just do the first part by hand (it's just editing a few hundred lines of text in a file; I have to do the same thing on each line and I'm sure there's a scripted solution for it but I can spare a couple hours).
So basically I have a file with several hundred lines, and each line is only 2-3 words.
I need the script to do these things in order:
1) kill process1
2) put the 1st line of this file list into already existing text file #1 (requires root to write): the line it needs to write to is like: "option LINE1" where the 1st line has to be written where 'LINE1' is, somewhere in this text file
3) save this text file
4) put the 1st line of this file list into existing text file #2: the line is like "set = { opt1 = LINE1; opt2 = LINE1; opt3 = LINE1; }" and again I want the 1st line to be filled in all where 'LINE1' says
5) save text file
6) start process1
7) run a certain command in shell which will execute a CLI program and leave it running (I don't think it can be run in the background because I tried starting it with the '&' sign at the end and it ran but it didn't connect the way it properly was supposed to)
8) repeat but use the next line in the file list
What programming language would be the quickest and easiest way to write something like this? It doesn't have to be fancy or anything, just work even if it's dirty
What programming language would be the quickest and easiest way to write something like this? It doesn't have to be fancy or anything, just work even if it's dirty
all of that can be done with the shell + *nix tools (eg awk) ( as well as other languages if you know them). To learn about shell scripting. To learn about awk, see the link in my sig
I sounds like a shell script to me. But... I think you need to define things a bit better; especially part #7; 'leave it running'. Does the program exist? What kind of program is it? Do you really need to launch an instance of it for each of the several hundred lines? When you say it didn't work properly when backgrounded, what happened. Is it expecting user interaction? With respect to the previous requirements: does 'put the 1st line of this file list into existing text file #2' mean anything about where in the file it is to go? Append okay? If yes, this part of the task becomes trivial.
all of that can be done with the shell + *nix tools (eg awk) ( as well as other languages if you know them). To learn about shell scripting. To learn about awk, see the link in my sig
Oh man I hate sed/awk. Last time I worked with those crazy complicated syntaxes and stuff, I went crazy
Quote:
Originally Posted by theNbomr
I sounds like a shell script to me. But... I think you need to define things a bit better; especially part #7; 'leave it running'. Does the program exist? What kind of program is it? Do you really need to launch an instance of it for each of the several hundred lines? When you say it didn't work properly when backgrounded, what happened. Is it expecting user interaction? With respect to the previous requirements: does 'put the 1st line of this file list into existing text file #2' mean anything about where in the file it is to go? Append okay? If yes, this part of the task becomes trivial.
Yes the program exists and can launch multiple instances. Each one has to be run separately. Its purpose is to have the user interact with it but for this purpose I don't need to; just leave it running. Both text files already have text in them, and the lines from the large file need to go into a certain place into the text files where they are necessary. What do you mean append okay?
Oh man I hate sed/awk. Last time I worked with those crazy complicated syntaxes and stuff, I went crazy
You hate maths, but you have to learn it to graduate. You want things easy? There's no such thing. Only through extensive learning, practicing can you become familiar with anything you do. If even after trying your best, you still don't like it, then find something else easier to use. (Python for example). Either way, stop whining and start learning.
You hate maths, but you have to learn it to graduate. You want things easy? There's no such thing. Only through extensive learning, practicing can you become familiar with anything you do. If even after trying your best, you still don't like it, then find something else easier to use. (Python for example). Either way, stop whining and start learning.
Sorry, I'm just looking for the easy way out because I just want it to do what I want it to do. Since I'm not looking to be a programmer or anything (I am a college dropout working at a fast food joint) since I don't have the intellectual capability to get work in such a demanding field. But I try very hard to play on my machines as my fun. That's not to say I haven't already planned to obtain some books on sed/awk today
Last edited by SentralOrigin; 08-24-2010 at 09:15 AM.
Since I'm not looking to be a programmer or anything
Actually ... yes you are. The moment you wrote this question that is exactly what you have set out to be. Are you going to become a guru, well by reading
your last post I would guess not, but the point still remains that if you wish to write a script then you need to be at least some sort of programmer.
Also, you have already done some of the hard yards The main idea behind bash scripts is to replicate a mundane task and automate it
to the best of your ability. As you already know the steps you wish to action, start placing them in a script one by one. When you get stuck, come and
ask a question related to where and why you are stuck.
So step one here is to work out how you are going to identify the process you wish to kill in step 1.
Considering your circumstances, i recommend Python (only because it has easy to read syntax just right for you). You can go to the link in my sig to learn about Python.
Your task has a few challenges. If you need to insert lines into a file(s) at specific positions within the file(s), it means reading and parsing the entire file(s), inserting the new data at the correct place, and then writing the file(s) back out to disk. Appending new data to the end of a file is simple, using file IO redirection. This is going to require more than a shell script, although a shell script can be a wrapper for the overall package.
You can do the easy looping part in a bash script, but you are probably going to want to use sed/awk/perl/python to do the file parsing/inserting.
You need to define/determine why simply backgrounding the processes is inadequate. It may be that they need to have a unique tty in which to run. Launching several hundred processes this way sounds like asking for an overloaded process table. Are you sure this is what you want to do? Can you give more application-specific details? It sounds like you may be looking at the wrong general solution to your problem.
You did come asking for help. When people respond with questions to try to get information about your problem, it serves your interest to actually provide answers to those questions. Please revisit the responses provided so far, and and see if you can supply more information. It will make your problem easier for you to solve.
--- rod.
I need the instances where it says 'WORD1' in both config files to be edited and replaced with the line I choose from the large file list. So say I want the word 'candy' to be placed where 'WORD1' is, I would run the script with the number '3' because 'candy' is the 3rd word in the list. But if I run the script again though (in a new terminal window), I have to make sure that 'candy' is replaced by the next word/line I choose because I can't have it hanging around from my previous use of the script.
Last edited by SentralOrigin; 08-24-2010 at 01:00 PM.
Well setting and increasing the number is no problem:
Code:
LINE_NO=$1
#<do other stuff>
(( LINE_NO++ ))
As shown by nbomr, sed is your friend for updating the files. Your main concern will be how to identify what you need to be changed, ie how unique WORD1 is.
Are you also requiring that after the first change has occurred, ie using candy, will the next run require candy to be changed??
Are you also requiring that after the first change has occurred, ie using candy, will the next run require candy to be changed??
I interpreted the requirements that way. My sample code seems to accommodate it, as long as the old word doesn't contain a semicolon. But, you never know; requirements seem to evolve in these sorts of cases. I guess I misunderstood that the processes launched would be killed on each iteration. At least that's the way it looks right now.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.