LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 09-26-2008, 10:18 AM   #1
vishal_titre
LQ Newbie
 
Registered: Jul 2006
Location: Pune
Distribution: Redhat
Posts: 22

Rep: Reputation: 15
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
 
Old 09-26-2008, 10:46 AM   #2
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,604

Rep: Reputation: 7960Reputation: 7960Reputation: 7960Reputation: 7960Reputation: 7960Reputation: 7960Reputation: 7960Reputation: 7960Reputation: 7960Reputation: 7960Reputation: 7960
Quote:
Originally Posted by vishal_titre View Post
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.
 
Old 09-26-2008, 10:48 AM   #3
cmnorton
Member
 
Registered: Feb 2005
Distribution: Ubuntu, CentOS
Posts: 585

Rep: Reputation: 35
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.
 
Old 09-27-2008, 01:27 PM   #4
gumuruhsspj
LQ Newbie
 
Registered: Sep 2008
Posts: 24

Rep: Reputation: 15
Talking 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

 
Old 09-27-2008, 03:49 PM   #5
Kenhelm
Member
 
Registered: Mar 2008
Location: N. W. England
Distribution: Mandriva
Posts: 360

Rep: Reputation: 170Reputation: 170
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
 
Old 09-29-2008, 07:18 AM   #6
vishal_titre
LQ Newbie
 
Registered: Jul 2006
Location: Pune
Distribution: Redhat
Posts: 22

Original Poster
Rep: Reputation: 15
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
 
Old 09-29-2008, 07:35 AM   #7
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
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".
 
Old 09-30-2008, 02:46 AM   #8
vishal_titre
LQ Newbie
 
Registered: Jul 2006
Location: Pune
Distribution: Redhat
Posts: 22

Original Poster
Rep: Reputation: 15
Thank pixellany

It working
Thank you Again
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
search and replace with multi line string kambrish Programming 5 04-28-2008 06:02 AM
vim search and replace only one line hakmed Programming 5 11-07-2006 07:13 PM
Search and Replace with multiple-line strings ChristianNerds.com Programming 4 08-21-2005 02:32 PM
Perl - Tpl file - Need to replace new line character. knnirmal Programming 2 09-07-2004 02:27 PM
vim search and replace - split line phreak7t7 Programming 2 08-16-2004 05:02 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 04:22 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration