Поскольку трудно определить, к какому столбцу относится строка, я делаю следующее предположение:
- Столбцы идеально выровнены и разделены пробелами
поэтому следующий сценарий awk примет:
- Строка, которая не начинается с даты, будет объединена в следующую строку
- Ширина столбцов будет определяться шириной столбца следующая строка
примечание: , если ваш файл выровнен попробелы (комбинация табуляций и пробелов), мы не можем использовать разделитель полей "\ t", чтобы различать поля, так как количество вкладок будет зависеть от ширины поля.
Вот проверенный скрипт:
# If you have a tab-aligned file, replace all tabs by the
# correct sequence of spaces. In this example, we assume a single
# tab is equivalent to 8 spaces. Adopt if needed
{ gsub(/\t/," ",$0) }
# If the line does not start with a combination of numbers and hyphens
# it is a line that needs to be merged into the next line.
# store it and move to the next line
($1 !~ /^[-0-9]+$/) { tomerge=$0; next }
# If we picked up a tomerge line, try to figure out the fields
# by looking into the current line and determining the field widths.
(tomerge != "") {
# extract fields
n=1
for(i=1;i<NF;++i) {
m=index($0,$(i+1))
field[i]=substr(tomerge,n,m-n)
sub(/^[[:blank:]]*/,"",field[i]) # remove leading blanks
sub(/[[:blank:]]*$/,"",field[i]) # remove trailing blanks
n=m
}
field[NF]=substr(tomerge,n)
# perform merging
for(i=1;i<=NF;++i) $i= field[i] $i
# reset the tomerge value
tomerge=""
}
# print the line
{ $1=$1;print $0 }
который выводит:
$ awk -f script.awk file.txt
02-10-2018 11:50 linux-is-a-opensource user file
02-10-2018 11:46 linux-userfile user file1
02-10-2018 11:40 linux-userfile user-1user file2
02-10-2018 11:30 linux-linux-userfile user-2user file3
Если вы хотите выровнять его, вы можете передать его в column -t
как
$ awk -f script.awk file.txt | column -t