Skip to content

Instantly share code, notes, and snippets.

@sairam
Created November 15, 2016 18:39
Show Gist options
  • Save sairam/887cb80f8e4e3b8141ce0bdd7eb69eff to your computer and use it in GitHub Desktop.
Save sairam/887cb80f8e4e3b8141ce0bdd7eb69eff to your computer and use it in GitHub Desktop.
Add a new line with string to multiple files
# I needed to not publish posts from past.
# Note: this is slow if you are doing for thousands of files
for file in `ls 200*`; do
echo $file
grep "draft = true" $file # looking for this string if already present.
if [[ $? == 1 ]]; then # if the string is not found. grep returns status 1
echo "string not found"
tempfile=123.txt # creating a temporary file
tac $file | sed '$d' > $tempfile # remove the last line from the main file
echo -e "draft = true\n+++" >> $tempfile # add draft = true along with the removed line
tac $tempfile > $file # reverse and put it back in the original file
rm $tempfile # delete the temporary file
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment