-1

This is what i am trying to achieve, seems it has some errors. Escape sequences in the strings yet to be added

search_string="LoadModule rewrite_module "${ORACLE_HOME}/ohs/modules/mod_rewrite.so"
insert_string1="LoadModule reqtimeout_module "${ORACLE_HOME}/modules/mod_reqtimeout.so"
insert_string2="<IfModule reqtimeout_module>RequestReadTimeout header=20-40,MinRate=500 body=20,MinRate=500 </IfModule>"
grep "$insert_string1" httpds.conf;
if [ $? -ne 0 ]; then
    sed -r -e ":a;/${search_string}/ {n;s/$/${insert_string1}/};ta" httpds.conf
    sed -r -e ":a;/${insert_string1}/ {n;s/$/${insert_string2}/};ta" httpds.conf
    echo "Configuration inserted successfully"
fi
#################### Output File should look as follows
LoadModule unique_id_module "${ORACLE_HOME}/ohs/modules/mod_unique_id.so"
LoadModule setenvif_module "${ORACLE_HOME}/ohs/modules/mod_setenvif.so"
LoadModule context_module "${ORACLE_HOME}/ohs/modules/mod_context.so"
LoadModule rewrite_module "${ORACLE_HOME}/ohs/modules/mod_rewrite.so"
LoadModule reqtimeout_module "${ORACLE_HOME}/modules/mod_reqtimeout.so"
<IfModule reqtimeout_module>
   RequestReadTimeout header=20-40,MinRate=500 body=20,MinRate=500 
</IfModule>"

LoadModule dumpio_module "${ORACLE_HOME}/ohs/modules/mod_dumpio.so"
  • 2
    `vi` seems a bit of an unusual choice for what it looks like you're doing. Why not consider `grep`, `awk`, `sed`, etc.? – dat Jun 07 '18 at 18:08
  • On my system it is a warning (add `2>/dev/null`). Or use something like `sed '/mysearchstring/ s/$/\nSampleInsert/' configfile` with a tmp file or option `-i` when supported, – Walter A Jun 07 '18 at 18:48
  • See: [How to edit files non-interactively (e.g. in pipeline)?](https://vi.stackexchange.com/questions/788/how-to-edit-files-non-interactively-e-g-in-pipeline), also consider migrating this Q to https://vi.stackexchange.com/ – agc Jun 07 '18 at 19:09
  • You can refer to this question here: https://stackoverflow.com/questions/15402770/how-to-grep-and-replace?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa – dhillonfarms Jun 07 '18 at 19:23
  • Please edit your post to include a set of sample "before" data and what it should look like after processing to eliminate any guessing. – Gary_W Jun 07 '18 at 20:58
  • i have edited it .. could you please have look – Gayathri Gowda Jun 14 '18 at 14:33

3 Answers3

0

You can check this one with sed:

sed -i '/string/a SampleInsert/' configfile.txt

if -i is not supported, then simply you can redirect the output to a new file:

sed '/string/a SampleInsert/' configfile.txt > output_file.txt

User123
  • 1,498
  • 2
  • 12
  • 26
0

By append do you mean add to the end of the line following the line with the match? If so, the trick is to get sed to rescan for the pattern else it will miss the line after the one where SampleInsert is added. You need to use the label and go to label commands:

$ cat configfile.txt
1 gary     tty7
2 mysearchstringgary     tty7
3 testingmysearchstring gary     tty7
4 gary     tty7mysearchstring
5
6 gary     tty7mysearchstring
7 gary     tty7
8 gary     tty7
$
$
$ sed -r -e ':a;/mysearchstring/ {n;s/$/SampleInsert/};ta' configfile.txt
1 gary     tty7
2 mysearchstringgary     tty7
3 testingmysearchstring gary     tty7SampleInsert
4 gary     tty7mysearchstringSampleInsert
5 SampleInsert
6 gary     tty7mysearchstring
7 gary     tty7SampleInsert
8 gary     tty7
$ 

Since you didn't supply test data I just made some up with the search text in various places. Don't forget a blank line for testing.

The sed command uses -r -e args for extended, regular expressions. Inside of the single quotes are multiple commands, separated by semi-colons. First set a label named a, then look for lines matching "mysearchstring". If found, run the commands surrounded by the curly braces and separated by semi-colons. n means read the next line into the pattern buffer since that's the line you want to append the text to. Search for the end of the line and append "SampleInsert". Now, THAT line may have the search text so it needs to be searched as well. That's why the next command is ta, which means go to label a. Label a is just before the search for the search string so that causes the current line to be rescanned for the search pattern.

EDIT: If used inside of a bash script where the search and insert values are variables defined in the script:

#!/bin/bash

readonly searchstr='mysearchstring'
readonly insertstr='SampleInsert'

sed -r -e ":a;/${searchstr}/ {n;s/$/${insertstr}/};ta" configfile.txt

Or passed in a arguments to the script (should have some error handing but this is just to illustrate):

$ fixfile.sh mysearchstr SampleInput

#!/bin/bash

readonly searchstr=$1
readonly insertstr=$2

sed -r -e ":a;/${searchstr}/ {n;s/$/${insertstr}/};ta" configfile.txt
Gary_W
  • 9,933
  • 1
  • 22
  • 40
  • thanks Gary, seems your solution will work..i just modified the question ... could you please have a look – Gayathri Gowda Jun 08 '18 at 17:54
  • Please add what the file should look like after processing. – Gary_W Jun 08 '18 at 18:03
  • I tried it but gives an error sed: -e expression #1, char 64: unknown option to `s' – Gayathri Gowda Jun 13 '18 at 05:54
  • search_string="LoadModule rewrite_module "${ORACLE_HOME}/ohs/modules/mod_rewrite.so" insert_string1="LoadModule reqtimeout_module "${ORACLE_HOME}/modules/mod_reqtimeout.so" insert_string2="RequestReadTimeout header=20-40,MinRate=500 body=20,MinRate=500 " grep "$insert_string1" httpds.conf; if [ $? -ne 0 ]; then sed -r -e ":a;/${search_string}/ {n;s/$/${insert_string1}/};ta" httpds.conf sed -r -e ":a;/${insert_string1}/ {n;s/$/${insert_string2}/};ta" httpds.conf echo "Configuration inserted successfully" fi – Gayathri Gowda Jun 13 '18 at 06:43
  • LoadModule unique_id_module "${ORACLE_HOME}/ohs/modules/mod_unique_id.so" LoadModule setenvif_module "${ORACLE_HOME}/ohs/modules/mod_setenvif.so" LoadModule context_module "${ORACLE_HOME}/ohs/modules/mod_context.so" LoadModule rewrite_module "${ORACLE_HOME}/ohs/modules/mod_rewrite.so" LoadModule reqtimeout_module "${ORACLE_HOME}/modules/mod_reqtimeout.so" RequestReadTimeout header=20-40,MinRate=500 body=20,MinRate=500 " ### output file after modification LoadModule dumpio_module "${ORACLE_HOME}/ohs/modules/mod_dumpio.so" – Gayathri Gowda Jun 13 '18 at 06:44
  • please find the code and output in the above comments .. you may copy that to a file .. i tried to format it .. but it didnt work – Gayathri Gowda Jun 13 '18 at 06:46
  • You need to edit the original post as comments do not allow for formatting. – Gary_W Jun 13 '18 at 12:32
  • I just edited the original post... hope now you have a better view of it – Gayathri Gowda Jun 14 '18 at 05:24
0

Using awk:

#!/bin/bash

FIND=".el"; # assign whatever variable you want
APPEND="Hi";
awk -v find="$FIND" -v append="$APPEND" '{print $0 (index($0, find) != 0 ? "\n"append : "")}' configfile.txt
REDDY PRASAD
  • 1,309
  • 2
  • 14
  • 29