editing txt file via scripts...

Associate
Joined
8 Mar 2007
Posts
2,176
Location
between here and there
hey guys,

I have a large txt file that i need to add some data to. Whats the best way of doing this from the command line?

example;

txt file to edit has a section within like this;

####_customer_data

I need to search for the above string, then under neath it add the following lines;

line one {
line two data
line three data
line four data
}

I need to be careful that the script DOESN'T overwrite any existing data.

So far I've had success with;

cat txtfile_to_edit.cfg | sed "/####_customer_data/a\
cfg_dir=$webroot/customers/$1" > txtfile_to_edit.edit.cfg

but I'm struggling to get to work over multiple lines.

Any suggestion?

Thanks in advance
 
It's probably easiest to do an inline search and replace using sed.

Something like this?

Code:
#!/bin/sh

KEY_WORD='####_customer_data'

REPLACE_WITH="$KEY_WORD\n\
{\n\
line two data\n\
line three data\n\
line four data\n\
}"

sed -i "s/$KEY_WORD/$REPLACE_WITH/g" txtfile_to_edit.cfg
 
Thanks.

I tried your suggestion but it didn't work, so I noted the \n in your code and applied in it mine, giving me the result i was after!!

so thank you again!!
 
Back
Top Bottom