我正试图从~1200个文件中提取行。我现在有一个文本文件,格式如下:

"1" "keyword1" "filename1"
"2" "keyword2" "filename2"
"3" "keyword3" "filename3"
"4" "keyword4" "filename4"
and so on.

我要做的是检查包含关键字“n”的行的文件名“n”。我猜这可以在bash脚本中使用某种循环来完成,如下所示
for (i in 1:n){
grep "dataframe[i, 2]" dataframe[i,3]}

但我真的很难弄清楚如何在bash脚本中编程,因为我习惯了使用r。

最佳答案

试试这个:

#Iterate over the file, reading one line at a time
#For each line read 3 columns
while read -r col1 col2 col3; do
  #remove leading and trailing quotes (") with sed
  pattern=`sed -e 's/^"//' -e 's/"$//' <<<"$col2"`;
  file=`sed -e 's/^"//' -e 's/"$//' <<<"$col3"`;
  echo "Matches in $file:"
  #find matches with grep
  grep "$pattern" "$file";
  echo ""
done < list.txt

添加任何想要grep的参数,比如-n表示行号。

09-09 20:14
查看更多