?
Solved

Apply an external function to a SED backreference

Posted on 2005-03-18
5
Medium Priority
?
502 Views
Last Modified: 2013-12-26
Hello,

I have a small shell script I intend to use to replace time stamps from a log file with readables dates :

1111054703 ConnStartListen starting thread ConnStartListen for 127.0.0.1
1111054703 iplud bound to address 0.0.0.0
1111054703 ipludAddMembership adding membership for 0.0.0.0

to

jeu 17 mars 2005 - 01:00:01 ConnStartListen starting thread ConnStartListen for 127.0.0.1
jeu 17 mars 2005 - 01:02:01 iplud bound to address 0.0.0.0
jeu 17 mars 2005 - 01:03:01 ipludAddMembership adding membership for 0.0.0.0

here's the script
-----------------------------
#!/bin/sh

#the date_time stamp functions returns a french date like "jeu 17 mars 2005 - 01:00:01" from a time stamp when called
date_timestamp()
 {
   date -u --date "Jan 1, 1970 00:00:00 -0100 + $1 seconds" | sed 's/ UTC//' | awk '{print $1" "$3" "$2" "$5" - "$4}'
 }

                                                             #date_timestamp \1 does not work below
tail /var/log/my.log | sed "s/\(^[0-9]*\):/`(date_timestamp \1)`/g"

exit 0
-----------------------------

The problem is that the last sed expression sends the first back reference \1 litteraly to the date_time function and not its value so the date_timestamp function receives \1 instead of 1111054709 for example.

So i just get

jeu 1 jan 1970 - 01:00:01 ConnStartListen: starting thread ConnStartListen for 195.137.248.170:0
jeu 1 jan 1970 - 01:00:01 connAccept: close(socket) failed with error 9
jeu 1 jan 1970 - 01:00:01 Retrospect Client Terminated.

Any clue to fix this ?

I might precise that many of you would will indicate me to use perl for this kind of stuff... but I intend to do this using shell script only just for the pleasure of knowledge ;-)

So the solution MUST use shell commands only ;-)

thanks for your help

0
Comment
Question by:FFT
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 45

Accepted Solution

by:
sunnycoder earned 2000 total points
ID: 13572500
Hi FFT,

#!/bin/sh

#the date_time stamp functions returns a french date like "jeu 17 mars 2005 - 01:00:01" from a time stamp when called
date_timestamp()
 {
   date -u --date "Jan 1, 1970 00:00:00 -0100 + $1 seconds" | sed 's/ UTC//' | awk '{print $1" "$3" "$2" "$5" - "$4}'
 }

tail /var/log/my.log | while read line
do
      pre=`echo $line | sed 's/\(^[0-9]*\).*/\1/'`
      post=`date_timestamp $var`
      echo $line | sed "s/^[0-9]*/$post/"
done

Cheers!
sunnycoder
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 13572744
perl -MPOSIX -lane '$F[0]=strftime("%D %T",localtime($F[0])); print join(" ",@F)' yourfile
# feel free to improve the format of strftime ;-)
0
 

Author Comment

by:FFT
ID: 13572916
Ok sunnycoder, it worked fine thanks very much !

for the record the "true" working script is as follow :
--------------------------------
#!/bin/sh

date_timestamp()
{
      date -u --date "Jan 1, 1970 00:00:00 -0100 + $1 seconds" | sed 's/ UTC//' | awk '{print $1" "$3" "$2" "$5" - "$4}'
}

tail /var/log/my.log | while read line
do
      pre=`echo $line | sed 's/\(^[0-9]*\).*/\1/'`
      post=`date_timestamp $pre`
      echo $line | sed "s/^[0-9]*/$post/"
done

exit 0
--------------------------------
PS : thanks to ahoffmann but I asked it to be in pure shell with no perl ;-)
0
 
LVL 45

Expert Comment

by:sunnycoder
ID: 13572937
:-)
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 13573043
> .. pure shell ..
sed, date, awk, tail is as "pure" as perl
;-)
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Suggested Courses

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question