簡體   English   中英

來自grep的BASH輸出

[英]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。

謝謝

使用sedawk可以更輕松,更快速地處理復雜文件。

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM