LinuxQuestions.org
Review your favorite Linux distribution.
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 11-14-2012, 08:11 PM   #1
yadam
LQ Newbie
 
Registered: Nov 2012
Posts: 2

Rep: Reputation: Disabled
Is it possible to use the matched string in sed as an input to shell date command?


Hi,
I am a newbee and appreciate any help I can get. I have been spending couple of hourse searching the net to find my answer, but can not find any post that includes answer to this question.
I have a file with records having timestamp fields that include GMT offset. I want to use the sed command to replace the value on the record to a regular timestamp (without GMT offset).
for ex.
date -d '2012/11/01 00:50:22 -0800' '+%Y-%m-%d %H:%M:%S'
returns this value which is what I am looking to do:
2012-11-01 01:50:22

Except I want to perform that operation on every line of this file and apply the date command to the timestamp value. Here is a sample record:
"SB","6GV96644X48128125","","","","T0006",2012/10/03 13:08:43 -0700,"NJ"

Here is my code:
head -1 myfile | sed 's/,[0-9: /\-]\{25\},/,'"`date -d \1 '+%Y-%m-%d %H:%M:%S'`"',/
which doesn't work: it just ignores \1 and replaces the matched pattern with today's date!
"SB","6GV96644X48128125","","","","T0006",2012-11-14 01:00:00,"NJ"
I hoped that \1 would result in the matched patterns be passed to the date function and return a regular timestamp value (as in the example I provided above showing how the date functions applies the GMT offset and returns a regular stimestamp string) and would replace the old value on the record.

REally appreciate your help,
Yadam

Last edited by yadam; 11-14-2012 at 08:15 PM.
 
Old 11-15-2012, 01:35 AM   #2
linosaurusroot
Member
 
Registered: Oct 2012
Distribution: OpenSuSE,RHEL,Fedora,OpenBSD
Posts: 979
Blog Entries: 2

Rep: Reputation: 235Reputation: 235Reputation: 235
You don't seem to be capturing \1 using \(\) on the left hand side.
 
Old 11-15-2012, 01:37 AM   #3
linosaurusroot
Member
 
Registered: Oct 2012
Distribution: OpenSuSE,RHEL,Fedora,OpenBSD
Posts: 979
Blog Entries: 2

Rep: Reputation: 235Reputation: 235Reputation: 235
How about this?
Code:
#!/usr/bin/perl

open(M, "<myfile") or die("open M $!");
while (<M>) {
 @f=split(",");
 $newdate=`date -d "$f[6]" "+%Y-%m-%d %H:%M:%S"`;
 chomp($newdate);
 $out=join(",",  (@f[0..5], $newdate, $f[7]));
 print $out;
}
close(M);
 
Old 11-16-2012, 01:59 AM   #4
yadam
LQ Newbie
 
Registered: Nov 2012
Posts: 2

Original Poster
Rep: Reputation: Disabled
I posted the same question on a different site and got the following response which does exactly what I needed to do:

awk '{cmd="date -d \""$7"\" \"+%Y-%m-%d %H:%M:%S\"";
cmd | getline k; $7=k; print}' FS=, OFS=, myFile
 
  


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



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Use same input string twice with sed? N0hAT Linux - Newbie 11 10-09-2012 10:03 AM
Replace string using sed command bloodstreetboy Linux - General 6 07-06-2012 04:03 PM
Using sed in a shell script to add date and time seefor Programming 3 02-25-2009 02:21 PM
sed command to replace date format 2007/01/22 with 070122? johnpaulodonnell Linux - Newbie 3 01-23-2007 08:29 AM
Inputting with sed, based on matched pattern mike9287 Programming 5 07-20-2006 08:49 AM


All times are GMT -5. The time now is 03:38 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration