awk print re-direction to a file with timestamp appended


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk print re-direction to a file with timestamp appended
# 1  
Old 08-13-2012
awk print re-direction to a file with timestamp appended

Hello

I need to split big xml file into multiple files based on xml declaration. for that i have written one awk 1 liner as below

Code:
awk '/<?xml\ version/{i++}{print > "outfile."i}' test123.xml

this is producing the desired out put. but i want the the currenttimestamp with milliseconds in the file name rather than outfile as filename.

ex: outfile=currenttimestamp with milliseconds (YYYYMMDDHHMISSSS) format.

ex:2012080819371613.1

Please help me

Thanks
Dsdev123


Moderator's Comments:
Mod Comment Please use code tags next time for your code and data.

Last edited by zaxxon; 08-14-2012 at 05:25 AM.. Reason: code tags
# 2  
Old 08-14-2012
If your local date command supports the rfc 3339 option, then this brute force way works:


Code:
awk '
    function ts(    c )     # timestamp with nano sec
    {
        c = "date --rfc-3339=ns | sed -E \"s/(..)-(..)-/\1\2/;s/ //; s/-.*//;s/://g\"";
        c | getline timestamp;
        close( c );
    }

    /<?xml\ version/{ ts(); }{ print >"outfile." timestamp; } ' in-file

# 3  
Old 08-14-2012
I tried this but its not working . iam using AIX 6.1.

Thanks again
# 4  
Old 08-14-2012
Quote:
Originally Posted by dsdev_123
I tried this but its not working . iam using AIX 6.1.

Thanks again

Assuming that you don't have GNU date available then. You could write a small bit of C and invoke the binary:


Code:
#include <unistd.h>
#include <stdio.h>
#include <time.h>
#include <sys/time.h>

int main( )
{
    struct tm ltime;
    struct timeval tv;
    time_t  now;

    now = time( NULL );
    localtime_r( &now, &ltime );
    gettimeofday( &tv, NULL );
    printf( "%4d%02d%02d%02d%02d%02d.%d\n", ltime.tm_year + 1900, ltime.tm_mon + 1, ltime.tm_mday, ltime.tm_hour, ltime.tm_min, ltime.tm_sec, tv.tv_usec );

    return 0;
}

If programme is saved as datens.c then this command should build datens binary:


Code:
cc -o datens datens.c

This of course assumes your libc includes gettimeofday(). I don't have access to an AIX box, so I'm unsure what other options you might have.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Can I please get a push in the right direction with awk/sed?

Hi Guys, I have this task to monitor a linux box. I found a program that displays the parameters that I want and I wrote a little .sh to run that program and record output into a file. The findings look promising but I would like to graph them. My output (for every iteration) looks like... (3 Replies)
Discussion started by: DraxDomax
3 Replies

2. Shell Programming and Scripting

How to copy or cut specific rows from appended file with some conditions

Hi I have one file which is containing about 5000 rows and 20 columns I will just explain about my requirement here briefly with sample file, I have attached also, please help....me.. 1 28.25 36.42 5 28.26 36.42 10 28.23 36.43 15 28.22 36.43 20 28.2 36.42 25... (6 Replies)
Discussion started by: nex_asp
6 Replies

3. UNIX for Dummies Questions & Answers

Move txt file to with current date appended to filename

I have multiple txt files which begin with the word "orders" in folder C:\source. I need to move the files to folder C:\dest and rename them to "process_<date>_<count>" So for example , if there are 3 files ordersa.txt , ordersb.txt and ordersc.txt in C:\source , after running the script I want... (7 Replies)
Discussion started by: johannd
7 Replies

4. Shell Programming and Scripting

Move txt file to with current date appended to filename

I have multiple txt files which begin with the word "orders" in folder C:\source. I need to move the files to folder C:\dest and rename them to "process_<date>_<count>" So for example , if there are 3 files ordersa.txt , ordersb.txt and ordersc.txt in C:\source , after running the script I want... (1 Reply)
Discussion started by: johannd
1 Replies

5. Shell Programming and Scripting

Adding Header and Trailer records to a appended file

How can we a shell script and pass date parameters .I have 3 files comming from Datastage with |" delimited I need append 3 files as above: File1: P0000|"47416954|"AU|"000|"INS|"0000|"|"20060601|"99991231|"|"|"|"|"01 File 2:... (2 Replies)
Discussion started by: e1994264
2 Replies

6. Shell Programming and Scripting

Finding when a file switches direction using awk

Hi guys I have a file that is filled with x,y values: 0.000000 0.00129578 0.000191 0.00272187 0.000381 0.0125676 0.000572 0.0120014 0.000763 0.00203461 0.000954 0.00682248 0.001144 0.00202773 0.001335 0.000840523 0.001526 0.00451419 ....5MB of that I wanted to know if there is a... (7 Replies)
Discussion started by: bflinchum
7 Replies

7. UNIX for Dummies Questions & Answers

How to compare a file by its timestamp and store in a different location whenever timestamp changes?

Hi All, I am new to unix programming. I am trying for a requirement and the requirement goes like this..... I have a test folder. Which tracks log files. After certain time, the log file is getting overwritten by another file (randomly as the time interval is not periodic). I need to preserve... (2 Replies)
Discussion started by: mailsara
2 Replies

8. UNIX for Dummies Questions & Answers

Delete lines from a file where data is continously appended

Hello , Is there a way to delete lines from a file where data is continously appended to the file. I can use normal vi command ndd to remove n number of lines from the file, as the data is continously appended the line numbers doesnt work. (1 Reply)
Discussion started by: sophos
1 Replies

9. Shell Programming and Scripting

Find file using date appended to file name

The script below is a backup script I wrote. It Tars and gzips files and move them to backup folder. It appends the current date of action to the name of the .tar.gz file echo “Taking Domain Backup...” hostname echo “Tarring internetprod” sleep 2 cd /usr/web/bea/projects/domains tar... (4 Replies)
Discussion started by: joshuaadom
4 Replies

10. Linux

SED/AWK Script to clear log file using timestamp?

I have a log file on our system which fills up with lines that have been timestamped, as follows.... 03/03/2008 10:56:06:815] (ERROR) balance: continuing session to genapp02 : 18500 03/03/2008 10:56:06:820] (ERROR) balance: continuing session to genapp02 : 18500 03/03/2008 10:56:07:003]... (2 Replies)
Discussion started by: davesimm
2 Replies
Login or Register to Ask a Question