linux 从多个文件中获取特定字段

linux get specific fields from several files

我有几个以字符串 "file" 开头然后是数字(file1、file2 等)的文件。

这些文件的内容相似,看起来像这样

file1:
$xx_ at 10.0 "$elt_(0) coordinates 636.46 1800.37 9.90"
$xx_ at 10.0 "$elt_(1) coordinates 367.78 1263.63 7.90"

对于每个文件,我只想保留元素的索引和紧跟在坐标之后的 2 个数字字段(在同一文件或另一个文件中):

文件 1:

0  636.46 1800.37 
1 367.78 1263.63 

我尝试做的是这样的(但不正确)

find . -name "file*"|while read fname; do
  echo "$fname"
  for line in $(cat "$fname") do
   FS="[_() ]"
   print    "\t"  "\t"  > $fname
  done
done

这就是awk的完美使用。 使用 awk,您可以简单地打印特定的单词。

如果索引是行号,你可以使用这个:

cat -n ./file1 | awk '{print -1 " " " " }' 这只是打印带有行号的文件并打印第一个、第七个和第八个字。

如果索引是括号中的 $elt_(0) 数字,您可以像这样使用 sed:

cat ./file1 | awk '{print  " "  " " }' | sed 's/"$elt_(//g' | sed 's/)//g' | sed 's/"//g'

输出:

1 636.46 1800.37
2 367.78 1263.63

Link to awk Link to sed