LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (http://www.linuxquestions.org/questions/linux-general-1/)
-   -   how to search and replace character from middle of line (http://www.linuxquestions.org/questions/linux-general-1/how-to-search-and-replace-character-from-middle-of-line-672544/)

vishal_titre 09-26-2008 11:18 AM

how to search and replace character from middle of line
 
Hi ,

I have one xml file which is having some lines like below
<StartDate>2008-05-07T09:45:48.000000000-05:00</StartDate>
<EndDate>2008-05-07T09:45:48.123456789-05:00</EndDate>

in above lines there is one number of 9 digits ( in blue color) . i want only first three digits out of this 9 digit and rest of the line remain uncganged .


please suggest me the solution for this

Thank you
Vishal

TB0ne 09-26-2008 11:46 AM

Quote:

Originally Posted by vishal_titre (Post 3292543)
Hi ,

I have one xml file which is having some lines like below
<StartDate>2008-05-07T09:45:48.000000000-05:00</StartDate>
<EndDate>2008-05-07T09:45:48.123456789-05:00</EndDate>

in above lines there is one number of 9 digits ( in blue color) . i want only first three digits out of this 9 digit and rest of the line remain uncganged .

please suggest me the solution for this

You don't say what you want to write this in. You could do it with a bash script, Perl, Python, Ruby, or pretty much any other programming language you can think of.

This page http://tldp.org/LDP/abs/html/string-manipulation.html, will give you a start for a bash script.

cmnorton 09-26-2008 11:48 AM

C, Perl, or Awk (and others)
 
Suggest you play around with C, Perl, or Awk. Python has nice string handling, too, but I do not use it.

You'll want to pull these lines apart, and then re-assemble them with the characters you want replaced.

From my experience, Perl has one of the strongest string/regular expression handling facilities. If you can quantify these strings into unique fields of some type, then Awk would be a good solution. C is always a good standby, but you'll do a little more work.

gumuruhsspj 09-27-2008 02:27 PM

well surely what u need ...
 
is about String modification
yes use the Shell Scripting
or perhaps others like Tcl/TK

here

Quote:

Tcl/Tk is a portable scripting environment for Unix, Windows, and Macintosh.

Tcl provides a portable scripting environment for Unix, Windows, and Macintosh that supports string processing and pattern matching, native file system access, shell-like control over other programs, TCP/IP networking, timers, and event-driven I/O.

Tcl has traditional programming constructs like variables, loops, procedures, namespaces, error handling, script packages, and dynamic loading of DLLs. Tk provides portable GUIs on UNIX, Windows, and Macintosh.

A powerful widget set and the concise scripting interface to Tk make it a breeze to develop sophisticated user interfaces.

Tcl (Tool Command Language) is easy to learn and you can create a useful program in minutes! You are free to use Tcl/Tk however you wish, even in commercial applications.

with a specific more details you may check this :D


Kenhelm 09-27-2008 04:49 PM

I'm not sure if this is what you want; it just deletes the first three characters after the first '.' in lines starting with either <StartDate> or <EndDate>
Code:

sed '/^<StartDate>\|^<EndDate>/ s/\...././' infile > outfile

vishal_titre 09-29-2008 08:18 AM

Thank you all for your reply .

Kenhelm ,

I don't want to delete first three digit out of nine , but want to keep firts three digits and want to delete rest of the six digits and keep rest of the line asitis .

plese tell me the solution
Thank you all again

pixellany 09-29-2008 08:35 AM

sed 's/\.\(...\).*-/\.\1-/' filename

matches the pattern: ".", then 3 chars, then any number of chars, then "-"
replaces with: ".", then 3 chars then "-"

The "3 chars" are re-inserted using a "backreference".

vishal_titre 09-30-2008 03:46 AM

Thank pixellany

It working
Thank you Again


All times are GMT -5. The time now is 09:10 PM.