Awk 用文件名替换列名

Awk 用文件名替换列名,awk,sed,Awk,Sed,我在一个目录中有多个文本文件,我希望根据文件名更改列名 > Text1.out - filename counts -column name apples pears oranges 所需输出 > Text1.out - filename Text1_counts -column name (change column name to filename using the string before the . ) apples pears oranges

我在一个目录中有多个文本文件,我希望根据文件名更改列名

 > Text1.out - filename
 counts  -column name
 apples
 pears
 oranges 
所需输出

> Text1.out - filename
 Text1_counts  -column name (change column name to filename using the string before the . )
 apples
 pears
 oranges
我试过了

 awk -F, 'NR==1{split(FILENAME,a,".");split($1,b,"(");$1= a[1] "_" b[1]}1' Text1.out >Text1.out.counts
这是可行的,但当我尝试使用awk inplace处理多个文件时,会出现错误

awk -i inplace -F, 'NR==1{split(FILENAME,a,".");split($1,b,"(");$1= a[1] "_" b[1]}1' *.out
错误消息,或者更确切地说,它指出我的用法是错误的

Usage: awk [POSIX or GNU style options] -f progfile [--] file ...
Usage: awk [POSIX or GNU style options] [--] 'program' file ...

任何其他方法来重命名它也可以。我认为我的终端不支持awk就地替换。

也许
sed
是一个更好的选择

$ for f in file*; do sed -i '1s/.*/'"$f"'_&/' "$f"; done

在这里,
sed
也许是更好的选择

$ for f in file*; do sed -i '1s/.*/'"$f"'_&/' "$f"; done

用我收到的信息更新帖子。不是真正的错误消息,所以以前没有包含。FNR并没有使用我收到的消息更新帖子。不是真正的错误消息,所以以前没有包含。FNR不起作用