I have following file which I am splitting into multiple files using awk script

Name of original File: RTLOG_5019_05122016110959.DAT

I want to set the name of split files as below:

RTLOG_5019_<timestmap (format: mmddyyyyhhmiss) >.DAT.

How do I achieve this? I don't want to hard-code the filename and also make filename-RTLOG_5019_05122016110959.DAT as parameter

Following is my awk script

awk -v stamp=$(date +%Y%m%d%H%M%S)  '
/^FHEAD/        {HD = $0  

/^THEAD/        {if (FN)  { printf "FTAIL%010d%010d" ORS, LN+2, LN > FN
                        close (FN)
                        LN = 0
             FN = "RTLOG_5019_" stamp ".DAT" ++f
             print HD > FN

            !/^FTAIL/ {print >> FN

END             {printf "FTAIL%010d%010d" ORS, LN+2, LN > FN
            } ' RTLOG_5019_05122016110959.DAT
  • A bit unrelated, but I'd highly suggest using yymmdd instead of ddmmyy in any filename that contains a date because it will make it possible to browse the files in sorted order – V13 Dec 13 '16 at 9:19

I'm not sure what you are trying to do, exactly, but this might help:

 awk -v var="new" 'BEGIN { print substr(ARGV[1], 0, 2) var substr(ARGV[1], 6); }' myoldfilename
  • Actually , I want to set the filename as RTLOG_5019_timestamp where RTLOG_5019 is derived from filename RTLOG_5019_05122016110959.DAT – Amit Dec 13 '16 at 8:59
  • So, something like awk -v var="$(date +%Y%m%d%H%M)" 'BEGIN { print substr(ARGV[1], 0, 10) var; }' RTLOG_5019_05122016110959.DAT – Michael Vehrs Dec 13 '16 at 9:08

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.