Find the text in the file and delete the rest of the line.

Hi,

I have one requiremnet like this. I need to find some particular string (eg.IncludeDateTime = ) in a file. And wherever it finds the string the unix script has to delete the remaining text coming after the string (ie., 'IncludeDateTime = ' ) in the same line. I tried to write this script in unix using 'sed' command but no luck. I am giving some example as below.

Ex. The file contains..

Last update date $$IncludeDateTime = 04/18/2012 11:38:55

The output I am expecting is..

Last update date $$IncludeDateTime =

Can any one please help in this regard. Heartful Thanks to all in advance.

Thanks | Suresh

You can do some thing like below using sed

cat file  | sed 's#\(\.*IncludeDateTime\).*#\1#'
# awk -vx="IncludeDateTime =" '{sub(x".*",x)}1' file
Last update date $$IncludeDateTime =

Thanks Sarbjit. That is working fine. Now what I want is ..

Initial file is like this..

Last update date $$IncludeDateTime = 04/18/2012 11:38:55

After executing the command

cat file1  | sed 's#\(\.*IncludeDateTime\).*#\1#'

The out put is like this..

Last update date $$IncludeDateTime = 

Now The third step is what I want is ..

Last update date $$IncludeDateTime = 01/01/12 10:12:10

I tried using the following command..

cat file1 | sed 's#\(\.*$$IncludeDateTime =\).*#\1#' | sed "s/IncludeDateTime =/IncludeDateTime = $(echo ${STR1/
/\//\\\/})/g" file1

The output I am getting is ..

Last update date $$IncludeDateTime = 04/18/2012 11:38:55 = 01/01/12 10:12:10

But what I want is..

Last update date $$IncludeDateTime = 01/01/12 10:12:10

Can anyone please let me know how to write the script for this. Thanks for your help..
Suresh

---------- Post updated at 01:25 PM ---------- Previous update was at 01:21 PM ----------

And where the variable $STR1 contains the date value of '01/01/12 10:12:10'. Which was defined in the earlier part of the script.

# sed "s|\(.*IncludeDateTime = \).*|\1$STR1|" file
Last update date $$IncludeDateTime = 01/01/12 10:12:10

regards
ygemici

Thanks ygemici,

The following query worked fine to me.

sed "s|\(.*IncludeDateTime = \).*|\1$STR1|" file

It changed the existing line in the file from ->

Last update date $$IncludeDateTime = 04/18/2012 11:38:55

to ->

Last update date $$IncludeDateTime = 01/01/12 10:12:10

Thanks
Suresh