Monday, 7 July 2008

split each line from a file as a new file

Using awk if you would like to split each record/line into individual file with first line as header for all files, use below awk script.


#start code
BEGIN{
filename_counter = 10000;
}
{
if(FNR == 1)
header=$0
else
{
fname=filename_counter".txt"
print header > fname
print $0 >> fname
filename_counter++
}
}

#end Code


copy the above code and save as split_file.awk.

Run the script as below.

nawk -f split_file.awk file1

No comments: