LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 01-22-2007, 08:41 AM   #1
johnpaulodonnell
Member
 
Registered: Jun 2006
Location: Galway
Distribution: Suse 10.1
Posts: 114

Rep: Reputation: 15
sed command to replace date format 2007/01/22 with 070122?


Hi.

I have a data file containing the following columns:

$1 $2 $3 $4
record no. date time data

The date format at present is: yyyy/mm/dd
I need to put the date in the format yymmdd, ie drop the leading two digits and then remove the backslashes.

The time format at present is: hh:mm:ss.ms
I need to put this in the format hhmmss, ie drop the milisec part and remove the colons.

I only know basic sed commands and can't figure this out...

Can the individual characters of a string be referenced in a shell script?
 
Old 01-22-2007, 08:58 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

This should work:

sed 's%[0-9][0-9]\([0-9][0-9]\)/\([0-9][0-9]\)/\([0-9][0-9]\)%\1\2\3%' infile

- You can change sed's separator (/) to anything you like, I used %. This to avoid escaping the / in the date string.
- Sed can use backreferencing. I.e. something in the searchstring surrounded by () (need to be escaped) can be represented by \1 (\2 \3 etc) in the replace string.

Take a look here (it's about halfway down the article) for another sed backreference explanation/example.

The same can be done for the time string, but that's for you to figure out

If anything is unclear or you cannot get it to work: Just ask.

Last edited by druuna; 01-22-2007 at 09:06 AM.
 
Old 01-23-2007, 06:39 AM   #3
johnpaulodonnell
Member
 
Registered: Jun 2006
Location: Galway
Distribution: Suse 10.1
Posts: 114

Original Poster
Rep: Reputation: 15
Thanks. Appreciate that.
 
Old 01-23-2007, 07:29 AM   #4
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
Quote:
Can the individual characters of a string be referenced in a shell script?
Do you mean put an individual character into a variable? Awk will do this, but I don't have the syntax in front of me.
 
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
'date' command format Neorio Linux - General 3 01-18-2011 05:18 AM
sed command to replace slash with backslash stelmed Slackware 13 08-19-2010 05:01 AM
sed - find and replace command bullshit Programming 9 01-05-2006 03:25 AM
replace empty function from the old format to the new format powah Programming 4 07-12-2005 08:07 PM
problem in perl replace command with slash (/) in search/replace string ramesh_ps1 Red Hat 4 09-10-2003 01:04 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 06:15 PM.

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