0

I am completely new to sed script. I have been researching how to add text to a file and managed to get the text I want adding to the correct line in the file but can not find a way to add it to the correct position! so the line I have in the text file looks like this

/^From: \s*(.*@)?(((test\.new\.com)))/ REJECT You are not me

and i want to get input from user and add input to above line, which result should be like below, user input is "test2.newer.com" and i want to add this string |(test2\.newer\.com)

/^From: \s*(.*@)?(((test\.new\.com)|(test2.\newer\.com)))/ REJECT You are not me

i try this but not working

read -p "Enter new domain: " newdomain
file="./testfile"
sed -i "/You are not me/ s/^\(.*\)\())\)/\1, |($newdomain)\2/" $file

how do I go about adding it to the correct position?

mrsadeghi
  • 1
  • 1
  • Remove the spaces around the `|` mark. (also you've changed **.com** to **.ir** between your examples, i don't know if that was intentional or not, just thought i'd mention it). – Raxi Jan 02 '22 at 08:49
  • 1
    But for a higher level solution: considering the `/^From: ` i can take an educated guess that you are parsing email messages. There are plenty of existing tools around that let you parse an email, and give a structured response in a more useful format (like maybe JSON for example). doing it with regular expressions is very likely to be a poor choice (harder to build, much harder to maintain, less reliable for edgecases, ...) – Raxi Jan 02 '22 at 08:51
  • You should add more and certain details. You didn't write the the text you want to change. First code line includes test.new.com in command, on the other hand test.new.ir in the second code line. – ust Jan 02 '22 at 08:57
  • acutually i find this solution but it not work sed -i "/You are not me/ s/^\(.*\)\())\)/\1, |($newdomain)\2/" $file – mrsadeghi Jan 02 '22 at 08:58
  • `how to add text to a file and managed to get the text I want adding to the correct line in the file` Could you split that into multiple sentences and explain in steps what you want. What text do you want to add? Where? To what line? Which one is correct? Where is the correct position? `i want to add new string "test2.newer.com"` You seem to want to add `|(test2.\newer\.com)`, not `test2.newer.com`. – KamilCuk Jan 02 '22 at 09:43
  • Seriously please give sample data and expected output. – konsolebox Jan 02 '22 at 12:01
  • sorry about that but i want get user input for example: "test2.newer.com" and change the input user like this: "test2\.newer\.com" ultimately search for line which have string "You are not me" and add string "test2\.newer\.com" output file should be like this: /^From:\s*(.*@)?(((test\.new\.com)|(test2\.newer\.com)))/ REJECT You are not me – mrsadeghi Jan 02 '22 at 12:42
  • Overall, your question is specific... but this is XY question. Are you trying to automate modifying configuration of some tool that is used for mail filtering? If yes, what tool is that? – KamilCuk Jan 02 '22 at 18:22

3 Answers3

0

With Escape a string for a sed replace pattern you can replace any line content that you want.

lineinfile='/^From: \s*(.*@)?(((test\.new\.com)))/ REJECT You are not me'
newcontent='/^From: \s*(.*@)?(((test\.new\.com)|(test2.\newer\.com)))/ REJECT You are not me'

ESCAPED_KEYWORD=$(printf '%s\n' "$lineinfile" | sed -e 's/[]\/$*.^[]/\\&/g');
ESCAPED_REPLACE=$(printf '%s\n' "$newlinecontent" | sed -e 's/[\/&]/\\&/g')
sed "s/$ESCAPED_KEYWORD/$ESCAPED_REPLACE/g"
KamilCuk
  • 120,984
  • 8
  • 59
  • 111
0

With newdomain='test2\.newer\.com' you can use the awk command

awk -v repl="${newdomain//\\/\\\\}" '
  BEGIN {OFS=FS=")))"}
  /You are not me/ {$1=$1 ")|(" repl}
  1
' "$file" > "$file".new && mv "$file".new "$file"

Explanation:
${newdomain//\\/\\\\}" awk wants to treat the escape sequence \. treated as a ., so add backslashes.
BEGIN {OFS=FS=")))"} Before parsing lines set ")))" as field separator (input and output).
/You are not me/ Change lines with this substring.
$1=$1 ")|(" repl' Append )|( and the string in repl to $1.
1 Print the line.
"$file" > "$file".new && mv "$file".new "$file" Dpn't edit in place but redirect to a new file and move that file to the original file when awk succeeded.

Walter A
  • 19,067
  • 2
  • 23
  • 43
0

thanks guys i find my problem and answer is like this:

read -p "Enter domain: " newdomain
sed -i "/You are not me/ s/\()))\)/)|$newdomain)))/' $file

but the output is like this:

/^From: \s*(.*@)?(((test\.new\.com)|(test2.newer.com)))/ REJECT You are not me

but i want this output:

/^From: \s*(.*@)?(((test\.new\.com)|(test2.\newer\.com)))/ REJECT You are not me
mrsadeghi
  • 1
  • 1