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 08-23-2010, 10:40 PM   #1
SentralOrigin
Member
 
Registered: Jul 2005
Distribution: Gentoo, Ubuntu
Posts: 318

Rep: Reputation: 30
Looking to write a simple script


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
 
Old 08-24-2010, 12:13 AM   #2
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by SentralOrigin View Post
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
 
1 members found this post helpful.
Old 08-24-2010, 12:20 AM   #3
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
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.

--- rod.
 
1 members found this post helpful.
Old 08-24-2010, 07:29 AM   #4
SentralOrigin
Member
 
Registered: Jul 2005
Distribution: Gentoo, Ubuntu
Posts: 318

Original Poster
Rep: Reputation: 30
Quote:
Originally Posted by ghostdog74 View Post
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 View Post
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?
 
Old 08-24-2010, 08:40 AM   #5
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by SentralOrigin View Post
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.
 
Old 08-24-2010, 09:14 AM   #6
SentralOrigin
Member
 
Registered: Jul 2005
Distribution: Gentoo, Ubuntu
Posts: 318

Original Poster
Rep: Reputation: 30
Quote:
Originally Posted by ghostdog74 View Post
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.
 
Old 08-24-2010, 09:19 AM   #7
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192
Quote:
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.

Good luck
 
1 members found this post helpful.
Old 08-24-2010, 09:20 AM   #8
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
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.
 
Old 08-24-2010, 12:41 PM   #9
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
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.
 
1 members found this post helpful.
Old 08-24-2010, 12:57 PM   #10
SentralOrigin
Member
 
Registered: Jul 2005
Distribution: Gentoo, Ubuntu
Posts: 318

Original Poster
Rep: Reputation: 30
Okay, I decided to make it a bit simpler, and with bash scripting it seems shorter. I have this so far:

Code:
#!/bin/bash
killall process1
#editing configs
process1 &
sleep 5
process2
The #editing configs part is the only part left. So this is what I need to get this script to do now:

The large file is in this format:
Code:
apple
banana
candy
...
[several hundred lines of this]
yoyo
zebra
1st config file:
Code:
option WORD1
otheroption blaha
moreoption blahb
2nd config:
Code:
name = { john }
colors = { red,blue }
options = { opt1 = WORD1; opt2 = WORD1; opt3 = WORD1; }
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.
 
Old 08-24-2010, 03:44 PM   #11
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
Code:
NEWWORD=dog
sed "s/opt1 = .*; opt2 = .*; opt3 = .*; /opt1 = ${NEWWORD}; opt2 = ${NEWWORD}; opt3 = ${NEWWORD}; /g" LQ-SentralOrigin2.dat
name = { john }
colors = { red,blue }
options = { opt1 = dog; opt2 = dog; opt3 = dog; }
man sed for more details.

--- rod.

Last edited by theNbomr; 08-24-2010 at 03:48 PM.
 
1 members found this post helpful.
Old 08-24-2010, 07:42 PM   #12
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192
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??
 
1 members found this post helpful.
Old 08-24-2010, 11:12 PM   #13
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
Quote:
Originally Posted by grail View Post
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.

--- rod.
 
  


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
How to write a simple BASH script to "test if have folowing files, than delete."? hocheetiong Programming 10 10-01-2009 12:17 PM
Newbie trying to write a simple backup script to backup a single folder Nd for school stryker759a Linux - Newbie 2 09-16-2009 08:52 AM
how can I write a simple script to determine cpu? mtucker1337 Programming 5 05-24-2007 12:33 PM
how to write a simple c prog to call a script RajRed Programming 1 10-05-2005 05:10 PM
how to write a simple c prog to call a script RajRed Linux - Software 1 10-05-2005 02:57 PM

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

All times are GMT -5. The time now is 12:31 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