Loop through multiple files in bash script

Hi Everybody,

I'm a newbie to shell scripting, and I'd appreciate some help. I have a bunch of .txt files that have some unwanted content. I want to remove lines 1-3 and 1028-1098.

#!/bin/bash

for '*.txt' in <path to folder>
do
sed '1,3 d' "$f";
sed '1028,1098 d' "$f";

done

I know my first line is incorrect...How do I loop through all of these text files?

Thanks!!

Your for loop is a bit wonky in the first line. I suggest reading up a bit before trying stuff on system files.

The general form is

for VARIABLE  in list-of-possibilities

So, for your requirement, it would be

for FILE in /some/path/*.txt
do
  # some operation using $FILE 
  # some more with $FILE 
done

Cheers and happy reading!

You probably want to save your changes, and mayhap back to the original file? Make it

sed '...' "$f" >TMP && mv TMP "$f"

, then.