I have a file that has a RESOURCE keyword. It can occur anywhere in the file, like:


Now I want to replace Lnreddy37@gmail.com with reddy17777@gmail.com. I have to search for the line where I can find the RESOURCE keyword and then replace the word after the equal sign. The RESOURCE keyword has to remain unmodified.

Could some one please help me on this?





grep is not useful in this case as it does not allow you to modify the contents of the file.

Instead, one may use sed like so:


sed '/^RESOURCE=/s/='"$fromaddr"'$/='"$toaddr"'/' file >newfile

Given file as

some data
more data

this creates newfile as

some data
more data

The sed expression will select lines that starts with the string RESOURCE. For each such line, it will replace the email address, if it's present on the line. The pattern used for the substitution makes sure that we match the = and that the address ends at the end of line.

  • I dont want to hard code the Lnreddy37@gmail.com , it should be anything like nasir37@yahoo.com. i want to replace with any other like nasir45@gmail.com. it should be dynamic – Lakshminarayana just now edit – Lakshminarayana May 29 '18 at 11:04
  • @Lakshminarayana See updated answer. – Kusalananda May 29 '18 at 11:06
  • I dont want to write it in a new file again .. because the file consists of other data as well. It like below. – Lakshminarayana May 29 '18 at 11:14
  • @Lakshminarayana Then run with sed -i if you think it's doing the correct thing, and don't redirect to a new file. I'm generally avoiding using sed -i as it's non-portable and since if you make a typo, it may destroy your original data. – Kusalananda May 29 '18 at 11:20
  • I understand that. how can i search for the line which is having RESOURCE word. I have to search it in a file which is having other data and have to replace it. Moreover i have to pass the file name as a parameter – Lakshminarayana May 29 '18 at 11:43

The sed command is better.

sed -i 's/RESOURCE=Lnreddy37@gmail.com/RESOURCE=reddy17777@gmail.com/' yourfile

The above works in my test but if you want to test it for yourself then you can try this one first:

sed 's/RESOURCE=Lnreddy37@gmail.com/RESOURCE=reddy17777@gmail.com/' yourfile

That will write the changes to standard output without affecting the file.

Either way, it gives your desired change of:


If the value of RESOURCE is different in the file and you want to change it something different then:

grep RESOURCE= yourfile

That will return the line where it is and show the value.

You can then use

sed 's/RESOURCE=existingemail@gmail.com/RESOURCE=emailyouwant@gmail.com/' yourfile

For future reference, it's important to make all of this clear in your original question so that you can get the help that you need without all of this rigamarole.

  • I dont want to hard code the Lnreddy37@gmail.com , it should be anything like nasir37@yahoo.com. i want to replace with any other like nasir45@gmail.com. it should be dynamic – Lakshminarayana May 29 '18 at 11:04
  • I don't understand what you mean. In your question, it shows that you want the output to be RESOURCE=reddy17777@gmail.com which both of the answers achieve. What do you mean by: "I dont want to hard code the Lnreddy37@gmail.com"? and "it should be dynamic"? – Nasir Riley May 29 '18 at 11:20
  • @Lakshminarayana If you want it to actually make the changes to the file then use sed i like I gave in my first command. Test it first with the sed by itself in my second command to make sure that it's doing what you want and then use the sed -i in my first command once you're sure that it works. – Nasir Riley May 29 '18 at 11:37
  • i mean i dont want to hard code them. sed 's/RESOURCE=Lnreddy37@gmail.com/RESOURCE=reddy17777@gmail.com/' yourfile. i want to pass it like parameters both input and output. and also i want to search for that line where i have RESOURCE and have to replace it – Lakshminarayana May 29 '18 at 11:41
  • @Lakshminarayana Then either use Kusalananda's answer and pass the email addresses to a variable or grep RESOURCE= file, get the email address that appears, and then use sed -i and replace Lnreddy37@gmail.com and reddy17777@gmail.com with whatever is there and whatever you want it to be. – Nasir Riley May 29 '18 at 11:52

You seem to be saying that you want to replace the string that appears after the =, no matter what it isfor example, in the sample data, you want to replace Lnreddy37@gmail.com.  But you’re saying that, no matter what the string after the = is, you want to replace it with reddy17777@gmail.com — apparently you want that hardcoded.

There are a few variations on how to do this.  The simplest is

sed 's/RESOURCE=.*/RESOURCE=reddy17777@gmail.com/'

which (like all the below commands) uses the fact that .* means “match whatever is there”.  If you don’t want to type RESOURCE= twice, you can shorten the above to

sed 's/\(RESOURCE=\).*/\1reddy17777@gmail.com/'

where \(\) marks a part of the search string as a group (you can have up to nine groups) and \1 means replace with the first group.

The above commands will find and replace RESOURCE= wherever it appears on the line.  So, for example, the input

# Lakshminarayana wants to change all occurrences of "RESOURCE=".
FOX RESOURCE=The quick brown fox
# Comment: Originally line 2 said RESOURCE=ringo@liverpool.co.uk

would be changed to

# Lakshminarayana wants to change all occurrences of "RESOURCE=reddy17777@gmail.com
FOX RESOURCE=reddy17777@gmail.com
# Comment: Originally line 2 said RESOURCE=reddy17777@gmail.com

If you want to match only when RESOURCE= appears at the beginning of a line, use ^:

sed 's/^RESOURCE=.*/RESOURCE=reddy17777@gmail.com/'


sed 's/^\(RESOURCE=\).*/\1reddy17777@gmail.com/'

If you want to replace only the resource value, and not the entire rest of the line — e.g.,

RESOURCE=Lnreddy37@gmail.com    TALENT=george@hotmail.com


RESOURCE=reddy17777@gmail.com   TALENT=george@hotmail.com

that can be done, too.  Edit your question to say exactly what you want, with full, clear explanations and examples.

OK, pick one of the above s commands.  Now, if you want to edit your file in place (as you have indicated), do

sed  -i  {s command }  { yourfile }

If you want to produce a new file, do

sed  {s command }  { oldfile }  >  { newfile }

Don’t actually type the { and }; they are there just for demarcation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.