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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Find the text in the file and delete the rest of the line.
# 1  
Old 04-20-2012
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
# 2  
Old 04-20-2012
You can do some thing like below using sed

Code:
cat file  | sed 's#\(\.*IncludeDateTime\).*#\1#'

# 3  
Old 04-20-2012
Code:
# awk -vx="IncludeDateTime =" '{sub(x".*",x)}1' file
Last update date $$IncludeDateTime =

# 4  
Old 04-20-2012
Thanks Sarbjit. That is working fine. Now what I want is ..

Initial file is like this..
Code:
Last update date $$IncludeDateTime = 04/18/2012 11:38:55

After executing the command
Code:
cat file1  | sed 's#\(\.*IncludeDateTime\).*#\1#'

The out put is like this..
Code:
Last update date $$IncludeDateTime =

Now The third step is what I want is ..
Code:
Last update date $$IncludeDateTime = 01/01/12 10:12:10

I tried using the following command..
Code:
cat file1 | sed 's#\(\.*$$IncludeDateTime =\).*#\1#' | sed "s/IncludeDateTime =/IncludeDateTime = $(echo ${STR1/
/\//\\\/})/g" file1

The output I am getting is ..
Code:
Last update date $$IncludeDateTime = 04/18/2012 11:38:55 = 01/01/12 10:12:10

But what I want is..

Code:
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.
Moderator's Comments:
Mod Comment How to use code tags

Last edited by Scrutinizer; 04-20-2012 at 04:59 AM.. Reason: code tags
# 5  
Old 04-20-2012
Code:
# sed "s|\(.*IncludeDateTime = \).*|\1$STR1|" file
Last update date $$IncludeDateTime = 01/01/12 10:12:10

regards
ygemici
# 6  
Old 04-20-2012
Thanks ygemici,

The following query worked fine to me.
Code:
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

Last edited by Franklin52; 04-20-2012 at 05:36 AM.. Reason: Code tags
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

How to find a string in a line in UNIX file and delete that line and previous 3 lines ?

Hi , i have a file with data as below.This is same file. But actual file contains to many rows. i want to search for a string "Field 039 00" and delete that line and previous 3 lines in that file.. Can some body suggested me how can i do using either sed or awk command ? Field 004... (7 Replies)
Discussion started by: vadlamudy
7 Replies

2. UNIX for Dummies Questions & Answers

find string and get the rest of the line in a pipe delimited file

Hi friends, I have a file where I should search for a string and get the rest of the line but without the delimiter using awk. for example I have the series of string in a file: input_string.txt bbb ccc aaa and the mapping file looks like this. mapping.txt aaa|12 bbb|23 ccc|43... (11 Replies)
Discussion started by: kokoro
11 Replies

3. Shell Programming and Scripting

awk delete/remove rest of line on multiple search pattern

Need to remove rest of line after the equals sign on search pattern from the searchfile. Can anybody help. Couldn't find any similar example in the forum: infile: 64_1535: Delm. = 86 var, aaga 64_1535: Fran. = 57 ex. ccc 64_1639: Feb. = 26 (link). def 64_1817: mar. = 3/4. drz ... (7 Replies)
Discussion started by: sdf
7 Replies

4. Shell Programming and Scripting

Find and delete rest of the line

i need to find ' - ' in a line , if i found i need to rest of the line including '-' example libmm-2.0 libytytsunos-5.6 output libmm libytytsunos (7 Replies)
Discussion started by: girija
7 Replies

5. Shell Programming and Scripting

Delete rest of line with sed works on Linux but not on Solaris

Hi all, Our application installation uses "sed" command to delete rest of line. It work perfect on Linux but fail on Solaris. The OS versions are Solaris 9 and Linux Red Hat AS 3. yourfile.txt hello and world cat and dog hello world in linux: cat yourfile.txt | sed ‘s/\(\+\)... (3 Replies)
Discussion started by: javac2005
3 Replies

6. UNIX for Dummies Questions & Answers

How to delete one line from a text file

PATTERN="" sed "/$PATTERN/d" $file In the file data is stored as ::: ::: I need to delete only the line containing the PATTERN pls help to point out why the above code is not working (4 Replies)
Discussion started by: thomsandy
4 Replies

7. UNIX for Dummies Questions & Answers

Delete line in text file

Hi, How do I go about deleting a line in a text file. I have used grep to find a particlular word, now how do I delete the line? so far I got: grep -i $keyword file.txt (6 Replies)
Discussion started by: Berserk
6 Replies

8. UNIX for Advanced & Expert Users

Delete rest of the line

Hi, can anyone please answer my question in deleting the rest of the line. I have an example below of a file contaning: Serial3/1.5 43.70.195.13 YES NVRAM down down Serial3/3 225.94.155.69 YES NVRAM up down Serial3/6 ... (3 Replies)
Discussion started by: Aejaz
3 Replies

9. Shell Programming and Scripting

Delete first line from any text file ?

What command I can use to delete first line from any text file ? Thk (5 Replies)
Discussion started by: aungomarin
5 Replies

10. Shell Programming and Scripting

delete last line from text file

I have a file with which i must remove the last line of the file (the footer). I know how to copy and redirect with tail -1 etc, but how do i delete it permanentely (4 Replies)
Discussion started by: hcclnoodles
4 Replies
Login or Register to Ask a Question