Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

There are many-many files in a directory that has e.x.: the following content:

INPUT:

root    1324711901
sshd    1272725792

And I'm searching for a solution how to convert the content of the files to this:

OUTPUT1:

filenameXXX.txt root    1324711901
filenameXXX.txt sshd    1272725792

OUTPUT2:

filenameXXX.txt root    Sat Dec 24 08:31:41 CET 2011
filenameXXX.txt sshd    Sat May  1 16:56:32 CEST 2010

So two things needed in two phases:

INPUT -> "filenameXXX.txt " before the first column -> OUTPUT1

OUTPUT1 -> convert unix times to normal times -> OUTPUT2

How to do this using bash OR sed OR perl? [don't need in all only 1 is enough :D]

ehh..and I need it in two part [so two one-liner]:
- INPUT to OUTPUT1
- OUTPUT1 to OUTPUT2

Field separators are "\t"-s [tabs]!

share|improve this question
    
Is the first field (that looks like usernames) guaranteed to not contain spaces? –  Ulrich Schwarz Nov 4 '11 at 6:34
    
no, it doesn't contain any spaces –  LanceBaynes Nov 4 '11 at 16:38

1 Answer 1

up vote 5 down vote accepted

I prefer gawk for this:

awk -vOFS='\t' 'NF{$1=FILENAME OFS $1;$2=strftime("%c",$2)}1' filename.txt

Here is one perl alternative too:

perl -nae 'print$ARGV,"\t",$F[0],"\t".localtime($F[1]),"\n"' filename.txt

As you also asked about bash, here is what it could do:

while read -r who when; do
  readlink -n /proc/$$/fd/0
  echo -en "\t$who\t"
  date -d "@$when"
done < filename.txt

Regarding sed, its usage would be hard and the benefit would be insignificant as it is unable to tell the name of its input file and to convert date.

share|improve this answer
    
field separators are "tabs" –  LanceBaynes Nov 4 '11 at 16:39
    
your a living GOD :) [question updated! sry!] –  LanceBaynes Nov 4 '11 at 16:42
    
Answer updated too. –  manatwork Nov 4 '11 at 17:12
    
@manatwork: Thanks; a good answer!...I like that method of getting the filename from fd/0.. Will it always return the fully-qualified path? –  Peter.O Nov 5 '11 at 0:45
    
@fered, sadly man 5 proc not mentions that. Other symlinks like /dev/fd/0 and /dev/stdin usually point directly to /proc/$$/fd/0. Other symlinks like /proc/self/fd/0 indirectly points to the same file: actually /proc/self/ points to /proc/$$/. So many links in different places pointing to each other, would be hard to follow in case of relative links. So I would never expect to find relative links there. Of course, all these /proc/ and /dev/ related things are system specific in some aspects, may not apply exactly to Unix and BSD. –  manatwork Nov 5 '11 at 12:28

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.