0
我想通過在不同文件上列出的名稱來提取某些列。Linux - 限制文本的長度
我發現這個有用的代碼如下面的鏈接:AWK extract columns from file based on header selected from 2nd file
我試圖編輯一些打印頭和限制頭的長度。
#!/bin/bash
DATAFILE=${1:-data.txt}
COLUMNFILE=${2:-list.txt}
awk -v colsFile="$COLUMNFILE" '
BEGIN {
j=1
while ((getline < colsFile) > 0) {
col[j++] = $1
}
n=j-1;
close(colsFile)
for (i=1; i<=n; i++) s[col[i]]=i
}
NR==1 {
for (f=1; f<=NF; f++)
if ($f in s) c[s[$f]]=f
printf ${$f:0:3}
}
{ sep=""
for (f=1; f<=n; f++) {
printf("%c%s",sep,$c[f])
sep=" "
}
print ""
}
' "$DATAFILE"
我在鏈接之前使用了相同的例子。
$ cat data.txt
ID,head1,head2,head3,head4
1,25.5,1364.0,22.5,13.2
2,10.1,215.56,1.15,22.2
$ cat list.txt
ID
head1
head4
$ dataExtractor.sh data.txt list.txt
1,25.5,13.2
2,10.1,22.2
與輸出我要的是
ID,hea,hea
1,25.5,13.2
2,10.1,22.2
後,我編輯的代碼,包括$ {$ F:0:3},它給了我一個語法錯誤。 請幫我這個,謝謝!