0

I am relatively new to bash and I am testing my code for the first case.

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

I want to subtract all the lines containing '2018-07' from my file. The new file needs to be named $file_bis.txt.

Thanks

2
  • You may use sed command in linux for this purpose Commented Jul 18, 2018 at 9:04
  • 1
    @JPV, adding two sample files along with expected output and file name would help here.. grep -v matches with your text subtract but you have accepted an answer that does opposite.. also, not sure why you are using counter in your script Commented Jul 18, 2018 at 9:39

3 Answers 3

2

With sed or awk it's much easier and faster to process complex files.

sed -n '/2018-07/p' not_processed/*.txt

then you get the output in your console. If you want you can pipe the output to a new file.

sed -n '/2018-07/p' not_processed/*.txt >> out.txt
Sign up to request clarification or add additional context in comments.

4 Comments

But I would like to pipie into different files.
That was not in your example. you pipe all to the same file and overwrite it.
easier and faster depends on use case.. grep would be faster here as well as more compact.. grep '2018-07'
That was my mistake. But could you please tell me ?
0

This is to do it on all files in not_processed/*.txt

for file in not_processed/*.txt
do
  grep -v '2018-07' $file > "$file"_bis.txt
done

And this is to do it only on the first 2 files in not_processed/*.txt

for file in $(ls not_processed/*.txt|head -2)
do
  grep -v '2018-07' $file > "$file"_bis.txt
done

Don't forget to add "" on $file, because otherwise bash considers $file_bis as a new variable, which has no assigned value.

Comments

0

I don't understood why you are using a counter and if condition for this simple requirement. Use below script which will fulfill you requirement:-

#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

Better avoid for loop here as below single line is suffice

grep -h '2018-07' /your/path/*.txt >  bis.txt

4 Comments

don't use ls output, it would not work if file name contains space, newline, etc... no need to save result in variable, when you can use for file in /your/path/*.txt reliably.. and no need for loop when you can do grep '2018-07' /your/path/*.txt (unless there are too many files)
@Sundeep, you are very true. I have just modified his for loop. I have modified my answer with your suggestion. Thanks for your comment and advice.
grep xxx file1 file2 will put "file1:" or "file2:" at the start of matching lines, while grep xxx file1 won't, by default. Use the -H option to force the name to be added to output lines, or -h to suppress the name.
@simon3270, thanks for your advice I have modified my answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.