Jump to content

Bash script to change NFO genres


Recommended Posts

Posted

Hey Guys,
I have a problem with wrong genres: (Reported Link)
now I have created a bash-script to delete all genres from documentary series episodes: (I have a own library for documentary and i want specific
genres like "Natur", "Physics","Animals","Crime",...)

#!/bin/bash
DIR="/mnt/media-series/docu"
echo "Start clean Emby-NFO Genres"
find $DIR -mindepth 3 -maxdepth 3 -type f -name "*.nfo" -print0 | xargs -0 sed -i '/<genre>.*<\/genre>/d'
echo "Finish!"


That works perfectly. now I would like to also block genres field (for new meta-search):

#!/bin/bash
DIR="/mnt/media-series/docu"
echo "Start block Emby-NFO Genres"
find $DIR -mindepth 3 -maxdepth 3 -type f -name "*.nfo" -print0 | xargs -0 sed -zi '/<lockedfields>Genres<\/lockedfields>/!s/$/\n<lockedfields>Genres<\/lockedfields>/'
echo "Finish!"

The script searches whether a .NFO file has the entry "<lockedfields>Genres</lockedfields>", if not this text should be entered.
The problem: The command saves it in the last line. I want to put it on line 6, but I can't.

Problem line: sed -zi '/<lockedfields>Genres<\/lockedfields>/!s/$/\n<lockedfields>Genres<\/lockedfields>/' (n = last line, i try 6a but only errors)
bash experts out here?
:) 

Posted

HI, I don't know the answer to your question, but thanks for sharing this.

Posted (edited)

i have studied for hours and written the best solution to delete all genres and lock it:

#!/bin/bash
# Change media-path:
DIR="/mnt/media/docu"
echo "Starte Emby-NFO Genre Script..."
# 
# List all .NFO files
# !!! NOTICE: I use it ONLY delete Seasons and Episodes genres:
# Warning : If you want to change ALL NFOs in selected "DIR" (include series + season + episodes) remove "-mindepth 3 -maxdepth 3"
# Or remove it for a movie folder 
# 
find $DIR -mindepth 3 -maxdepth 3 -type f -name "*.nfo" -print0 | 
while IFS= read -r -d '' file; do 
# Open only .NFOs without genre blocker entries
if ! grep -q "<lockedfields>Genres</lockedfields>" "$file";
then
  # REMOVE all genre entries
  sed -i "/<genre>.*<\/genre>/d" "$file"
  # INSERT genre blocker
  sed -i "6i \ \ <lockedfields>Genres<\/lockedfields>" "$file"
  # OUTPUT which file was edit
  echo "File edited: '$file'"
fi
done
echo "Script finished ..."

This script first checks whether an NFO has locked genre or not. (Only open UNLOCKED files)
If the "Genre" field was not set to locked, all genres are deleted first,
then the field is set to locked "genres"

----
For Unraid user or user with a SSD Cache drive (better because you only work with newly added files) :

#!/bin/bash
# Change to only CACHE dir:
DIR="/mnt/mycache/dive/media/docu"

# Check if cache folder exists
if [ -d "$DIR" ]; then
  # insert the script
fi

Now use a cron-job (run every hour) and i never have genres in my documentary episodes again 😀

Sorry for my bad english guys 😄

Edited by EliteGroup
  • Thanks 1

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...