Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Bash 更好的grep搜索文件_Bash_Grep - Fatal编程技术网

Bash 更好的grep搜索文件

Bash 更好的grep搜索文件,bash,grep,Bash,Grep,我在bash中有一个简单的脚本,它使用grep在文件中搜索 我想做和下面一样的事情,但我不喜欢使用2个临时文件,有更好的方法吗 #! /bin/bash FILE="text.txt" rm /tmp/temp* temp_file1=$(mktemp /tmp/temp1) temp_file2=$(mktemp /tmp/temp2) cp $FILE $temp_file1 for string in $@ do grep -i "$string" $temp_file1

我在bash中有一个简单的脚本,它使用grep在文件中搜索

我想做和下面一样的事情,但我不喜欢使用2个临时文件,有更好的方法吗

    #! /bin/bash

FILE="text.txt"

rm /tmp/temp*
temp_file1=$(mktemp /tmp/temp1)
temp_file2=$(mktemp /tmp/temp2)

cp $FILE $temp_file1

for string in $@
do
grep -i "$string" $temp_file1 > $temp_file2
mv $temp_file2 $temp_file1
done

cat $temp_file1

谢谢

我使用了
awk
并不是为了提高性能,而是因为它更适合您的需要(在同一行中搜索多个模式,而不考虑订单)

my_script.sh:

search=${@:2}
awk -v search="$search" 'BEGIN{nb_patterns=split(search, a)}{p=1;for(i=1;i<=nb_patterns;i++){if(!(tolower($0)~tolower(a[i]))){p=0}}}p' $1
用法:

sh test.sh test.txt HeLlo WorLd
结果:

hello world
world hello

我使用
awk
不是为了提高性能,而是因为它更适合您的需要(在同一行中搜索多个模式,而不考虑订单)

my_script.sh:

search=${@:2}
awk -v search="$search" 'BEGIN{nb_patterns=split(search, a)}{p=1;for(i=1;i<=nb_patterns;i++){if(!(tolower($0)~tolower(a[i]))){p=0}}}p' $1
用法:

sh test.sh test.txt HeLlo WorLd
结果:

hello world
world hello

下面是一个快速perl脚本:

#/usr/bin/env perl
使用警告;
严格使用;
使用自动模具;
使用列表::Util qw/all/;
使用Getopt::Long;
我的$file;
GetOptions(“file=s”,\$file);
my@patterns=map{qr/$\ui}@ARGV;

打开我的$in,“这里有一个快速的perl脚本可以完成这项工作:

!/usr/bin/env perl
使用警告;
严格使用;
使用自动模具;
使用列表::Util qw/all/;
使用Getopt::Long;
我的$file;
GetOptions(“file=s”,\$file);
my@patterns=map{qr/$\ui}@ARGV;
打开我的$in,“尝试以下操作:

script.sh

echo "$@" | sed 's/[[:space:]]/.*/g' | xargs -Ifile grep -E 'file' test.txt`
示例

test.txt的样本内容:

echo "$@" | sed 's/[[:space:]]/.*/g' | xargs -Ifile grep -E 'file' test.txt`
这是示例文本
完成测试程序的良好实例,是
在执行script.sh时:

echo "$@" | sed 's/[[:space:]]/.*/g' | xargs -Ifile grep -E 'file' test.txt`
/script.sh良好测试是
完成测试程序的良好实例,是
试试这个:

script.sh

echo "$@" | sed 's/[[:space:]]/.*/g' | xargs -Ifile grep -E 'file' test.txt`
示例

test.txt的样本内容:

echo "$@" | sed 's/[[:space:]]/.*/g' | xargs -Ifile grep -E 'file' test.txt`
这是示例文本
完成测试程序的良好实例,是
在执行script.sh时:

echo "$@" | sed 's/[[:space:]]/.*/g' | xargs -Ifile grep -E 'file' test.txt`
/script.sh良好测试是
完成测试程序的良好实例,是
有了
gawk
,我想可能会更简单



使用
gawk
我想它可能会更简单。

我知道awk是一种更快的方法
->哦,我一直认为awk更快,谢谢!更新了问题。
我知道awk是一种更快的方法
->哦,我一直认为awk更快,非常感谢!更新了问题。这似乎不是做同样的事情,它组合搜索词并输出所有结果。我想合并搜索词,只有当它们在文本文件中的同一行时才输出更新到上面的注释:这似乎不是做同样的事情,它没有合并搜索词。它输出单词所在位置的所有结果。我想合并搜索词,仅当它们在文本文件中的同一行时才输出。使用awk时,您必须声明文件吗?我只想在单个文件中搜索,这样就可以在脚本中添加这些内容了吗?当然,看看@Mihir answer这似乎不是做同样的事情,它组合了搜索词并输出所有结果。我想合并搜索词,只有当它们在文本文件中的同一行时才输出更新到上面的注释:这似乎不是做同样的事情,它没有合并搜索词。它输出单词所在位置的所有结果。我想合并搜索词,仅当它们在文本文件中的同一行时才输出。使用awk时,您必须声明文件吗?我只想在单个文件中搜索,这样就可以在脚本中添加这些内容了吗?当然可以查看@Mihir answer谢谢!我更喜欢狂欢script@user3411002shell脚本编写就是将各种语言编写的外部程序结合起来,以完成有用的事情。与使用grep、mv等没有什么不同。谢谢!我更喜欢狂欢script@user3411002shell脚本编写就是将各种语言编写的外部程序结合起来,以完成有用的事情。与使用grep、mv等没有什么不同。这正是我想要的!太简单了!非常感谢。但是如果您使用
/script.sh Test Good yes
,它会匹配任何东西吗?可能它适用于更高版本的bash或GNU版本,不在我的系统上运行。这正是我想要的!太简单了!非常感谢。但是如果您使用
/script.sh Test Good yes
,它会匹配任何东西吗?可能它适用于更高版本的bash或GNU版本,但不在我的系统上运行。