LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices

Reply
 
Search this Thread
Old 03-14-2008, 02:25 PM   #1
KSUA
LQ Newbie
 
Registered: Mar 2008
Posts: 2

Rep: Reputation: 0
Create a script that will modify a text file


I am trying to create a script that will modify a text file.

I would like help writing a script that will open a text file called ‘Event22.txt’, and change the value ‘Event 22’ to ‘Event 23’, and ‘id-22’ to ‘id=23’ then save the file as ‘Event 23’. The test looks like this:

"wms_title" "Event 22"
END
# Get the lines from the 'location' column of the 'settlement' table
DATA "location from
(SELECT
sp.settlement_part_id,
sp.name as settlementname,
sp.location,
lr.loss_unit as unit,
lr.value as val
FROM (settlement_part sp left join loss_result lr on (sp.settlement_part_id=lr.item_id and lr.item_type='settlement_part') )
left join loss_model lm on lm.model_id=lr.model_id
WHERE lr.model_id in (SELECT l.model_id FROM loss_model l WHERE l.calculation_session_id=22)
and lr.loss_unit='patients (mean)'

) as set_calc
using unique settlement_part_id using SRID=4326

"
 
Old 03-14-2008, 02:31 PM   #2
osor
HCL Maintainer
 
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450

Rep: Reputation: 70
Code:
sed -e 's/Event 22/Event 23/; s/id-22/id=23/' Event22.txt > Event23.txt
If you want all occurrences of ‘22’ to ‘23‘, just use:
Code:
sed -e 's/22/23/g' Event22.txt > Event23.txt
 
Old 03-14-2008, 03:01 PM   #3
KSUA
LQ Newbie
 
Registered: Mar 2008
Posts: 2

Original Poster
Rep: Reputation: 0
Question Thanks for the help!

Thanks for the help!

Is there a way to have this script run as a result of a click event on a webpage?

In addition, can the number ‘23’ be ‘24’ for the next click event and ’25’ for the next and so on?
 
Old 03-14-2008, 04:51 PM   #4
osor
HCL Maintainer
 
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450

Rep: Reputation: 70
Quote:
Originally Posted by KSUA View Post
Is there a way to have this script run as a result of a click event on a webpage?
You have to set up CGI in your webserver, so that it will execute bash scripts (if you chose to write the script in bash). Then, use a script like this:
Code:
#!/bin/bash

COUNT_FILE="countfile"

if [ -f "$COUNT_FILE" ]; then
	count=$(<$COUNT_FILE)
else
	count=1
fi

let next=count+1

sed -e "s/Event $count/Event $next/; s/id_$count/id_$next/" "Event$count.txt" > "Event$next.txt"

echo -n $next > $COUNT_FILE
You might need to put a cd in the beginning of the script (to the data directory), and will obviously need write permissions in that directory and to the files. You might want to write your script in Perl, as I am not very knowledgeable about the security implications of CGI bash scripts.

As an aside, what exactly are you trying to accomplish? If the Event files are served up as web content, you might serve them dynamically instead of making almost-identical copies on your server’s disk.
 
Old 03-16-2008, 06:22 AM   #5
frenchn00b
Senior Member
 
Registered: Jun 2007
Location: E.U., Mountains :-)
Distribution: Debian, Etch, the greatest
Posts: 2,546

Rep: Reputation: 51
cool tool too, in the debian repositories:

Code:
 rpl
usage: rpl [options] old_string new_string target_file(s)

rpl: error: must have at least three arguments
 
Old 03-16-2008, 09:31 AM   #6
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 241Reputation: 241Reputation: 241
Quote:
Originally Posted by KSUA View Post
Thanks for the help!

Is there a way to have this script run as a result of a click event on a webpage?

In addition, can the number ‘23’ be ‘24’ for the next click event and ’25’ for the next and so on?
depending on what language you are using for CGI, you can use that language to change things in a file.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
modify content in a file using shell script fjkum Linux - Newbie 2 08-03-2006 05:46 AM
how to modify a text file or a string in shell programming luckyvietman Programming 5 07-12-2005 06:08 PM
Using grep, etc to modify a text file dangerousdave Linux - Newbie 7 02-23-2005 03:49 PM
modify text of an jpg file on linux with gimp 2.0.4 tcma Linux - Software 1 10-19-2004 12:12 PM
Create text file with a script zael Programming 3 06-02-2004 04:27 AM


All times are GMT -5. The time now is 11:03 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration