LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   Create a script that will modify a text file (https://www.linuxquestions.org/questions/linux-general-1/create-a-script-that-will-modify-a-text-file-628072/)

KSUA 03-14-2008 01:25 PM

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

"

osor 03-14-2008 01:31 PM

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

KSUA 03-14-2008 02:01 PM

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?

osor 03-14-2008 03:51 PM

Quote:

Originally Posted by KSUA (Post 3088802)
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.

frenchn00b 03-16-2008 05:22 AM

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


ghostdog74 03-16-2008 08:31 AM

Quote:

Originally Posted by KSUA (Post 3088802)
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.


All times are GMT -5. The time now is 06:16 AM.