我有很多文件如下(所有文件都有相同的概念)
more list-OS23
linux-d
linux-01 fedora-linux-c
Solaris_machine1
AIX-65 linux-redhat18 unixware7-h unix-digital
linux56-a linuxr9
我的目标是将这些行缩进为以下格式顺序
more list-OS23
cycel 1 cycel2 cycel3 cycel4
linux-d fedora-linux-c unixware7-h unix-digital
linux-01 linux-redhat18
Solaris_machine1 linuxr9
AIX-65
linux56-a
我尝试用awk缩进文件
awk '{print $1" "$2" "$3}' file
但缩进不像我预期的那样
我可以获得如何缩进文件的想法或真实示例吗?
我认为printf很适合这个建议
答案 0 :(得分:1)
$ cat tst.sh
awk '
{
for (i=1;i<=NF;i++) {
rowNr = ++rowNrs[i]
colNr = ++colNrs[rowNr]
vals[rowNr,colNr] = $i
numCols = (NF > numCols ? NF : numCols)
}
}
END {
for (colNr=1; colNr <= numCols; colNr++) {
printf "cycle%s%s", colNr, (colNr<numCols ? OFS : ORS)
}
for (rowNr=1; rowNr <= NR; rowNr++) {
for (colNr=1; (rowNr,colNr) in vals; colNr++) {
printf "%s%s", (colNr>1 ? OFS : ""), vals[rowNr,colNr]
}
print ""
}
}
' "$1" | column -t
$ ./tst.sh file
cycle1 cycle2 cycle3 cycle4
linux-d fedora-linux-c unixware7-h unix-digital
linux-01 linux-redhat18
Solaris_machine1 linuxr9
AIX-65
linux56-a
答案 1 :(得分:0)
这样的事可能在AWK中:
$ cat > test.awk
BEGIN {
FS="( +)" # input field separator is a bunch of spaces
OFS="\t" # output separator is tab
}
NR==1 { # just print the first line as is
print $0
}
{ # the rest are re-delimited and printed
$1=$1
print $0
}
$ awk -f test.awk your_input.in
或作为单行:
awk 'BEGIN{FS="( +)";OFS="\t"}NR==1 {print $0;}{$1=$1;print $0}' your_input.in
答案 2 :(得分:0)
owner