?
Solved

Apply an external function to a SED backreference

Posted on 2005-03-18
5
Medium Priority
?
518 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
  • 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

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. …
Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
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.
How can you see what you are working on when you want to see it while you to save a copy? Add a "Save As" icon to the Quick Access Toolbar, or QAT. That way, when you save a copy of a query, form, report, or other object you are modifying, you…
Suggested Courses
Course of the Month9 days, 17 hours left to enroll

571 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