Hallo Zusammen,
ich habe wieder hunderte OBOLETE oder_alt Kanäle in der Channels.conf.
da ich nicht der begnadete Scripter bin habe ich chatGPT mal gefragt.
Ich finde die Antwort durchaus angemessen, werde ich sicherlich mit einer copy mal testen...
VDR stop und wieder starten habe ich noch nicht drin...
Viele Grüße,
Günter
ChatGPT 3.5
I have a long text file called channels.conf. The file is on Linux with one entry per line. I need a programm for Linux Shell, which deletes every line, which contains a certain string. The list of strings to match is stored in file sample.list.
ChatGPT
You can achieve this using a combination of shell commands in a small script or a one-liner. One efficient way to do this is by using grep
in combination with a loop or xargs
. Below, I provide a simple script that reads each line from sample.list
and then uses grep -v
to filter out lines containing each string from channels.conf
. The -v
option in grep
inverts the match, meaning it will select lines that do not match the given pattern.
Here's a straightforward script to accomplish this:
#!/bin/bash
# Path to the input file (the file you want to filter).
input_file="channels.conf"
# Path to the list containing strings to match.
string_list="sample.list"
# Temporary file to hold intermediate results
temp_file=$(mktemp)
# Make sure the temp file is removed on exit
trap "rm -f $temp_file" EXIT
# Copy the original file to the temp file
cp "$input_file" "$temp_file"
# Read each line from the list
while IFS= read -r line
do # Use grep to filter out lines containing the current string, # writing the result back to the temp file grep -vF -- "$line" "$temp_file" > "$temp_file.tmp" && mv "$temp_file.tmp" "$temp_file"
done < "$string_list"
# Move the filtered result back to the original file
mv "$temp_file" "$input_file"
Alles anzeigen
How to Use This Script
- Save the script to a file, for example,
filter_lines.sh
. - Make it executable by running
chmod +x filter_lines.sh
in the terminal. - Execute the script by running
./filter_lines.sh
from the terminal in the directory where yourchannels.conf
andsample.list
files are located.
Explanation
- The script first creates a temporary file using
mktemp
, where it keeps the intermediate results of filtering. - It then enters a loop, reading each line from
sample.list
. - For each line read from
sample.list
, it usesgrep -vF
to filter out lines containing the string from the temporary file. The-F
option treats the pattern as a fixed string, not a regular expression, which is generally safer and more efficient for this use case. - After processing all lines in
sample.list
, it moves the temporary file back tochannels.conf
, effectively updating it with the filtered content.
Remember, this script directly modifies the original channels.conf
file. If you want to keep the original file unchanged, you can modify the last line of the script to move the temporary file to a new file instead.