Имеются 3 файла, содержащие следующие данные:
file1.txt
Heading 1
Text 1
Text 2
file2.txt
Heading 2
Text 1
file3.txt
Heading 3
Text 1
text 2
Text 3
Ожидаемые результаты:
Heading 1,Text 1,Text 2
Heading 2,Text1
Heading 3,Text 1,text 2,Text 3
Это достигается с помощью программы createcsv.awk, приведенной ниже, которая вызывается как
gawk -f createcsv.awk file1.txt file2.txt file3.txt
createcsv.awk
{
if (1 == FNR) {
# It is the first line of a new file
if (csvline != "") {
# First file or empty files we can ignore
print csvline;
}
csvline = "";
delimiter = "";
}
csvline = csvline delimiter $0;
if ("" == delimiter) { delimiter="," }
}
END{
print csvline;
}