[英]BASH output from grep
我对bash比较陌生,并且我正在测试第一种情况的代码。
counter=1
for file in not_processed/*.txt; do
if [ $counter -le 1 ]; then
grep -v '2018-07' $file > bis.txt;
counter=$(($counter+1));
fi;
done
我想从文件中减去所有包含“ 2018-07”的行。 新文件需要命名为$ file_bis.txt。
谢谢
使用sed
或awk
可以更轻松,更快速地处理复杂文件。
sed -n '/2018-07/p' not_processed/*.txt
然后您将在控制台中获得输出。 如果需要,可以将输出通过管道传输到新文件。
sed -n '/2018-07/p' not_processed/*.txt >> out.txt
我不明白您为什么要使用计数器以及if
满足此简单要求的条件。 使用以下脚本将满足您的要求:-
#first store all the files in a variable
files=$(ls /your/path/*.txt)
# now use a for loop
for file in $files;
do
grep '2018-07' $file >> bis.txt
done
最好避免在此处进行for循环,因为单行以下就足够了
grep -h '2018-07' /your/path/*.txt > bis.txt
这是对not_processed / *。txt中的所有文件执行的操作
for file in not_processed/*.txt
do
grep -v '2018-07' $file > "$file"_bis.txt
done
这是仅对not_processed / *。txt中的前两个文件执行的操作
for file in $(ls not_processed/*.txt|head -2)
do
grep -v '2018-07' $file > "$file"_bis.txt
done
不要忘记在$ file上添加“”,否则bash会将$ file_bis视为没有赋值的新变量。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.