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

simple mapping of two files -> map.awk

How to map to files using awk.


# save the below code to a file named as "map.awk"
# start from here
BEGIN{
FS=OFS="|";
while(getline<ARGV[0]>1)
{
arr[$1]=$0
}
delete ARGV[0];
}
{
if($5 in arr)
print $0 FS arr[$5]
else
print $0 FS
}
#end here


Run the file as below
nawk -f map.awk file1 file2 > file3

File3 will have all the rows from file2 along with column1 of the file1 where ever column5 of file2 matches.