5 Replies - 4022 Views - Last Post: 24 August 2010 - 10:59 AM Rate Topic: -----

#1 Guest_ckuyook*


Reputation:

How to write simple script editing text files and executing commands?

Posted 23 August 2010 - 08:51 PM

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

Is This A Good Question/Topic? 0

Replies To: How to write simple script editing text files and executing commands?

#2 pchapin  Icon User is offline

  • New D.I.C Head

Reputation: 34
  • View blog
  • Posts: 43
  • Joined: 23-July 10

Re: How to write simple script editing text files and executing commands?

Posted 24 August 2010 - 05:59 AM

If you already know a programming language using the language you know is likely to be the quickest. Even if another language makes the task easier the time you spend learning it can quickly overshadow the time saved using it... particularly if you only plan to use the language once.

Back when C was the only language I really felt fluent with I wrote pretty much everything in C. It worked surprisingly well. I knew enough of the library off the top of my head that I didn't have to look things up, and I knew the idioms and pitfalls of the language well enough to produce the code I needed with a minimum of fuss.

These days I'm comfortable with a variety of languages and I sometimes spend more time than it's worth worrying about which language is the "best" for a particular task. Maybe I should just go back to using C for everything!

Anyway... you didn't say if you are fluent with any particular language or not. Also you might have other reasons for wanting to learn something new. If it's on your to-do list to learn Perl this might be a good application for it. Perl does very well when it comes to processing text files in various ways.
Was This Post Helpful? 0
  • +
  • -

#3 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5795
  • View blog
  • Posts: 12,628
  • Joined: 16-October 07

Re: How to write simple script editing text files and executing commands?

Posted 24 August 2010 - 06:15 AM

View Postckuyook, on 23 August 2010 - 09:51 PM, said:

7) run a certain command in shell which will execute a CLI program and leave it running


Well, this is the first hint of the OS used. :P

For a script, in a windows environment, the current best choice is probably a PowerShell script.

It should allow you to do what you're looking for. It can even leverage the .NET framework directly.
Was This Post Helpful? 0
  • +
  • -

#4 Guest_ckuyook*


Reputation:

Re: How to write simple script editing text files and executing commands?

Posted 24 August 2010 - 07:04 AM

View Postpchapin, on 24 August 2010 - 04:59 AM, said:

If you already know a programming language using the language you know is likely to be the quickest. Even if another language makes the task easier the time you spend learning it can quickly overshadow the time saved using it... particularly if you only plan to use the language once.

...

Anyway... you didn't say if you are fluent with any particular language or not. Also you might have other reasons for wanting to learn something new. If it's on your to-do list to learn Perl this might be a good application for it. Perl does very well when it comes to processing text files in various ways.

I've learned bits and pieces of various languages, enough to write simple scripts in C, Perl, PHP, and Java but they were literally like less than a dozen lines of code. I'm getting from other people that bash scripting would do this job.

View Postbaavgai, on 24 August 2010 - 05:15 AM, said:

View Postckuyook, on 23 August 2010 - 09:51 PM, said:

7) run a certain command in shell which will execute a CLI program and leave it running


Well, this is the first hint of the OS used. :P

For a script, in a windows environment, the current best choice is probably a PowerShell script.

It should allow you to do what you're looking for. It can even leverage the .NET framework directly.

I'm in OpenBSD
Was This Post Helpful? 0

#5 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5795
  • View blog
  • Posts: 12,628
  • Joined: 16-October 07

Re: How to write simple script editing text files and executing commands?

Posted 24 August 2010 - 07:22 AM

View Postckuyook, on 24 August 2010 - 08:04 AM, said:

I'm in OpenBSD


Well, then, PowerShell wouldn't be an option.

For shell type scripts, Perl is probably the most popular. However, Python has gained a lot of *nix scripting converts and is my current favorite for the environment.

You can do a lot with bash, but most of it's power comes from gluing together other shell tools. This is in accordance with the unix philosophy of having many programs that each do one thing well. Getting everything done in a simple shell script is fun, but when it becomes less simple you'll often find you're in for a fight.

You say you're familiar with Perl; I'd go with that.
Was This Post Helpful? 0
  • +
  • -

#6 Guest_ckuyook*


Reputation:

Re: How to write simple script editing text files and executing commands?

Posted 24 August 2010 - 10:59 AM

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

#!/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:
apple
banana
candy
...
[several hundred lines of this]
yoyo
zebra



1st config file:
option WORD1
otheroption blaha
moreoption blahb



2nd config:
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 while I leave the previous one running), 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.
Was This Post Helpful? 0

Page 1 of 1