1

I have a file in the following format:

"2004-04-19 12:25:57" 44 44
"2004-04-19 13:39:32" 36 36
"2004-04-19 14:00:53" 34 34

I need 2 new files:

a) A file which will replace "time" values of the first column of my file with numbers starting from 1, like this:

1 44 44
2 36 36
3 34 34

b) Another file which will replace "time" values of the first column of file with numbers unix tamestamp data, like this:

1082377557 44 44
1082381972 36 36
1082383253 34 34
4

You can use this bash one liner :

i=1; while IFS=' ' read a b c; do echo "$i $c" >>foo.txt; ((i+=1)); \
     echo "$(date -d "${a#\"} ${b%\"}" '+%s')" "$c" >>bar.txt; done <file.txt

Expanded form :

i=1
while IFS=' ' read a b c; do 
    echo "$i $c" >>foo.txt
    ((i+=1))
    echo "$(date -d "${a#\"} ${b%\"}" '+%s')" "$c" >>bar.txt
done <file.txt

After the operation foo.txt will have :

1 44 44
2 36 36
3 34 34

and bar.txt will have :

1082377557 44 44
1082381972 36 36
1082383253 34 34
  • foo.txt was created right, but bar.txt isn't getting filled with anything, instead i get following output at shell....................................................... -bash: echo: command not found usage: date [-jnu] [-d dst] [-r seconds] [-t west] [-v[+|-]val[ymwdHMS]] ... [-f fmt date | [[[mm]dd]HH]MM[[cc]yy][.ss]] [+format] -bash: echo: command not found usage: date [-jnu] [-d dst] [-r seconds] [-t west] [-v[+|-]val[ymwdHMS]] ... [-f fmt date | [[[mm]dd]HH]MM[[cc]yy][.ss]] [+format] -bash: echo: command not found ... – Haos Sep 4 '15 at 0:36
  • @Haos I have tested this in my Ubuntu 14.04..works fine..what OS are you using ? what does type -a echo say ? – heemayl Sep 4 '15 at 0:40
  • echo is a shell builtin echo is /bin/echo echo is /bin/echo – Haos Sep 4 '15 at 1:29
  • it seems there is a problem with date command in Mac – Haos Sep 4 '15 at 1:30
1

awk:

awk '{
    # store the time value (first 2 words)
    timestamp = $1 " " $2

    # shift the other fields 2 places (I wish this was simpler in awk)
    for (i=3; i<=NF; i++) $(i-2) = $i
    NF -= 2

    # print to the line-numbers file
    print NR, $0  > "file1"

    # convert the timestamp and print to that file
    gsub(/[-:"]/, " ", timestamp)
    print mktime(timestamp), $0   > "file2"
}' file

mktime requires GNU awk (I think).

perl:

perl -MTime::Piece -anE '
    BEGIN {
        $, = " "; 
        open $f1, ">", "file1"; 
        open $f2, ">", "file2"
    } 
    $date = shift @F; 
    $time = shift @F; 
    say $f1 $., @F; 
    say $f2 Time::Piece->strptime("$date $time", "\"%Y-%m-%d %H:%M:%S\"")->epoch, @F
' file
0

Well at the risk of doing your homework for you. Here you are.

Assuming your data is in a file named YOURFILENAME this first oneliner with add the line numbers and the two last fields from the file

count=1;cut -d" " -f 3,4 YOURFILENAME| while read line ; do echo $count $line;((++count)); done

This second one-liner will convert your dates to epochs and print out the rest of the line (had to add one more sed to get rid of the quote marks but I did this quick and dirty)

cut -d"\"" -f2 YOURFILENAME| while read line; do SWAP=$(date -d "$line" +\%s); sed -i "s/$line/$SWAP/g" YOURFILENAME;done ; sed 's/"//g' YOURFILENAME

Please realize that this is just one way that you can do this. There are probably quite a few more.

0

I'd do it like this in perl:

#!/usr/bin/env perl
use strict;
use warnings;
use Time::Piece;

#open our files for output
open( my $output1, '>', "output_file_one.txt" ) or die $!;
open( my $output2, '>', "output_file_two.txt" ) or die $!;

#iterate the magic filehandle - <> - which reads either data piped from
#stdin, or opens files specified on command line. (Just like grep/awk/sed)
while (<>) {

    #regex match the values out of your source file.
    my ( $t, @values ) = m/^\"(.*)\" (\d+) (\d+)/;

    #convert $t into a time object.
    $t = Time::Piece->strptime( $t, "%Y-%m-%d %H:%M:%S" );

    #use the "epoch" method to extract the numeric time from $t
    print {$output1} join( " ", $t->epoch, @values );

    # $. is the perl special var for current line number.
    print {$output2} join( " ", $., @values );
}

close($output1);
close($output2);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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