Hi,
I am splitting file on each HEADER. I am using following command.
awk '/^HEADER/{close("file"f);f++}{print $0 > "FILE_"f}' $Filename
I want to use a variable while generating output file. like in above command, splitted files have name "FILE_1", "FILE_2" , "FILE_3" and so on.
I dont want to use "FILE_", I want some variable that I can use instead ... like DATETIME or any run time value. I tried placing variables in place of "FILE_" but it was not working.
Please advice how can I do this.
Thanks.
Try this.
var=`date +"%d%m%Y"`
awk '/^HEADER/{close("file"f);f++}{print $0 > '$var'"_FILE_"f}' $Filename
Try this:
awk -v var=`date +"%d%m%Y"` '/^HEADER/{close(var"_"f);f++}{print $0 > var"_"f}' file
Hi Franklin52,
Your code generates only one file whereas it should generate 4 files.
Hi lathavim,
I tried with var=`date +"%m%d%y%H%M%S"` in your code and the filename generated was "7.2709e+10_FILE_1" (exponential value)
also it working with numbers not with Characters...
Do you have the word "HEADER" on the start of every header? Post a sample of your input file within code tags.
Actually file is really big so posting few Lines ....
My awk solution works on my system with the given sample (output 2 files). Try nawk or /usr/xpg4/bin/awk on Solaris.
Regards
I am on Solaris and tried /usr/xpg4/bin/awk but it generated only one output file.
any idea how we can use string variables... its taking only numbers.